Java API and range queries


(Stefano Violetta) #1

Hello,
I'd like to perform a range query containing:

 {
 	"range": {
 		"a": {
 			"lte": 1
 		},
 		"b": {
 			"gte": 1
 		},
 		"c": {
 			"gte": 1
 		},
 		"d": {
 			"lte": 2
 		}
 	}
 }

(a, b, c and d are sub-fields of a nested object) using the Java API version 2.2 (I cannot update them).
The problem is that the Java API seems to allow only a single field inside the Range Query:

RangeQueryBuilder rangeQueryBuilder = new RangeQueryBuilder("a");
rangeQueryBuilder.lte(1);

How could I do that?
Thanks for your help.


(David Pilato) #2

I don't think you can do that in REST either.

But wrap all your range queries inside a boolean query, add them as filters and you should be ok.


(Stefano Violetta) #3

I did it, in REST (via Sense); that's why I'm trying to do that in Java :slight_smile:


(David Pilato) #4

Hmmmm. This clearly not documented: https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-range-query.html

I think that in that case may be only one of the range query is applied, but unsure though.
May be a full test would help to clarify that.

But whatever, I'd use a bool query here.


(Stefano Violetta) #5

I did it using a nested object... (of course I put a mapping first)

{
  "query": {
    "bool": {
      "must": [       
        {
          "nested": {
            "path": "the_path", 
            "query": {
              "bool": {                  
                "must": [ 
                  {"range": {
                      "the_path.a": {"lte":2},
                      "the_path.b": {"gte":2},
                      "the_path.c":{"gte":18},
                      "the_path.d":{"lte":200}
                    }
                  }
                ]
              }
            }
          }
        }
      ]
}}}

I'll try to follow your advice, anyway, and reach the expected result in some other way.
Thanks.


(Stefano Violetta) #6

At the end, I was doing something wrong...

I changed my query to

        {
	"query": {
		"nested": {
			"path": "the_path",
			"query": {
				"bool": {
					"must": [{
						"range": {
							...
						}
					}, {
						"range": {
							...
						}
					}, {
						"range": {
							...
						}
					}, {
						"range": {
							...
						}
					}]
				}
			}
		}
	}
}

And it works!


(system) #7

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