Lucene Query String Elasticsearch "less than or equal to"[URI Search]

O Connor picture O Connor · May 15, 2014 · Viewed 52.6k times · Source

On so many websites they teach how to query data from Elasticsearch using range query. I would like to query data that is less than or equal to a certain number from Elasticsearch using Lucene Style Query String like this.

fieldname:[* TO 100] 

or

fieldname:["*" TO "100"]

I have tried in other formats but none of those worked. Can someone help me?

Answer

John Petrone picture John Petrone · May 15, 2014

You will want to use Query String Syntax (https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html) ranges combined with the URI Search (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html)

Ranges

Ranges can be specified for date, numeric or string fields. Inclusive ranges are specified with square brackets [min TO max] and exclusive ranges with curly brackets {min TO max}.

    All days in 2012:

    date:[2012/01/01 TO 2012/12/31]

    Numbers 1..5

    count:[1 TO 5]

    Tags between alpha and omega, excluding alpha and omega:

    tag:{alpha TO omega}

    Numbers from 10 upwards

    count:[10 TO *]

    Dates before 2012

    date:{* TO 2012/01/01}

Curly and square brackets can be combined:

    Numbers from 1 up to but not including 5

    count:[1..5}

Ranges with one side unbounded can use the following syntax:

age:>10
age:>=10
age:<10
age:<=10

Note

To combine an upper and lower bound with the simplified syntax, you would need to join two clauses with an AND operator:

age:(>=10 AND < 20)
age:(+>=10 +<20)

The parsing of ranges in query strings can be complex and error prone. It is much more reliable to use an explicit range filter.

URI Search

Search URI Search Request Body Search Search Shards API Search Template Facets Aggregations Suggesters Context Suggester Multi Search API Count API Validate API Explain API Percolator More Like This API Benchmark

A search request can be executed purely using a URI by providing request parameters. Not all search options are exposed when executing a search using this mode, but it can be handy for quick "curl tests". Here is an example:

$ curl -XGET
'http://localhost:9200/twitter/tweet/_search?q=user:kimchy'