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

61,584

Solution 1

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'

Solution 2

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..!

Share:
61,584
O Connor
Author by

O Connor

Updated on July 21, 2022

Comments

  • O Connor
    O Connor almost 2 years

    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?

  • O Connor
    O Connor about 10 years
    I think this is not a Lucene query string. I use a Elasticsearch PHP Client API and I don't want to use a JSON or an array format as parameter to query data but would like to use a Lucene query string instead.
  • O Connor
    O Connor about 10 years
    It's 1.0. Hope you can help me out.
  • BlackPOP
    BlackPOP about 10 years
    You should mention your query in which format you want.Gimme some time i ll get back to you
  • O Connor
    O Connor about 10 years
    For example: fieldname: <= "value1". This example does not work but just give you an idea. Normally it works with: fieldname:"value1"