弹性搜索bool查询组合必须与OR [英] elasticsearch bool query combine must with OR
问题描述
我有这个lucene查询
((
name:(+ foo + bar)
OR info:(+ foo + bar)
))AND状态:(1) AND(has_image:(0)OR has_image:(1)^ 100)
据我所知这是MUST子句与布尔OR的组合OR:
获取包含所有文档(名称中的foo AND栏)OR(信息中的foo AND栏)之后根据条件状态= 1过滤结果,并提升具有图像的文档。
我一直在尝试使用一个布尔查询,但我没有得到布尔或成为必须条款。这是我有的:
GET / test / object / _search
{
from: 0,
size:20,
sort:{
_score:desc
},
查询:{
bool:{
must:[
{
match:{
name:foo
}
} ,
{
match:{
name:bar
}
}
],
must_not []
应该:[
{
match:{
has_image:{
query:1,
提升:100
}
}
}
]
}
}
}
正如你所看到的,info的条件将丢失。
一个解决方案?
非常感谢你。
**更新**
我已经更新了我的弹性搜索查询并删除了的功能得分。我的基础问题仍然存在。
我终于设法创建了一个完全符合我想要的查询:
一个过滤的嵌套布尔查询。
我不知道为什么没有记录。也许有人可以告诉我?
这是查询:
$$$$$$$$$$$$$ desc
},
query:{
filtered:{
filter:{
bool:{
必须:[
{
term:{
state:1
}
}
]
}
$,
查询:{
bool:{
should:[
{
bool:{
:[
{
match:{
name:foo
}
},
{
:{
name:bar
}
}
],
should:[
{
match:{
has_image:{
query:1,
boost:100
}
}
]
}
},
{
bool:{
必须 [
{
match:{
info:foo
}
},
{
match {
info:bar
}
}
],
应该:[
{
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。
minimum_should_match:1说,至少有一个should语句必须为true。
这个语句意味着每当结果集中包含has_image:1的文档时,它被提升为因子100.这会改变结果排序。
should:[
{
match:{
has_image:{
query
boost:100
}
}
}
]
有乐趣的人:)
I am currently trying to migrate a solr-based application to elasticsearch.
I have this lucene query
((
name:(+foo +bar)
OR info:(+foo +bar)
)) AND state:(1) AND (has_image:(0) OR has_image:(1)^100)
As far as I understand this is a combination of MUST clauses combined with boolean OR:
"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."
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
}
}
}
]
}
}
}
As you can see, MUST conditions for "info" are missing.
Does anyone have a solution?
Thank you so much.
** UPDATE **
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?
Here is the query:
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
}
}
}
}
}
In pseudo-SQL:
SELECT * FROM /test/object
WHERE
((name=foo AND name=bar) OR (info=foo AND info=bar))
AND state=1
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 says, that at least one of the should statements must be true.
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
}
}
}
]
Have fun guys :)
这篇关于弹性搜索bool查询组合必须与OR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!