使用elasticsearch精确搜索数组对象类型 [英] Exact search in array object type using elasticsearch

查看:3980
本文介绍了使用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查询: p>

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屋!

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