[英]Elasticsearch exclude association of multiple terms
我需要从我的搜索中排除不同术语的关联。 有没有什么聪明的方法来做这种多词排除?
示例:假设我想获取所有用户,除了那些(姓氏为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"
}
}
}
}
]
}
}
]
}
}
}
Elastic-sharp-eye 对此的任何帮助将不胜感激!
在 must_not 数组文档中不得匹配任何子句。 如果 last_name 是 Doe OR(first_name 是 annie 或 john 或不存在),您的查询基本上会转化为不考虑文档
用作并将您的查询放在 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"
}
}
}
}
]
}
}
]
}
}
]
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.