Elasticsearch数组必须和must_not [英] Elasticsearch array must and must_not

查看:1242
本文介绍了Elasticsearch数组必须和must_not的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的弹性搜索DB中有一个这样的文件:

I have a documents looking like this in my elasticsearch DB :

{
   "tags"   =>   [
      "tag-1",
      "tag-2",
      "tag-3",
      "tag-A"
   ]
   "created_at"   =>"2013-07-02 12:42:19   UTC",
   "label"   =>"Mon super label"
}

我想使用以下条件过滤我的文档:
文档标签数组必须有标签-1,标签-3和标签-2,但不能有标签-A。

I would like to be able to filter my documents with this criteria : Documents tags array must have tags-1, tags-3 and tags-2 but must not have tags-A.

我尝试使用布尔过滤器,但我无法设法使其工作!

I tried to use a bool filter but I can't manage to make it work !

推荐答案

这是一个似乎完成你想要的方法: http ://sense.qbox.io/gist/4dd806936f12a9668d61ce63f39cb2c284512443

Here is a method that seems to accomplish you want: http://sense.qbox.io/gist/4dd806936f12a9668d61ce63f39cb2c284512443

首先我创建了一个具有显式映射的索引。我这样做,所以我可以将标签属性设置为index:not_analyzed。这意味着文本不会以任何方式进行修改,这将简化此示例的查询过程。

First I created an index with an explicit mapping. I did this so I could set the "tags" property to "index": "not_analyzed". This means that the text will not be modified in any way, which will simplify the querying process for this example.

curl -XPUT "http://localhost:9200/test_index" -d'
{
    "mappings": {
        "docs" : {
            "properties": {
                "tags" : {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "label" : {
                    "type": "string"
                }
            }
        }
    }
}'

然后添加一些文档:

curl -XPUT "http://localhost:9200/test_index/docs/1" -d'
{
    "tags" : [
        "tag-1",
        "tag-2",
        "tag-3",
        "tag-A"
    ],
    "label" : "item 1"
}'
curl -XPUT "http://localhost:9200/test_index/docs/2" -d'
{
    "tags" : [
        "tag-1",
        "tag-2",
        "tag-3"
    ],
    "label" : "item 2"
}'
curl -XPUT "http://localhost:9200/test_index/docs/3" -d'
{
    "tags" : [
        "tag-1",
        "tag-2"
    ],
    "label" : "item 3"
}'

然后,我们可以使用中和 must_not > bool 过滤器如下:

Then we can query using must and must_not clauses in a bool filter as follows:

curl -XPOST "http://localhost:9200/test_index/_search" -d'
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "bool": {
               "must": [
                  {
                     "terms": {
                        "tags": [
                           "tag-1",
                           "tag-2",
                           "tag-3"
                        ],
                        "execution" : "and"
                     }
                  }
               ],
               "must_not": [
                  {
                      "term": {
                         "tags": "tag-A"
                      }
                  }
               ]
            }
         }
      }
   }
}'

可以产生正确的结果:

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "docs",
            "_id": "2",
            "_score": 1,
            "_source": {
               "tags": [
                  "tag-1",
                  "tag-2",
                  "tag-3"
               ],
               "label": "item 2"
            }
         }
      ]
   }
}

注意执行:和子句中的术语过滤器中的参数。这意味着只有具有指定的所有标签/ code的文档将被返回(而不是匹配一个或多个)。这可能是你失踪了您可以在 ES文档。

我创建了一个可运行的示例这里,你可以玩,如果您有ES安装并运行在 localhost:9200 ,或者您可以提供您自己的端点。

I made a runnable example here that you can play with, if you have ES installed and running at localhost:9200, or you can provide your own endpoint.

这篇关于Elasticsearch数组必须和must_not的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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