在Elasticsearch中在多个日期上具有多个范围的查询

以下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,并且源密钥中的所有内容实际上都是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

}

}

遗憾的是,我的问题仍然存在:我没有得到任何成功。

因此,沿着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查询,也许您在考虑and过滤器?

为了方便起见,将示例可运行播放作为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中在多个日期上具有多个范围的查询 的全部内容, 来源链接: utcz.com/qa/433738.html

回到顶部