How to write must and must not range query in Highlevel client

Hi i write a aggregation query in new java api using HighLevelClient. I need help to add must and must not range query for filtering the data.

  1. How can i add it ?
  2. Is it possible to add json query with query builder ?

My code is as like below

    Map<String, String> aggVal = = new Map<String, String>();

    String index = aggVal.get("Index");
    String mainAggField = aggVal.get("Main_Aggregation_FieldName");
    String subAggField = aggVal.get("Sub_Aggregation_FieldName");
    String histFormat = aggVal.get("Histogram_Format");
    String histIntrvl = aggVal.get("Histogram_Interval");
    long minDocCount = Long.valueOf(aggVal.get("Min_Doc_Count"));

    DateHistogramInterval Intrval = new DateHistogramInterval(histIntrvl);

    SearchResponse response = client.search(new SearchRequest(index)
            .source(new SearchSourceBuilder()
                    .aggregation(
                            AggregationBuilders.dateHistogram("mainAgg")
                                    .field(mainAggField)
                                    .dateHistogramInterval(Intrval)
                                    .format(histFormat)
                                    .minDocCount(minDocCount)
                                    .subAggregation(
                                            AggregationBuilders.terms("subAgg").field(subAggField)
                                            .minDocCount(minDocCount)                       ))
                                   .size(0)                ));

You need to provide a query as explained in https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search.html.

https://artifacts.elastic.co/javadoc/org/elasticsearch/elasticsearch/6.1.1/org/elasticsearch/index/query/QueryBuilders.html#boolQuery--

The boolQuery allows you to pass a must not query.

Is it like this ? or any other way ?

                BoolQueryBuilder bool = new BoolQueryBuilder().must(new RangeQueryBuilder("FieldName")
                            .format("")
                            .from("")
                            .to(""));

How can i pass json query DSL to query builder ? Is it possible ?
That way i can easily change the bool query and dynamically filter the response

Even easier:

QueryBuilders.boolQuery()
    .must(QueryBuilders.rangeQuery("FieldName").from(xxx).to(yyy));

But yes.

How can i pass json query DSL to query builder ? Is it possible ?

May be this: QueryBuilders (core 6.1.1 API)

 SearchResponse response = client.search(new SearchRequest(index) 
            .source(

                    new SearchSourceBuilder()

                            .query( new WrapperQueryBuilder( " My json query "))

Throws error as follows

{"error":{"root_cause":[{"type":"parse_exception","reason":"Failed to derive xcontent"}],"type":"parse_exception","reason":"Failed to derive xcontent"},"status":400}

How can i do it ?

What do you put in " My json query "?

i am thinking to use a must/must not query in place of "My json query"

but it will be changing. So need the query part in java api to be a json query.

"query": {"bool": {      "must": 
[{"range" : {
        "@timestamp" : {
            "gte": "01-21-2017 08",
            "lte": "07-21-2018 05",
            "format": "MM-dd-yyyy hh"
        }} }]}}

I "think" you need to pass something like:

{"bool": {      "must": 
[{"range" : {
        "@timestamp" : {
            "gte": "01-21-2017 08",
            "lte": "07-21-2018 05",
            "format": "MM-dd-yyyy hh"
        }} }]}}

That worked. Thanks

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