我在弹性搜索中有一个奇怪的行为。在我开始使用过滤器之前,我有一个查询:
不使用FILTER查询
{
"query": {
"bool": {
"must": [
{
"match": {
"_facility": {
"query": "error",
"operator": "AND"
}
}
},
{
"match": {
"_application": {
"query": "live",
"operator": "AND"
}
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10,
"sort": [
{
"created_at": {
"sort_mode": null,
"order": "desc",
"missing": null,
"ignore_unmapped": null
}
},
{
"_score": {
"sort_mode": null,
"order": null,
"missing": null,
"ignore_unmapped": null
}
}
]
}
然后我必须添加FILTER
{
"query": {
"bool": {
"must": [
{
"match": {
"_facility": {
"query": "error",
"operator": "AND"
}
}
},
{
"match": {
"_application": {
"query": "live",
"operator": "AND"
}
}
}
],
"must_not": [],
"should": []
}
},
"filter": {
"and": {
"filters": [
{
"range": {
"created_at": {
"from": 1373320800,
"to": 1373493599,
"include_lower": true,
"include_upper": true
},
"_cache": true
}
}
]
},
"_cache": false
},
"from": 0,
"size": 10,
"sort": [
{
"created_at": {
"sort_mode": null,
"order": "desc",
"missing": null,
"ignore_unmapped": null
}
},
{
"_score": {
"sort_mode": null,
"order": null,
"missing": null,
"ignore_unmapped": null
}
}
]
}
我还有下一个问题:
1)结果未按created_at正确排序,看起来像混洗数据
2)尺寸 - 不考虑任何与10不同的自定义值(假设我想显示20 [或5]条记录,但我有10条)
感谢您的帮助。可能我在弹性搜索概念中遗漏了一些东西。
答案 0 :(得分:1)
问题是"_cache": false
过滤器中的and
实际上在过滤器之外。它应该更深一层:
"filter": {
"and": {
"filters": [{
"range": {
"created_at": {
"from": 1373320800,
"to": 1373493599,
"include_lower": true,
"include_upper": true
},
"_cache": true
}
}],
"_cache": false
}
}
它抛弃了Elasticsearch解析器,它开始忽略查询的其余部分。顺便说一句,这些_cache
语句是无用的,因为它们只是确保默认设置。正如@Damien所说,整个请求会更好filtered
查询:
{
"query": {
"filtered": {
"query": {
"bool": {
"must": [{
"match": {
"_facility": {
"query": "error",
"operator": "AND"
}
}
}, {
"match": {
"_application": {
"query": "live",
"operator": "AND"
}
}
}]
}
},
"filter": {
"and": {
"filters": [{
"range": {
"created_at": {
"from": 1373320800,
"to": 1373493599,
"include_lower": true,
"include_upper": true
},
"_cache": true
}
}, {
"match_all": {}
}],
"_cache": false
}
}
}
},
"from": 0,
"size": 10,
"sort": [{
"created_at": {
"order": "desc"
}
}, {
"_score": {}
}]
}