javaelasticsearchelasticsearch-queryelasticsearch-rest-client

Write elastic search filter search using ES Java API v7.x


I tried to write a filter query using elastic search Java API version 7.6

But there is no good documentation on how to write a filter context search.

Anyone know how to write Java API for the following:

GET /_search
{
  "query": { 
    "bool": { 
      "must": [
        { "match": { "title":   "Search"        }},
        { "match": { "content": "Elasticsearch" }}
      ],
      "filter": [ 
        { "term":  { "status": "published" }},
        { "range": { "publish_date": { "gte": "2015-01-01" }}}
      ]
    }
  }
}

Solution

  • Try the following

    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    List<QueryBuilder> mustClauses = boolQueryBuilder.must();
    mustClauses.add(QueryBuilders.matchQuery("title", "Search"));
    mustClauses.add(QueryBuilders.matchQuery("content", "Elasticsearch"));
    
    List<QueryBuilder> filterClauses = boolQueryBuilder.filter();
    filterClauses.add(QueryBuilders.termQuery("status", "published"));
    filterClauses.add(QueryBuilders.rangeQuery("publish_date").gte("2015-01-01"));
    
    
    SearchRequest searchRequest = new SearchRequest();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(boolQueryBuilder);
    searchRequest.source(searchSourceBuilder);
    
    System.out.println(searchRequest.toString());
    

    The resulting query is

    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "title": {
                  "query": "Search",
                  "operator": "OR",
                  "prefix_length": 0,
                  "max_expansions": 50,
                  "fuzzy_transpositions": true,
                  "lenient": false,
                  "zero_terms_query": "NONE",
                  "auto_generate_synonyms_phrase_query": true,
                  "boost": 1.0
                }
              }
            },
            {
              "match": {
                "content": {
                  "query": "Elasticsearch",
                  "operator": "OR",
                  "prefix_length": 0,
                  "max_expansions": 50,
                  "fuzzy_transpositions": true,
                  "lenient": false,
                  "zero_terms_query": "NONE",
                  "auto_generate_synonyms_phrase_query": true,
                  "boost": 1.0
                }
              }
            }
          ],
          "filter": [
            {
              "term": {
                "status": {
                  "value": "published",
                  "boost": 1.0
                }
              }
            },
            {
              "range": {
                "publish_date": {
                  "from": "2015-01-01",
                  "to": null,
                  "include_lower": true,
                  "include_upper": true,
                  "boost": 1.0
                }
              }
            }
          ],
          "adjust_pure_negative": true,
          "boost": 1.0
        }
      }
    }