[英]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.