我想实现一次对ES的搜索具有2个或更多条件的多个值。
例如,“customer”索引有 2 个字段 userid 和 order
。我使用下面的查询来搜索与这两个字段匹配的结果。
`{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": [
{
"terms": {
"userid": [
"1","2"
]
}
},
{
"terms": {
"order": [
"A","B"
]
}
}
]
}
}
}
}`
结果匹配满足所有组合的文档(如1&A、1&B、2&A、2&B
)。但我需要仅按照发送顺序匹配的结果(例如 1&A, 2&B
)。我们可以通过术语过滤器或任何其他替代方案来实现这一目标吗?
最佳答案
您始终可以使用嵌套的“and”
和“or”
:
POST /test_index/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"or": {
"filters": [
{ "and": [
{ "term": { "userid": { "value": "1" } } },
{ "term": { "order": { "value": "A" } } }
]},
{ "and": [
{ "term": { "userid": { "value": "2" } } },
{ "term": { "order": { "value": "B" } } }
]}
]
}
}
}
}
}
关于java - elasticsearch - 具有多个条件的术语过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28614805/