过滤ElasticSearch结果,其数组包含至少1个标记

时间:2015-02-28 09:00:15

标签: elasticsearch

我使用以下DSL查询elasticsearch。

{
   "query": {
      "filtered": {
         "query": {
            "multi_match": {
               "query": "Next",
               "type": "phrase_prefix",
               "fields": [
                  "defaultContent"
               ]
            }
         },
         "filter": {
            "bool": {
               "must_not": {
                  "term": {
                     "_deleted": true
                  }
               },
               "should": [
                  {
                     "term": {
                        "site": "xxx"
                     }
                  },
                  {
                     "term": {
                        "site": "base"
                     }
                  }
               ]
            }
         }
      }
   }
}

它起作用并返回1场比赛。

{
   "took": 42,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 2.733073,
      "hits": [
         {
            "_index": "cms",
            "_type": "content",
            "_id": "base>3453fm9lxkmyy_17",
            "_score": 2.733073,
            "_source": {
               "tags": [
                  "tag1",
                  "tag2"
               ],
               "site": "base",
               "_rev": "1-3b6eb2b3c3d5554bb3ef3f16a299160c",
               "defaultContent": "Next action to be settled",
               "_id": "base>3453fm9lxkmyy_17",
               "type": "content",
               "key": "3453fm9lxkmyy_17"
            }
         }
      ]
   }
}

现在我想修改DSL并添加新条件 - 仅返回标记包含tag1tag8

的条件
{
   "query": {
      "filtered": {
         "query": {
            "multi_match": {
               "query": "Next",
               "type": "phrase_prefix",
               "fields": [
                  "defaultContent"
               ]
            }
         },
         "filter": {
            "bool": {
               "must": {
                   "term" : {
                       "tags" : ["tag1", "tag8"],
                       "minimum_should_match" : 1
                   }
               },
               "must_not": {
                  "term": {
                     "_deleted": true
                  }
               },
               "should": [
                  {
                     "term": {
                        "site": "xxx"
                     }
                  },
                  {
                     "term": {
                        "site": "base"
                     }
                  }
               ]
            }
         }
      }
   }
}

然后,我一无所获。

{
   "took": 23,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
   }
}

我做错了吗?它应该返回1个匹配项,因为它包含tag1

1 个答案:

答案 0 :(得分:1)

当您想要匹配单个术语时,使用“术语”过滤器。有点像SQL column1 = 'foo'

您希望使用“terms”过滤器,它等同于SQL column1 IN ('foo', 'bar')