spring-bootelasticsearchspring-data-elasticsearch

elasticsearch script_score query with java client


I'm using spring-boot 3.1.5 and spring-data-elasticsearch 5.1.5 java project. I try to make client program with this query.

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~~~~~~]
              }
            }
          }
        }
      ]
    }
  }
}

and my java code is like this.

        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();

but i got this exception. co.elastic.clients.util.MissingRequiredPropertyException: Missing required property 'ScriptScoreQuery.query' at co.elastic.clients.util.ApiTypeHelper.requireNonNull(ApiTypeHelper.java:76) at co.elastic.clients.elasticsearch._types.query_dsl.ScriptScoreQuery.(ScriptScoreQuery.java:63) at co.elastic.clients.elasticsearch._types.query_dsl.ScriptScoreQuery.(ScriptScoreQuery.java:49) at co.elastic.clients.elasticsearch._types.query_dsl.ScriptScoreQuery$Builder.build(ScriptScoreQuery.java:184) at co.elastic.clients.elasticsearch._types.query_dsl.ScriptScoreQuery$Builder.build(ScriptScoreQuery.java:124) at co.elastic.clients.elasticsearch._types.query_dsl.Query$Builder.scriptScore(Query.java:1601)

what did i miss?

c


Solution

  • As the error states it

    Missing required property 'ScriptScoreQuery.query' at

    The script_score query requires you to 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~~~~~~]
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    So you also need to specify a query in

        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();