如何在弹性搜索中正确写入布尔或逻辑? [英] How to properly write boolean or logic in elasticsearch?
问题描述
我想用 loId = 6
AND (actionType =SAVE_DATA
OR actionType =OPEN_SCREEN)
。
我写这个逻辑不正确?
为什么我的查询返回0个结果?
注意:我接受查询或过滤器来解决此问题。
Note: I'd accept a query or a filter to resolve this issue.
这些是一些示例文档: / p>
These are some sample documents:
{
"_index": "logs",
"_type": "record",
"_id": "eIIt3vtrSxmdOVGClQmN3w",
"_score": 1,
"_source": {
"timestamp": 1373569919000,
"uid": 6,
"paId": 56298,
"loId": 6,
"prId": 2,
"vId": 6577,
"actionType": "SAVE_DATA"
}
},
{
"_index": "logs",
"_type": "record",
"_id": "yQGCia6qRYCImZLyH7DrEA",
"_score": 1,
"_source": {
"timestamp": 1373570314000,
"uid": 6,
"paId": 56641,
"loId": 6,
"prId": 2,
"vId": 6578,
"actionType": "CHECK_IN"
}
},
{
"_index": "logs",
"_type": "record",
"_id": "2raajrNKTKeRKG88wiNPpw",
"_score": 1,
"_source": {
"timestamp": 1373570532000,
"uid": 6,
"paId": 56641,
"loId": 6,
"prId": 2,
"vId": 6579,
"actionType": "LOAD_DATA"
}
},
{
"_index": "logs",
"_type": "record",
"_id": "0FDBxW2ESl6tfmj81YJvIg",
"_score": 1,
"_source": {
"timestamp": 1373570761000,
"uid": 6,
"paId": 56298,
"loId": 6,
"prId": 2,
"vId": 6577,
"actionType": "OPEN_SCREEN"
}
},
{
"_index": "logs",
"_type": "record",
"_id": "-bFQyEdCQVWiXNldtxz04g",
"_score": 1,
"_source": {
"timestamp": 1373570777000,
"uid": 6,
"paId": 56298,
"loId": 6,
"prId": 2,
"vId": 6577,
"actionType": "OPEN_SCREEN"
}
}
查询
{
"query": {
"filtered": {
"query": {
"term": {
"loId": 6
}
},
"filter": {
"or": {
"filters": [
{
"term": {
"actionType": "SAVE_DATA"
}
},
{
"term": {
"actionType": "OPEN_SCREEN"
}
}
],
"_cache": false
}
}
}
}
}
推荐答案
您无法获取结果的主要原因是因为您尝试执行 term
filter on actionType
,这是一个分析字段。如果您想在该字段上执行术语
匹配,则需要更新类型映射以将该字段设置为 not_analyzed
。参见此示例映射:
The main reason you're having trouble getting results is because you're trying to do a term
filter on actionType
, which is an analyzed field. If you want to do term
matching on that field, you'll need to update your type mapping to set that field to not_analyzed
. See this example mapping:
{
"record": {
"properties": {
"actionType": {
"type": "string",
"index": "not_analyzed",
},
"loId": {
"type": "long"
},
"paId": {
"type": "long"
},
"prId": {
"type": "long"
},
"timestamp": {
"type": "long"
},
"uid": {
"type": "long"
},
"vId": {
"type": "long"
}
}
}
}
从这里开始读取映射:http://www.elasticsearch.org/guide/reference/mapping/ 。您需要重新索引您的数据。有了这个修正,这里有一个查询可以工作:
Read up on mappings starting here: http://www.elasticsearch.org/guide/reference/mapping/. You'll need to reindex your data. With that fixed, here's a query that will work:
{
"query": {
"filtered": {
"query": {"match_all":{}},
"filter": {
"bool": {
"must": [
{"term": {"loId":6}},
{
"or": [
{"term":{"actionType": "SAVE_DATA"}},
{"term":{"actionType": "OPEN_SCREEN"}}
]
}
]
}
}
}
}
}
这篇关于如何在弹性搜索中正确写入布尔或逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!