我目前正在尝试将基于solr的应用程序迁移到elasticsearch。
我有一个lucene查询:
((
name:(+foo +bar)
OR info:(+foo +bar)
)) AND state:(1) AND (has_image:(0) OR has_image:(1)^100)据我所知,这是must子句与布尔OR的组合。
获取所有包含
(foo AND bar in name) OR (foo AND bar in info)的文档。在此之后,筛选结果由具有图像的条件state=1和boost文档生成。
我一直试图在must中使用bool查询,但未能将布尔OR引入must子句。以下是我所拥有的:
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的info条件缺失了。
**更新**
我更新了我的elasticsearch查询,并去掉了这个功能分数。我的基本问题仍然存在。
发布于 2015-02-16 14:22:24
最后,我成功地创建了一个查询,它完全实现了我想要的结果:
筛选的嵌套布尔查询。我不知道为什么没有记录在案。也许这里有人能告诉我?
以下是查询:
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。这可以从模糊行为到严格行为不等。
"minimum_should_match":1说,至少应该有一个语句是正确的。
这个语句意味着,每当结果集中有包含has_image:1的文档时,它就会被因子100所推动。这会改变结果排序。
"should": [
{
"match": {
"has_image": {
"query": 1,
"boost": 100
}
}
}
]玩得开心的人:)
发布于 2016-11-23 04:14:30
示例:
您希望看到(圆形和(红色或蓝色)的所有项目):
{
"query": {
"bool": {
"must": [
{
"term": {"shape": "round"}
},
{
"bool": {
"should": [
{"term": {"color": "red"}},
{"term": {"color": "blue"}}
]
}
}
]
}
}
}您还可以执行更复杂的OR版本,例如,如果要匹配至少3 / 5,则可以在“应该”下指定5个选项,并将"minimum_should“设置为3。
感谢格伦汤普森和塞巴斯提亚隆索发现我的巢不太对以前。
还感谢Fatmajk指出,在ElasticSearch版本6中,“术语”变成了“匹配”。
发布于 2019-06-01 16:35:02
这就是如何使用Kibana在一个外部bool查询中嵌套多个bool查询的方法,
GET my_inedx/my_type/_search
{
"query" : {
"bool": { //bool indicates we are using boolean operator
"must" : [ //must is for **AND**
{
"match" : {
"description" : "some text"
}
},
{
"match" :{
"type" : "some Type"
}
},
{
"bool" : { //here its a nested boolean query
"should" : [ //should is for **OR**
{
"match" : {
//ur query
}
},
{
"match" : {}
}
]
}
}
]
}
}
}这是如何在ES中嵌套查询的方法。
在"bool“里有更多的类型,
https://stackoverflow.com/questions/28538760
复制相似问题