How to use elasticsearch rangequery to find values that are less than or equal to a number value in Java API

70
April 11, 2021, at 6:40 PM

Hello I'm new to Elastic Search and I'm trying to build an elastic search query using Java API. I have the following.

int count = 7
QueryBuilder findRangeNumber = QueryBuilders.rangeQuery("numberField").lte(count);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(findRangeNumber); 

This return numberField that are equal to 12,6,4, and, 5. I want it to return values where numberField is less than or equal to count (so the number 6,5, and 4 in the example).

If I change number count to 12 it only return numberField that are equal to 12. I'm confuse to how this works and if it possible to it return any value of numberField that is less than or equal to count.

I also have tried the following with no luck

int count = 7
QueryBuilder findRangeNumber = QueryBuilders.rangeQuery("numberField").lte(count).gte(0);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(findRangeNumber); 

This is what the query look like when I print out boolQueryBuilder

"must" : [
            {
              "range" : {
                "numberField" : {
                  "from" : null,
                  "to" : 7,
                  "include_lower" : true,
                  "include_upper" : true,
                  "boost" : 1.0
                }
              }
            }
          ],

This is what the full code part look like, of what I'm trying to do.

BoolQueryBuilder boolQuery = new BoolQueryBuilder();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
int count = 7
    if (foo != null) {
        boolQuery.should(QueryBuilders.matchQuery("something", matchSomething));
        boolQuery.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("foobar status")));
    }
    if (foobar != null) {
        BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
        queryBuilder.should(QueryBuilders.rangeQuery("someDateField").from(dateField));
        queryBuilder.should(QueryBuilders.rangeQuery("numberField").lte(count));
        boolQuery.must(queryBuilder);
     }
    searchSourceBuilder.query(boolQuery);

Any help would be appreciated!

Answer 1

You need to use RangeQueryBuilder to find values that are less than or equal to a given number

Try out this below code

int count=7;
RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
RangeQueryBuilder rangeQueryBuilder = new RangeQueryBuilder("numberField").lte(count);
searchSourceBuilder.query(rangeQueryBuilder);
SearchRequest searchRequest = new SearchRequest("my-index");
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
READ ALSO
How to save agora stream in Android?

How to save agora stream in Android?

I am using agora sdk for streamingI want to save the stream in agora cloud

68
can't receive response from OpenWeatherMap API at all

can't receive response from OpenWeatherMap API at all

I am new to backend programming and recently I've this problem that I cant receive any data from Open Map Weather API

31
How to center horizontal scrollbar?

How to center horizontal scrollbar?

How can I position the scrollbar in the center of the parent div like so?

90