elasticsearch查询行为

j8ag8udp  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(2)|浏览(339)

我对elasticsearch有两个不同的查询。这两个查询的区别在于,第一个查询在一个boolean should查询中得到两个搜索条件,第二个查询将其拆分为两个单独的bool should查询。第一个返回预期的响应,但是第二个与任何文档都不匹配,即使存在同时包含这两个条件的文档。如果我重构第二个,使两个分开的bool should查询被bool should querye封装,它将返回预期的响应,就像查询1一样。
问题是为什么查询2不像1和3那样返回响应?我错过什么了吗?
编辑:提供的示例数据
编辑:我的问题解决了,这只是在代码中构建范围查询时的拼写错误,我无法识别-。-但是这里的答案的解释可能会帮助其他人。
1

GET _search
{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "range": {
                  "streetNr": {
                    "from": "1",
                    "to": "100",
                    "include_lower": true,
                    "include_upper": true,
                    "boost": 1
                  }
                }
              },
              {
                "match": {
                  "geographicAddress.city": {
                    "query": "Berlin"
                  }
                }
              }
            ],
            "minimum_should_match": "1"
          }
        }
      ]
    }
  }
}
GET _search
{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "range": {
                  "streetNr": {
                    "from": "1",
                    "to": "100",
                    "include_lower": true,
                    "include_upper": true,
                    "boost": 1
                  }
                }
              }
            ],
            "minimum_should_match": "1"
          }
        },
        {
          "bool": {
            "should": [
              {
                "match": {
                  "geographicAddress.city": {
                    "query": "Berlin"
                  }
                }
              }
            ],
            "minimum_should_match": "1"
          }
        }
      ]
    }
  }
}
GET _search
{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "should": [
                    {
                      "range": {
                        "streetNr": {
                          "from": "1",
                          "to": "100",
                          "include_lower": true,
                          "include_upper": true,
                          "boost": 1
                        }
                      }
                    }
                  ],
                  "minimum_should_match": "1"
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "match": {
                        "geographicAddress.city": {
                          "query": "Berlin"
                        }
                      }
                    }
                  ],
                  "minimum_should_match": "1"
                }
              }
            ],
            "minimum_should_match": "1"
          }
        }
      ]
    }
  }
}

示例数据:

{
        "_index": "stof_64371064",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.0,
        "_source": {
          "streetNr": 90,
          "geographicAddress": {
            "city": "Berlin"
          }
        }
      },
      {
        "_index": "stof_64371064",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.0,
        "_source": {
          "streetNr": 10,
          "geographicAddress": {
            "city": "Berlin"
          }
        }
      }
jq6vz3qz

jq6vz3qz1#

请参考关于bool查询的官方文档,以获得对各种条款的详细了解。
第一个搜索查询的结构如下-

{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {},
              {}
            ],
            "minimum_should_match": 1
          }
        }
      ]
    }
  }
}
``` `filter` 子句正在 Package  `should` 查询,但在 `should` 条款, `"minimum_should_match": 1` 已添加,表示1 `should` 条款必须是强制性的。
第二个搜索查询的结构如下-

{
"query": {
"bool": {
"filter": [
{
"bool": {
"should": {},
"minimum_should_match": "1"
}
},
{
"bool": {
"should": {},
"minimum_should_match": "1"
}
}
]
}
}
}

既然你添加了 `"minimum_should_match": "1"` 每 `should` 从句,那么在某种程度上,它就像一个 `must` 子句,因为在 `should` 条款。 `filter` 子句应用于包含 `bool should` 子句,所以当 `should` 子句匹配,那么只有你才能得到结果。
第三个搜索查询的结构如下-

{
"query": {
"bool": {
"filter": [
{
"bool": {
"should": [
{
"bool": {
"should": [
{}
],
"minimum_should_match": 1
}
},
{
"bool": {
"should": [
{}
],
"minimum_should_match": 1
}
}
],
"minimum_should_match": 1
}
}
]
}
}
}

在本文中,您使用了 `bool should` 条款。第一外 `bool should` 条款,再包两个 `bool should` 条款。但在外太空的尽头 `should` 您添加的条款 `"minimum_should_match": 1` . 所以尽管在这里 `filter` 子句存在,但即使有结果也会返回 `bool should` 子句满足条件。
添加索引数据、搜索查询和搜索结果的工作示例
索引数据:

{
"streetNr":0,
"geographicAddress":{
"city":"Berlin"
}
}
{
"streetNr":90,
"geographicAddress":{
"city":"Berlin"
}
}

搜索查询:(根据您的问题进行第二次搜索查询)

{
"query": {
"bool": {
"should": [ <-- note this
{
"bool": {
"should": [
{
"range": {
"streetNr": {
"from": "1",
"to": "100",
"include_lower": true,
"include_upper": true,
"boost": 1
}
}
}
],
"minimum_should_match": "1"
}
},
{
"bool": {
"should": [
{
"match": {
"geographicAddress.city": {
"query": "Berlin"
}
}
}
],
"minimum_should_match": "1"
}
}
]
}
}
}

搜索结果:

"hits": [
{
"_index": "stof_64371064",
"_type": "_doc",
"_id": "1",
"_score": 0.0,
"_source": {
"streetNr": 90,
"geographicAddress": {
"city": "Berlin"
}
}
},
{
"_index": "stof_64371064",
"_type": "_doc",
"_id": "2",
"_score": 0.0,
"_source": {
"streetNr": 0,
"geographicAddress": {
"city": "Berlin"
}
}
}
]

yqkkidmi

yqkkidmi2#

shouldminimum should match=1 如果其中一个条件是right,则返回您在查询1和查询3中设置的文档。但是在第二个查询中,您在其中设置了两个条件 filter 和elasticsearch搜索并返回两个标准都有效的文档。因此,第二个查询的行为类似于 must 与…相比 should 在其他查询中。

相关问题