gpt4 book ai didi

elasticsearch - 在 elasticsearch 中查询多个日期的多个范围

转载 作者:行者123 更新时间:2023-11-29 02:45:47 25 4
gpt4 key购买 nike

以下range_query按预期返回结果:

{"query": {
"bool": {
"must": [
{
"range": {
"created_at": {
"gte": "2013-12-09"
}
}
}
]
}
}
}

但是将多个范围查询结合在一起,什么也不返回:

{"query": {
"bool":{
"must": [
{
"and": [
{
"range": {
"created_at": {
"gte": "2013-12-09"
}
}
},
{
"range": {
"happens_on": {
"lte": "2013-12-16"
}
}
},
{
"range": {
"created_at": {
"lte": "2013-12-14"
}
}
}
]
}
]
}
}
}

在多个字段上使用多个 range_queries 的正确方法是什么?

编辑:啊,好吧,这就是我使用 range_filter 而不是 range_query 的地方?这听起来很有希望,所以我只使用一个范围过滤器重新编写了我的查询。在这里发布所有内容,以防我在其他地方弄乱查询。我正在执行 GET,源 key 中的所有内容实际上都是 JSON,但为了便于阅读,我删除了转义的连字符:

{
"source": {
"filtered": {
"filter": {
"and": [
{
"term": {
"restricted": false
}
},
{
"not": {
"term": {
"deleted": true
}
}
},
{
"range": {
"happens_on": {
"lte": "2013-12-16"
}
}
}
]
},
"query": {
"bool": {
"must": [
]
}
}
},
"from": 0,
"size": 10
}
}

遗憾的是,我的问题还是一样:我没有得到任何点击。

EDIT2: 因此,像 Njal 建议的那样,沿着 must 子句内的范围走下去。这给了我一个像这样的多范围查询:

{
"source": {
"filter": {
"and": [
{
"term": {
"restricted": false
}
},
{
"not": {
"term": {
"deleted": true
}
}
}
]
},
"from": 0,
"query": {
"bool": {
"must": [
{
"range": {
"happens_on": {
"gte": "2013-12-06"
}
}
},
{
"range": {
"created_at": {
"gte": "2013-12-15"
}
}
},
{
"range": {
"happens_on": {
"lte": "2013-12-17"
}
}
},
{
"range": {
"created_at": {
"lte": "2013-12-17"
}
}
}
]
}
},
"size": 10
}
}

仍然没有返回结果。我在这里犯了什么明显的错误吗?

最佳答案

在您的 bool 查询的 must 子句下,无需将其包装在 and 中。没有 查询,也许您正在考虑 and filter

Example runnable play为方便起见作为 curl 命令:

#!/bin/bash

export ELASTICSEARCH_ENDPOINT="http://localhost:9200"

# Create indexes

curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{
"settings": {},
"mappings": {
"type": {
"properties": {
"created_at": {
"type": "date",
"format": "dateOptionalTime"
},
"name": {
"type": "string"
},
"happens_on": {
"type": "date",
"format": "dateOptionalTime"
}
}
}
}
}'


# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"type"}}
{"name":"foo","created_at":"2013-12-09T00:00:00.000Z","happens_on":"2013-12-16T00:00:00.000Z"}
{"index":{"_index":"play","_type":"type"}}
{"name":"bar","created_at":"2013-12-08T00:00:00.000Z","happens_on":"2013-12-16T00:00:00.000Z"}
{"index":{"_index":"play","_type":"type"}}
{"name":"bar","created_at":"2013-12-09T00:00:00.000Z","happens_on":"2013-12-17T00:00:00.000Z"}
'

# Do searches

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"query": {
"bool": {
"must": [
{
"range": {
"created_at": {
"gte": "2013-12-09T00:00:00.000Z"
}
}
},
{
"range": {
"happens_on": {
"lte": "2013-12-16T00:00:00.000Z"
}
}
}
]
}
}
}
'

关于elasticsearch - 在 elasticsearch 中查询多个日期的多个范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20610999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com