使用 elasticsearch 精确搜索数组对象类型 [英] Exact search in array object type using elasticsearch
问题描述
我正在寻找一种在弹性搜索中进行 精确 数组匹配的方法.假设这些是我的文件:
I'm looking for a way to do exact array matches in elastic search. Let's say these are my documents:
{"id": 1, "categories" : ["c", "d"]}
{"id": 2, "categories" : ["b", "c", "d"]}
{"id": 3, "categories" : ["c", "d", "e"]}
{"id": 4, "categories" : ["d"]}
{"id": 5, "categories" : ["c", "d"]}
有没有办法搜索所有完全类别为c"和d"(文档 1 和 5)的文档,不多也不少?
Is there a way to search for all document's that have exactly the categories "c" and "d" (documents 1 and 5), no more or less?
作为奖励:搜索其中一个"类别应该仍然是可能的(例如,您可以搜索c"并获得 1、2、3 和 5)
As a bonus: Searching for "one of these" categories should still be possible as well (for example you could search for "c" and get 1, 2, 3 and 5)
有什么巧妙的办法解决这个问题?
Any clever way to tackle this problem?
推荐答案
如果你有一组离散的、已知的类别,你可以使用 bool 查询:
If you have a discrete, known set of categories, you could use a bool query:
"bool" : {
"must" : {
"terms" : { "categories" : ["c", "d"],
minimum_should_match : 2
}
},
"must_not" : {
"terms" : { "categories" : ["a", "b", "e"],
minimum_should_match : 1
}
}
}
否则,我认为最简单的方法可能是存储另一个字段作为类别关键字.
Otherwise, Probably the easiest way to accomplish this, I think, is to store another field serving as a categories keyword.
{"id": 1, "categories" : ["c", "d"], "categorieskey" : "cd"}
类似的东西.然后您可以轻松地使用术语查询精确地查询您想要的结果,例如:
Something like that. Then you could easily query with a term query for precisely the results you want, like:
term { "categorieskey" : "cd" }
您仍然可以进行非排他性搜索,例如;
And you could still search non-exclusively, as;
term { "categories" : "c" }
查询必须同时存在的两个类别很容易,但阻止任何其他潜在类别存在则有点困难.你可以做到,也许.您可能想要编写一个查询来查找两者的记录,然后对其应用过滤器,以消除除指定类别之外的任何记录.据我所知,这并不是 Lucene 真正设计用来处理的一种搜索.
Querying for two categories that must both be present is easy enough, but then preventing any other potential categories from being present is a bit harder. You could do it, probably. You'dd probably want to write a query to find records with both, then apply a filter to it eliminating any records with categories other than the ones specified. It's not really a sort of search that Lucene is really designed to handle, to my knowledge.
老实说,我很难在这里找到一个好的过滤器.您可能需要一个脚本过滤器,或者您可以在检索到结果后对其进行过滤.
Honestly I'm having a bit of trouble coming up with a good filter to use here. You might need a script filter, or you could filter the results after they have been retrieved.
这篇关于使用 elasticsearch 精确搜索数组对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!