我需要从搜索中排除不同术语的关联。有什么聪明的方法可以进行这种多项排斥吗?
示例:说我想让所有用户(其姓氏为Doe)为和(名字为John或Annie或没有任何名字值)的用户。
预期结果示例:
first_name | last_name | Search result
--------------------------------------
Bob | Doe | appears
--------------------------------------
Annie | Doe | excluded
--------------------------------------
(null) | Doe | excluded
--------------------------------------
John | Foo | appears
--------------------------------------
(null) | Foo | appears
到目前为止,我所做的最好的事情是以下请求,但它不起作用:在我们的示例中,此请求将排除姓氏为Doe的所有人,无论名字是什么……我都无法理解为什么?
GET user/_search
{
"query": {
"bool": {
"must_not": [
{
"bool": {
"must": [
{
"term": {
"last_name": "Doe"
}
}
]
}
},
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"terms": {
"first_name": [
"John",
"Annie"
]
}
}
]
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "first_name"
}
}
}
}
]
}
}
]
}
}
}
对此有任何帮助,我们将不胜感激!
最佳答案
在must_not数组中,文档不能与任何子句匹配。
如果last_name是Doe OR(first_name是annie或john或不存在),则查询基本上会转换为不考虑文档
用作AND并将您的查询放入bool
{
"query": {
"bool": {
"must_not": [
{
"bool": {
"must": [
{
"match": {
"last_name": "Doe"
}
},
{
"bool": {
"should": [
{
"terms": {
"first_name.keyword": [
"John",
"Annie"
]
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "first_name"
}
}
}
}
]
}
}
]
}
}
]
}
}
}
关于elasticsearch - Elasticsearch排除多个术语的关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60556348/