Lucene Query String Elasticsearch“小于或等于”[URI搜索]

ef1yzkbh  于 5个月前  发布在  ElasticSearch
关注(0)|答案(3)|浏览(61)

在很多网站上,他们教如何使用范围查询从Elasticsearch中查询数据。我想使用Lucene样式查询字符串从Elasticsearch中查询小于或等于某个数字的数据,就像这样。

fieldname:[* TO 100]

字符串

fieldname:["*" TO "100"]


我尝试过其他格式,但都不起作用。有人能帮助我吗?

wgmfuz8q

wgmfuz8q1#

您将需要使用查询字符串搜索(https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html)范围与URI搜索(https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html)相结合
范围
可以为日期、数字或字符串字段指定范围。包含范围用方括号[min TO max]指定,排除范围用花括号{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 URI Search Request Body Search Search Shards API Search Template Facets Aggregations Suggesters Context Suggesters Multi Search API Count API Reply API Explain API Percolator More Like This API Benchmark
通过提供请求参数,可以纯粹使用URI来执行搜索请求。使用此模式执行搜索时,并不是所有搜索选项都会暴露,但它可以方便地进行快速“curl测试”。下面是一个示例:

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

u1ehiz5o

u1ehiz5o2#

我认为你想查询小于等于100的文档。

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

字符串

PHP API客户端

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


更多查询..参考

您要求的查询格式..!

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


希望能帮到你!

hmae6n7t

hmae6n7t3#

扩展BlackPOP的答案

很高兴知道对于range date searches,您必须使用引号,否则年份可以被解释为时间戳:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html#numeric-date
如果没有指定日期格式,并且范围查询的目标是日期字段,则会解释表示自纪元以来的毫秒的数值。如果希望值表示年份,例如2020,则需要将其作为String值(例如“2020”)传递,该值将根据默认格式或设置格式进行解析。

示例

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'

字符串

相关问题