Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
<img src="//i.stack.imgur.com/RUiNP.png" height="16" width="18" alt="" class="sponsor tag img">elasticsearch Elasticsearch数组必须且不能_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Filtering - Fatal编程技术网 elasticsearch Elasticsearch数组必须且不能,elasticsearch,filtering,elasticsearch,Filtering" /> elasticsearch Elasticsearch数组必须且不能,elasticsearch,filtering,elasticsearch,Filtering" />

elasticsearch Elasticsearch数组必须且不能

elasticsearch Elasticsearch数组必须且不能,elasticsearch,filtering,elasticsearch,Filtering,我的elasticsearch数据库中有一个类似这样的文档: { "tags" => [ "tag-1", "tag-2", "tag-3", "tag-A" ] "created_at" =>"2013-07-02 12:42:19 UTC", "label" =>"Mon super label" } 我希望能够使用以下条件筛选我的文档: 文档标记数组必须有标记-1、标记-3和标记

我的elasticsearch数据库中有一个类似这样的文档:

{
   "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


我试着使用一个布尔过滤器,但我无法使它工作

这里有一种方法似乎可以满足您的需要:

首先,我创建了一个带有显式映射的索引。我这样做是为了将
“tags”
属性设置为
“index”:“not_analysis”
。这意味着不会以任何方式修改文本,这将简化本例的查询过程

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"
}'
然后我们可以使用
bool
过滤器中的
must
must\u not
子句进行查询,如下所示:

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"
            }
         }
      ]
   }
}
请注意
must
子句中
术语
过滤器中的
执行“:”和“
参数。这意味着只有指定了所有
“标记”
的文档才会被返回(而不是那些匹配一个或多个标记的文档)。那可能就是你所错过的。您可以阅读中有关选项的更多信息

我制作了一个可运行的示例,如果您安装了ES并在
localhost:9200
上运行,或者您可以提供自己的端点,则可以使用该示例