elasticsearch脚本_score查询与java客户端

csbfibhn  于 12个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(139)

我正在使用spring-boot 3.1.5和spring-data-elasticsearch 5.1.5 java项目。我尝试使用此查询编写客户端程序。

GET vector_search/_search
{
  "query": {
    "function_score": {
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "Math.max(0,cosineSimilarity(params.queryVector,'text_VEC'))",
              "params": {
                "queryVector": [0.1,0.1,0.0.1~~~~~~]
              }
            }
          }
        }
      ]
    }
  }
}

字符串
我的java代码是这样的。

Query searchQuery = NativeQuery.builder()
                .withQuery(q -> q.scriptScore(m -> m.script(fn -> fn.inline(InlineScript.of(fn2 -> fn2.source("Math.max(0,cosineSimilarity(params.queryVector,'text_VEC'))"))))))
                .withQuery(q -> q.scriptScore(m -> m.script(fn -> fn.inline(InlineScript.of(fn2 -> fn2.params(params))))))
                .withMaxResults(5)
                .build();


但是我得到了这个异常。co. elastic. clients. util. MissingRedPropertyException:在co. elastic. clients. util. ApiTypeHelper. requireNonError处缺少必需的属性"ScriptScoreQuery. query "(ApiTypeHelper.java:在co.elastic.clients.elasticsearch._types. query_dsl.ScriptScoreQuery中找到。(ScriptScoreQuery.java:63)位于co.elastic.clients.elasticsearch._types. query_dsl.ScriptScoreQuery。(ScriptScoreQuery.java:49)位于co.elastic.clients.elasticsearch._types. query_query. ScriptScoreQuery $www.example.com(ScriptScoreQuery.java:在co.elastic.clients.elasticsearch._types. query_credit. ScriptScoreQuery $www.example.com上搜索(ScriptScoreQuery.java:124)at co.elastic.clients.elasticsearch._types. query_query. Query $Builder. scriptScore(Query.java:1601)
我错过了什么?
C

byqmnocz

byqmnocz1#

如错误所述,
在以下位置缺少必需的属性“ScriptScoreQuery.query "
script_score查询要求您specify a query

GET vector_search/_search
{
  "query": {
    "function_score": {
      "functions": [
        {
          "script_score": {
            "query": {       <---
                ...          <--- this is required
            },               <---
            "script": {
              "source": "Math.max(0,cosineSimilarity(params.queryVector,'text_VEC'))",
              "params": {
                "queryVector": [0.1,0.1,0.0.1~~~~~~]
              }
            }
          }
        }
      ]
    }
  }
}

字符串
因此,您还需要在

Query searchQuery = NativeQuery.builder()
            .withQuery(q -> q.scriptScore(
                 m -> m.query(...)      <---- this is required
                       .script(fn -> fn.inline(InlineScript.of(
                           fn2 -> fn2.source("Math.max(0, cosineSimilarity(params.queryVector, 'text_VEC'))")                                              
                                     .params(params)
                       )
                     )
                  )
               )
            )
            .withMaxResults(5)
            .build();

相关问题