简体   繁体   English

如何搜索特定字段包含一个列表中的所有项目但不包含另一个列表中的任何项目的文档?

[英]How to search for documents whose specific field contains all items from one list and does not contain any item from another list?

I want to get documents whose specific field contains all items from one list and does not contain any item from another list.我想获取其特定字段包含一个列表中的所有项目并且不包含另一个列表中的任何项目的文档。 For example:例如:

The document may look something like this:该文件可能看起来像这样:

{
          "name" : "text",
          "number" : "1.0",
          "price" : "3.99"
}

I have a list of strings, which have to be in the field "name".我有一个字符串列表,必须在“名称”字段中。

has_to_be = [element1, element2, ...]

And I have a list of strings which cannot be in the field "name".我有一个字符串列表,不能在“名称”字段中。

cannot_be = [element1, element2, ...]

I want to get the documents, whose field "name" consists all strings from has_to_be and does not consist any word from cannot_be.我想获取文档,其字段“name”包含 has_to_be 中的所有字符串,并且不包含 cannot_be 中的任何单词。

I tried something like this:我试过这样的事情:

"query": 
{"bool": 
{"must": [
{"match": {"name": "element1"}}, 
{"match": {"name": "element2"}}
]}}

But I didn't get any hits.但我没有得到任何命中。

I tried also this query:我也试过这个查询:

GET /index_name/_search?q=name:+element1 +element2

(Additional question: How can I send such a query from python to elasticsearch?) (附加问题:如何将这样的查询从python发送到elasticsearch?)

But I get also hits, which didn't contain all the elements (element1 or element2).但我也得到了命中,其中不包含所有元素(element1 或 element2)。

About your filter needs, you can do what you need with a boolean query and specify the matching operator.关于您的过滤器需求,您可以使用 boolean 查询并指定匹配运算符来执行您需要的操作。

I used some test documents:我使用了一些测试文件:

POST test/_doc
{
  "elt": ["test1", "test2", "test3"]
}

POST test/_doc
{
  "elt": ["test1", "test2", "test3", "test4"]
}

POST test/_doc
{
  "elt": ["test1", "test2"]
}

And below and example query where document must contains test1 AND test2 AND test3 but NOT test 4 .下面是示例查询,其中文档必须包含test1 AND test2 AND test3 but NOT test 4 So the first document will be the only one to match.所以第一个文档将是唯一匹配的文档。

GET test/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "match": {
            "elt": {
              "query": "test1 test2 test3",
              "operator": "AND"
            }
          }
        }
      ],
      "must_not": [
        {
          "match": {
           "elt": {
              "query": "test4 test5",
              "operator": "OR"
            }
          }
        }
      ]
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从包含另一个列表中的所有特定字符的列表中获取项目 - How to get items from a list that contain all specific characters from another one 如果项目包含另一个列表中的任何字符串,则从列表中获取项目(Python) - Get items from list if the item contains any of the strings in another list (Python) 如何从列表中提取值包含项的对应键? - How to extract the corresponding key whose value contains the item from a list? 检查列表是否包含另一个列表中的所有项目 - Checking if List contains all items from another list 将一个列表中的项目添加到另一列表中的特定项目 - Adding items from one list to specific items of another list Python检查列表项是否包含任何其他列表项 - Python Check if list item does (not) contain any of other list items 从数据框中删除文本不包含列表项的行 - Remove rows from dataframe whose text does not contain items from a list 从列表中删除包含其他项目的所有项目 - Python - Remove (from list) all the items which contains other item - Python 将生成的组合中的项目从一个列表重新定义为其项目位置链接的另一个列表的术语 - Redefine items in generated combinations from one list into terms of another list whose items are positionally linked 将一个列表中的项目添加到另一个列表中的 n 个项目 - add item from one list to n number of items to another list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM