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

(Varun) #1

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 = SearchRequest(index)
            .source(new SearchSourceBuilder()
                                            .minDocCount(minDocCount)                       ))
                                   .size(0)                ));

(David Pilato) #2

You need to provide a query as explained in

The boolQuery allows you to pass a must not query.

(Varun) #3

Is it like this ? or any other way ?

                BoolQueryBuilder bool = new BoolQueryBuilder().must(new RangeQueryBuilder("FieldName")

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

(David Pilato) #4

Even easier:


But yes.

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

May be this:

(Varun) #5
 SearchResponse response = SearchRequest(index) 

                    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 ?

(David Pilato) #6

What do you put in " My json query "?

(Varun) #7

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

(David Pilato) #8

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

(Varun) #9

That worked. Thanks

(system) #10

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