O Connor
O Connor

Reputation: 4402

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

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?

Upvotes: 48

Views: 79591

Answers (4)

Marcel Matus
Marcel Matus

Reputation: 171

I was just exploring this same topic now, and it is worth mentioning that gt/gte/lt/lte in a query_string works well, ie. like this:

duration:>=100

BUT NOT like this:

duration :>= 100

The space after the equal sign cause query_shard_exception, Caused by type number_format_exception...

Upvotes: 1

user8026617
user8026617

Reputation: 1

Expanding on BlackPOP's answer

It is good to know that for range date searches you have to use quotes otherwise the year can be interpreted as a timestamp:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html#numeric-date

When no date format is specified and the range query is targeting a date field, numeric values are interpreted representing milliseconds-since-the-epoch. If you want the value to represent a year, e.g. 2020, you need to pass it as a String value (e.g. "2020") that will be parsed according to the default format or the set format.

Example

curl -s -X GET "localhost:9200/backup/_search?pretty=true" -H 'Content-Type: application/json' -d ' {
  "query": {
    "range": {
      "lastBackupStartTime": {"gt" : "2023-08-01"}
    }
  }
}' | jq '.hits.hits[] | ._source.id'

Upvotes: 0

John Petrone
John Petrone

Reputation: 27517

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'

Upvotes: 71

BlackPOP
BlackPOP

Reputation: 5747

I think you wanna query the documents with less than equal to 100.

 curl -XPOST "http://hostname:9200/index/try/_search" -d'
{
 "query": {
    "range": {
      "FieldName": {
         "lte" : 100
      }
    }
  }
}'

PHP API client

array(
'query' => array(
    'range' => array(
        'FieldName' => array(
            array("lte" => 100)
        )
    )
  )
);

for more queries.. refer

The query format thet you asked for..!

curl -XPOST "http://hostname:9200/index/type/_search?q=FieldName:[* to 100]"

HOpe it helps..!

Upvotes: 4

Related Questions