仅具有与术语列表相交但不与其他术语相交的术语的Elasticsearch文档 [英] Elasticsearch documents that only have terms intersecting a list of terms but no other terms

查看:89
本文介绍了仅具有与术语列表相交但不与其他术语相交的术语的Elasticsearch文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些带有标签列表的文档:

I have documents that have a list of labels:



    {
       "fields": {
          "label": [
               "foo",
               "bar",
               "baz"
          ],
          "name": [
             "Document One"
          ],
          "description" : "A fine first document",
          "id" : 1
       }
    },
    {
       "fields": {
          "label": [
               "foo",
               "dog"
          ],
          "name": [
             "Document Two"
          ],
          "description" : "A fine second document",
          "id" : 2
       }
    }

我有一个术语列表:



    [ "foo", "bar", "qux", "zip", "baz"]

我想要一个查询,该查询将返回在术语列表中具有标签的文档-但没有其他术语。

I want a query that will return documents that have labels in the list of terms - but no other terms.

因此,鉴于上面的列表,查询将返回 Document One ,但不是 文档2 (因为它的术语不在术语列表中。

So given the list above, the query would return Document One, but not Document Two (because it has the term dog that is not in the list of terms.

我尝试使用 not 条款过滤器进行查询,如下所示:

I've tried doing a query using a not terms filter, like this:



    POST /documents/_search?size=1000
    {
       "fields": [
          "id",
          "name",
          "label"
       ],
       "filter": {
           "not": {
               "filter" : {
                   "bool" : {
                       "must_not": {
                          "terms": {
                             "label": [
                                "foo",
                                "bar",
                                "qux",
                                "zip",
                                "baz"
                             ]
                          }
                       }
                   }
               }
           }
       }
    }

但这没用。

如何创建一个查询,给定一个术语列表,该查询将匹配仅包含以下内容的文档列表中的术语,没有其他术语吗?换句话说,所有文档都应包含标签列表,这些标签是所提供术语列表的子集。

How can I create a query that, given a list of terms, will match documents that only contain terms in the list, and no other terms? In other words, all documents should contain a list of labels that are a subset of the list of supplied terms.

推荐答案

脚本过滤器,以检查数组项是否具有文档中标签数组的所有值。我建议您制作一个单独的常规文件或普通javascript文件,将其放在config / scripts / folderToYourScript中,并在过滤器中的查询中使用它:{script:{script_file:file}}

You can script filter to check if the array terms has all the values of label array in a document. I suggest you to make a separate groovy file or plain javascript file, put it in config/scripts/folderToYourScript, and use it in your query infilter: { script : {script_file: file } }

在脚本文件中,您可以使用循环检查要求

While in script file you can use loop to check the requirement

这篇关于仅具有与术语列表相交但不与其他术语相交的术语的Elasticsearch文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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