我目前正在尝试将基于 solr 的应用程序迁移到 elasticsearch.
I am currently trying to migrate a solr-based application to elasticsearch.
我有这个 lucene 查询
I have this lucene query
(( name:(+foo +bar) OR info:(+foo +bar) )) AND state:(1) AND (has_image:(0) OR has_image:(1)^100)据我所知,这是 MUST 子句与布尔 OR 的组合:
As far as I understand this is a combination of MUST clauses combined with boolean OR:
获取包含 (foo AND bar in name) OR (foo AND bar in info) 的所有文档.然后通过条件 state=1 过滤结果并提升具有图像的文档."
"Get all documents containing (foo AND bar in name) OR (foo AND bar in info). After that filter results by condition state=1 and boost documents that have an image."
我一直在尝试使用带有 MUST 的 bool 查询,但我无法将布尔 OR 放入 must 子句中.这是我所拥有的:
I have been trying to use a bool query with MUST but I am failing to get boolean OR into must clauses. Here is what I have:
GET /test/object/_search { "from": 0, "size": 20, "sort": { "_score": "desc" }, "query": { "bool": { "must": [ { "match": { "name": "foo" } }, { "match": { "name": "bar" } } ], "must_not": [], "should": [ { "match": { "has_image": { "query": 1, "boost": 100 } } } ] } } }如您所见,缺少信息"的 MUST 条件.
As you can see, MUST conditions for "info" are missing.
有人有解决办法吗?
非常感谢.
** 更新 **
我已经更新了我的 elasticsearch 查询并去掉了那个函数分数.我的基本问题仍然存在.
I have updated my elasticsearch query and got rid of that function score. My base problem still exists.
推荐答案我终于设法创建了一个完全符合我想要的查询:
I finally managed to create a query that does exactly what i wanted to have:
过滤后的嵌套布尔查询.我不确定为什么没有记录下来.也许这里有人可以告诉我?
A filtered nested boolean query. I am not sure why this is not documented. Maybe someone here can tell me?
这是查询:
GET /test/object/_search { "from": 0, "size": 20, "sort": { "_score": "desc" }, "query": { "filtered": { "filter": { "bool": { "must": [ { "term": { "state": 1 } } ] } }, "query": { "bool": { "should": [ { "bool": { "must": [ { "match": { "name": "foo" } }, { "match": { "name": "bar" } } ], "should": [ { "match": { "has_image": { "query": 1, "boost": 100 } } } ] } }, { "bool": { "must": [ { "match": { "info": "foo" } }, { "match": { "info": "bar" } } ], "should": [ { "match": { "has_image": { "query": 1, "boost": 100 } } } ] } } ], "minimum_should_match": 1 } } } } }在伪 SQL 中:
SELECT * FROM /test/object WHERE ((name=foo AND name=bar) OR (info=foo AND info=bar)) AND state=1请记住,这取决于您的文档字段分析和映射,如何在内部处理 name=foo.这可以从模糊到严格的行为.
Please keep in mind that it depends on your document field analysis and mappings how name=foo is internally handled. This can vary from a fuzzy to strict behavior.
"minimum_should_match": 1 表示,至少有一个 should 语句必须为真.
"minimum_should_match": 1 says, that at least one of the should statements must be true.
这个语句意味着只要结果集中有一个包含 has_image:1 的文档,它就会被提升 100 倍.这会改变结果排序.
This statements means that whenever there is a document in the resultset that contains has_image:1 it is boosted by factor 100. This changes result ordering.
"should": [ { "match": { "has_image": { "query": 1, "boost": 100 } } } ]祝大家玩得开心:)
更多推荐
elasticsearch bool 查询结合 must 与 OR
发布评论