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

3

47

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?

Cruce answered 15/5, 2014 at 9:27 Comment(2)
if you are not satisfied with answers ask again.. Don't abandon the questions. Accept answers and help others..Cytokinesis
Just tried @John Petrone 's answer and it worked. fieldname:<10Articular
F
70

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'
Flattop answered 15/5, 2014 at 14:39 Comment(0)
C
4

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

Cytokinesis answered 15/5, 2014 at 10:3 Comment(4)
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.Cruce
It's 1.0. Hope you can help me out.Cruce
You should mention your query in which format you want.Gimme some time i ll get back to youCytokinesis
For example: fieldname: <= "value1". This example does not work but just give you an idea. Normally it works with: fieldname:"value1"Cruce
B
0

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'
Bethina answered 28/11, 2023 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.