0
votes

I am trying to run RANGE query on nested document using ES 7.7 Java API. I noticed that it is not filtering data as expected. When I put debug statements on the actual query that ES runs, I am not able to get the appropriate results back mainly for "to" section of range query. I have noticed that GT or GTE works fine by LT and LTE is not working as expected as well.

Below is the simple Java code for this range query. Please note that weight within the nested document is defined as float like ""weight": {"type": "float"},".

query.must(QueryBuilders.nestedQuery(fields[0], QueryBuilders.rangeQuery(c.getField()).from(c.getValues().get(0)).to(c.getValues().get(1)),ScoreMode.None).innerHit(innerHitBuilder));

Below is the Java API code example for LT.

query.must(QueryBuilders.nestedQuery(fields[0], QueryBuilders.rangeQuery(c.getField()).lt(c.getValue()), ScoreMode.None).innerHit(innerHitBuilder));

Below is the query that ES actually runs for the above statement RANGE query statement.

{
   "from":0,
   "size":50,
   "query":{
      "bool":{
         "must":[
            {
               "match_phrase":{
                  "fundsponsor":{
                     "query":"vanguard group inc",
                     "slop":0,
                     "zero_terms_query":"NONE",
                     "boost":1.0
                  }
               }
            },
            {
               "nested":{
                  "query":{
                     "match_phrase":{
                        "holdings.componentticker":{
                           "query":"baba",
                           "slop":0,
                           "zero_terms_query":"NONE",
                           "boost":1.0
                        }
                     }
                  },
                  "path":"holdings",
                  "ignore_unmapped":false,
                  "score_mode":"none",
                  "boost":1.0,
                  "inner_hits":{
                     "name":"holdings.componenttickerbaba",
                     "ignore_unmapped":false,
                     "from":0,
                     "size":100,
                     "version":false,
                     "seq_no_primary_term":false,
                     "explain":false,
                     "track_scores":false
                  }
               }
            },
            {
               "nested":{
                  "query":{
                     "range":{
                        "holdings.weight":{
                           "from":1.0,
                           "to":2.0,
                           "include_lower":true,
                           "include_upper":true,
                           "boost":1.0
                        }
                     }
                  },
                  "path":"holdings",
                  "ignore_unmapped":false,
                  "score_mode":"none",
                  "boost":1.0,
                  "inner_hits":{
                     "name":"holdings.weightnull",
                     "ignore_unmapped":false,
                     "from":0,
                     "size":100,
                     "version":false,
                     "seq_no_primary_term":false,
                     "explain":false,
                     "track_scores":false
                  }
               }
            }
         ],
         "adjust_pure_negative":true,
         "boost":1.0
      }
   },
   "_source":{
      "includes":[
         "fundsymbol",
         "fundsponsor",
         "fundname",
         "componentcount",
         "holdings.componentticker",
         "holdings.weight"
      ],
      "excludes":[



      ]
   }
}

Please pay attention to the below section which is the culprit here. Everything else works as expected. As I have mentioned above, "from" works as expected and it gets converted to GTE as expected but "to" doesn't work and neither does "LT" or "LTE" for some weird reason.

               "nested":{
                  "query":{
                     "range":{
                        "holdings.weight":{
                           "from":1.0,
                           "to":2.0,
                           "include_lower":true,
                           "include_upper":true,
                           "boost":1.0
                        }
                     }
                  },
1

1 Answers

0
votes

First, from and toare not range query properties. Range query expects lte, le, gte and ge. The query should look like:

{
    "query": {
        "nested": {
            "path": "holdings",
            "query": {
                "range": {
                    "holdings.weight": {
                        "gte": 1,
                        "lte": 2
                    }
                }
            }
        }
    }
}

Second, check your index mapping and make sure that holdings is mapped as a nestedand that weight is mapped as float.