How to search with only raw query JSON with Elastic Java API 6.5.1?

How can I take the raw JSON String and query via the Java API? It should
work for any complex query that Curl accepts in Elastic.

For example:

{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "name": "<name>"
          }
        },
        {
          "match": {
            "address": {
              "query": "<address>",
              "fuzziness": 1,
              "prefix_length": 1,
              "operator": "or",
              "minimum_should_match": "80%"
            }
          }
        },
        {
          "match_phrase": {
            "city_nm": "<city_nm>"
          }
        },
        {
          "term": {
            "state_province_cd": "<state_province_cd>"
          }
        }
      ]
    }
  }
}

I tried Query Builders' simpleQueryStringQuery method to achieve this but it creates the query which doesn't give a correct result.

For Example, to search a record using just name, this is the query which simpleQueryStringQuery is generating but instead of returning just one record it returns multiple records.

{
  "query": {
    "simple_query_string": {
      "query": """{"query":{"bool":{"must":[{"match_phrase":{"name":"Neeraj"}}]}}}"""
    }
  }
}

whereas if I run the same query like this in my Kibana console that returns the correct number of results.

{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "name": "Jaas"
          }
        }
      ]
    }
  }
}

If you want to just pass a JSON to any endpoint, you can use the Low Level client...

But if you want to mimic your QueryDSL with Java, you can use a boolQuery builder: https://artifacts.elastic.co/javadoc/org/elasticsearch/elasticsearch/6.7.0/org/elasticsearch/index/query/QueryBuilders.html#boolQuery--

And add all the must clauses you need with https://artifacts.elastic.co/javadoc/org/elasticsearch/elasticsearch/6.7.0/org/elasticsearch/index/query/BoolQueryBuilder.html#must(org.elasticsearch.index.query.QueryBuilder)

Can you please help me with the method name in low level client to be used for my requirement.

Read https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low-usage-requests.html

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.