Home > other >  How to write custom Elasticsearch Query in Java?
How to write custom Elasticsearch Query in Java?

Time:07-29

This is my query on Elasticsearch,

GET index101/_search
{
  "query": {
    "function_score": {
      "boost_mode": "replace", 
      "query": {
        "constant_score": {
          "filter": {
            "terms": {
              "fields": ["767","434", "101", "222"]
            }
          }
        }
      },
      "pqcode_score": {
             "descriptors": [
              {
            "descriptor": "base64string"
              }
        ],
        "pqparams": {
        "bucket_field": "fields",
        "pqcode_field": "fields2",
        "distance_function": "similarity",
        "model": "random"
        }
      }
    }
  }
}

Looked into the documentation of Elasticsearch with Java, but couldn't find anything that could resolve this query in Java.

I created a JSON file, and got the input query in the jsonObject and then passed it as a parameter to searchSourceBuilder.query(). But it gives the error that the jsonObject can't be converted into QueryBuilder.

How can we go ahead with this query in Java?
Is there any other workaround for this?

CodePudding user response:

looks like pqcode_score is your custom key in the Elasticsearch query, and you are trying to add the custom component/constructs in your Elasticsearch query thats not possible, hence you are getting the error.

You need to use the constructs in your Elasticsearch query thats supported by Elasticsearch.

  • Related