Elasticsearch多项式过滤器 [英] Elasticsearch multi term filter
问题描述
我是Elasticsearch的新手,所以这是我的问题。
我想使用elasticsearch进行搜索查询,并希望使用多个术语进行过滤。
I'm quite new to Elasticsearch, so here's my question. I wanna do a search query with elasticsearch and wanna filter with multiple terms.
如果我想搜索用户 tom,那么我想具有用户 isActive = 1, isPrivate = 0和 isOwner = 1的所有匹配项。
If I want to search for a user 'tom', then I would like to have all the matches where the user 'isActive = 1', 'isPrivate = 0' and 'isOwner = 1'.
这是我的搜索查询
"query":{
"filtered": {
"query": {
"query_string": {
"query":"*tom*",
"default_operator": "OR",
"fields": ["username"]
}
},
"filter": {
"term": {
"isActive": "1",
"isPrivate": "0",
"isOwner": "1"
}
}
}
}
当我使用2个术语时,它就像一个符咒,但是当我使用3个术语时,它不是。
When I use 2 terms, it works like a charm, but when i use 3 terms it doesn't.
感谢您的帮助!
推荐答案
您应使用布尔过滤器
到和
您的所有条款:
You should use bool filter
to AND
all your terms:
"query":{
"filtered": {
"query": {
"query_string": {
"query":"*tom*",
"default_operator": "OR",
"fields": ["username"]
}
},
"filter": {
"bool" : {
"must" : [
{"term" : { "isActive" : "1" } },
{"term" : { "isPrivate" : "0" } },
{"term" : { "isOwner" : "1" } }
]
}
}
}
}
对于2.x +版本,您可以使用 bool
查询代替过滤的
查询,并进行了一些简单的替换: https://www.elastic.co/guide/zh-CN/elasticsearch/reference/7.4/query-dsl-filtered-query.html
For version 2.x+ you can use bool
query instead of filtered
query with some simple replacement: https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl-filtered-query.html
这篇关于Elasticsearch多项式过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!