ElasticSearch布尔查询必须与OR组合

au9on6nz  于 2022-10-06  发布在  ElasticSearch
关注(0)|答案(3)|浏览(186)

我目前正在尝试将一个基于Solr的应用程序迁移到ElasticSearch。

我有一个Lucene查询:

(( 
    name:(+foo +bar) 
    OR info:(+foo +bar) 
)) AND state:(1) AND (has_image:(0) OR has_image:(1)^100)

据我所知,这是must子句和布尔e1d1e的组合:

获取包含(foo AND bar in name) OR (foo AND bar in info)的所有文档。之后,根据条件state=1对结果进行过滤,并对包含图像的文档进行Boost。

我一直在尝试对must使用布尔查询,但未能将布尔值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
            }
          }
        }
      ]
    }
  }
}

如您所见,缺少infomust条件。

更新

我已经更新了我的ElasticSearch查询,并删除了该函数分数。我的基本问题仍然存在。

7fhtutme

7fhtutme1#

  • 拼写为应该**
  • 拼写为必须**
  • Nor拼写为Shout_Not**

示例:

您希望查看以下所有项目(圆形和(红色或蓝色)):

{
        "query": {
            "bool": {
                "must": [
                    {
                        "term": {"shape": "round"}
                    },
                    {
                        "bool": {
                            "should": [
                                {"term": {"color": "red"}},
                                {"term": {"color": "blue"}}
                            ]
                        }
                    }
                ]
            }
        }
    }

您还可以进行更复杂的OR运算,例如,如果您想要匹配5个中的至少3个,则可以在“应该”下指定5个选项,并将“Minimum_Share”设置为3。

多亏了格伦·汤普森和塞巴斯蒂亚隆索,他们发现了我以前筑巢的地方不太对。

还要感谢Fatmank指出,在ElasticSearch版本6中,“Term”变成了“Match”。

r3i60tvu

r3i60tvu2#

我最终成功地创建了一个查询,它执行的正是我想要的:

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

以下是查询:

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

请记住,这取决于您的文档字段分析和Map如何在内部处理name=foo。这可能是模糊的行为,也可能是严格的行为。

“MINIMUM_HASH_MATCH”:1表示至少有一条应该语句必须为真。

这条语句意味着,只要结果集中有包含HAS_IMAGE:1的文档,它就会因数100而增加。这会更改结果顺序。

"should": [
  {
    "match": {
      "has_image": {
        "query": 1,
        "boost": 100
      }
    }
   }
 ]

祝你们玩得开心:)

6uxekuva

6uxekuva3#

这就是如何使用Kibana将多个布尔查询嵌套在一个外部布尔查询中,

  • bool**表示我们使用的是Boolean
  • 必须用于和**
  • 应该是用于或**
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.过滤器
1.不得

相关问题