Elasticsearch 数组仅包含查询
假设我有这种格式的数据:
{
"id": "doc0",
"tags": ["a", "b", "c"]
}
{
"id": "doc1",
"tags": ["a", "b", "c", "d"]
}
{
"id": "doc2",
"tags": ["a", "b"]
}
我需要形成一个 ES 查询,该查询仅获取包含“a”、“b”且不包含其他内容的文档。
如果我编写一个术语查询,它会匹配所有文档,因为所有文档都有“a”和“b”,但只有一个文档除了“a”和“b”之外没有其他任何内容,
形成此查询的最佳方式是什么?我没有添加“not_contains”子句的其他值的列表。
Let's say I've data in this format:
{
"id": "doc0",
"tags": ["a", "b", "c"]
}
{
"id": "doc1",
"tags": ["a", "b", "c", "d"]
}
{
"id": "doc2",
"tags": ["a", "b"]
}
I need to form an ES query that fetches only documents that contains both "a", "b" and nothing else.
If I write a terms query, it matches all the documents, as all documents have both "a" and "b" but only one document has nothing else apart from "a" and "b"
What is the best way to form this query? I don't have the list of the other values to add "not_contains" clause.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两种方法可以实现您的结果:
must
和filter
子句)和脚本查询,仅检索同时具有“a ”和“b”。索引数据:
搜索查询:
搜索结果:
minimum_should_match_script
参数查询。与脚本查询相比,术语集查询会更快。There are two ways in which you can achieve your result :
must
andfilter
clause) and script query to retrieve only those documents that have both "a" and "b".Index Data:
Search Query:
Search Result:
minimum_should_match_script
param with terms set query. When compared to a script query, Terms set query will be faster.您可以使用 条款集< /a> 查询。
在使用团队集查询之前,您需要使用一个字段中的元素计数更新索引文档。
查询:
结果:
You can use Terms Set query.
Before using teams set query, you need to update your index document with number of elements count in one field.
Query:
Result: