首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >elasticsearch bool查询必须与OR结合

elasticsearch bool查询必须与OR结合
EN

Stack Overflow用户
提问于 2015-02-16 09:59:13
回答 6查看 253.7K关注 0票数 289

我目前正在尝试将基于solr的应用程序迁移到elasticsearch。

我有一个lucene查询:

代码语言:javascript
运行
复制
(( 
    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子句。以下是我所拥有的:

代码语言:javascript
运行
复制
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
            }
          }
        }
      ]
    }
  }
}

如您所见,mustinfo条件缺失了。

**更新**

我更新了我的elasticsearch查询,并去掉了这个功能分数。我的基本问题仍然存在。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2015-02-16 14:22:24

最后,我成功地创建了一个查询,它完全实现了我想要的结果:

筛选的嵌套布尔查询。我不知道为什么没有记录在案。也许这里有人能告诉我?

以下是查询:

代码语言:javascript
运行
复制
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中:

代码语言:javascript
运行
复制
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所推动。这会改变结果排序。

代码语言:javascript
运行
复制
"should": [
  {
    "match": {
      "has_image": {
        "query": 1,
        "boost": 100
      }
    }
   }
 ]

玩得开心的人:)

票数 95
EN

Stack Overflow用户

发布于 2016-11-23 04:14:30

  • 拼写为OR
  • 拼写为 is
  • 拼写为should_not

示例:

您希望看到(圆形和(红色或蓝色)的所有项目):

代码语言:javascript
运行
复制
    {
        "query": {
            "bool": {
                "must": [
                    {
                        "term": {"shape": "round"}
                    },
                    {
                        "bool": {
                            "should": [
                                {"term": {"color": "red"}},
                                {"term": {"color": "blue"}}
                            ]
                        }
                    }
                ]
            }
        }
    }

您还可以执行更复杂的OR版本,例如,如果要匹配至少3 / 5,则可以在“应该”下指定5个选项,并将"minimum_should“设置为3。

感谢格伦汤普森和塞巴斯提亚隆索发现我的巢不太对以前。

还感谢Fatmajk指出,在ElasticSearch版本6中,“术语”变成了“匹配”。

票数 753
EN

Stack Overflow用户

发布于 2019-06-01 16:35:02

这就是如何使用Kibana在一个外部bool查询中嵌套多个bool查询的方法,

  • bool表示我们使用布尔值
  • 应为
代码语言:javascript
运行
复制
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“里有更多的类型,

  1. 过滤器
  2. must_not
票数 49
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28538760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档