术语,嵌套文档和must_not查询在ElasticSearch中不兼容? [英] Term, nested documents and must_not query incompatible in ElasticSearch?

查看:550
本文介绍了术语,嵌套文档和must_not查询在ElasticSearch中不兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难组合术语,对嵌套文档的must_not查询.

I have trouble combining term, must_not queries on nested documents.

可以在此处找到感官示例: http://sense.qbox.io/gist/be436a1ffa01e4630a964f48b2d5 /a>

Sense example can be found here : http://sense.qbox.io/gist/be436a1ffa01e4630a964f48b2d5b3a1ef5fa176

这是我的映射:

{
    "mappings": {
        "docs" : {
            "properties": {
                "tags" : {
                    "type": "nested",
                    "properties" : {
                        "type": {
                           "type": "string",
                           "index": "not_analyzed"
                        }
                    }
                },
                "label" : {
                    "type": "string"
                }
            }
        }
    }
}

在此索引中有两个文档:

with two documents in this index :

{
    "tags" : [
        {"type" : "POST"},
        {"type" : "DELETE"}
    ],
    "label" : "item 1"
},
{
    "tags" : [
        {"type" : "POST"}
    ],
    "label" : "item 2"
}

当我像这样查询该索引时:

When I query this index like this :

{
  "query": {
    "nested": {
      "path": "tags",
      "query": {
        "bool": {
          "must": {
            "term": {
              "tags.type": "DELETE"
            }
          }
        }
      }
    }
  }
}

我一击(正确)

当我想获取不包含标签"DELETE"的文档时,请使用以下查询:

When I want to get documents WHICH DON'T CONTAIN the tag "DELETE", with this query :

{
  "query": {
    "nested": {
      "path": "tags",
      "query": {
        "bool": {
          "must_not": {
            "term": {
              "tags.type": "delete"
            }
          }
        }
      }
    }
  }
}

我有2次匹配(不正确). 这个问题似乎非常接近这个问题( Elasticsearch数组必须和must_not ),但是不是...

I've got 2 hits (which is incorrect). This issue seems very close to this one (Elasticsearch array must and must_not) but it's not...

您能给我一些解决此问题的线索吗?

Can you give me some clues to resolve this issue ?

谢谢

推荐答案

这应该可以解决您的问题: http: //sense.qbox.io/gist/f4694f542bc76c29624b5b5c9b3ecdee36f7e3ea

This should fix your problem: http://sense.qbox.io/gist/f4694f542bc76c29624b5b5c9b3ecdee36f7e3ea

两个最重要的事情:

  1. include_in_root放在"tags.type"上.这将告诉ES将标签类型索引为"doc.tags.types" : ['DELETE', 'POSTS'],因此您可以在根doc上访问扁平化"的那些值的数组.这意味着您不再需要嵌套查询(请参阅#2)

  1. include_in_root on "tags.type". This will tell ES to index tag types as "doc.tags.types" : ['DELETE', 'POSTS'], so you can access an array of those values "flattened" on the root doc . This means you no longer need a nested query (see #2)

删除嵌套查询.

 

{
    "mappings": {
        "docs" : {
            "properties": {
                "tags" : {
                    "type": "nested",
                    "properties" : {
                        "type": {
                           "type": "string",
                           "index": "not_analyzed"
                        }
                    },
                    "include_in_root": true
                },
                "label" : {
                    "type": "string"
                }
            }
        }
    }
}

 

{
   "query": {
      "bool": {
         "must_not": {
            "term": {
               "tags.type": "DELETE"
            }
         }
      }
   }
}

这篇关于术语,嵌套文档和must_not查询在ElasticSearch中不兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆