添加过滤器似乎会产生更多结果

时间:2019-06-01 01:01:50

标签: elasticsearch

一种奇怪的情况,涉及向现有的布尔查询添加过滤器。

此查询在此仅显示一个结果,即“ pages”索引中的一个结果。 可以预料,在我们这一边的“文档”索引对于该查询的结果是非常有用的。

这很有道理。此版本的查询工作正常。

Out
          1         2         3          4  ...         8         9        10    y
0  4.490397 -9.633152  7.701815 -15.195824  ...  2.666405  2.962832  1.229739  6.0

[1 rows x 11 columns]
Empty DataFrame
Columns: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, y]
Index: []

但是,用户可以将过滤器添加到现有查询中,在本例中为产品。

这是发送的查询。

{
  "index": "pages"
}
{
  "size": 30,
  "query": {
    "dis_max": {
      "queries": {
        "bool": {
          "should": [
            {
              "term": {
                "title_exact": "\"this is a test search phrase\""
              }
            },
            {
              "query_string": {
                "fields": [
                  "title"
                ],
                "query": "\"this is a test search phrase\""
              }
            },
            {
              "nested": {
                "path": "versions",
                "query": {
                  "query_string": {
                    "fields": [
                      "versions.page_content"
                    ],
                    "query": "\"this is a test search phrase\""
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}
{
  "index": "documents"
}
{
  "size": 30,
  "query": {
    "dis_max": {
      "queries": {
        "bool": {
          "should": [
            {
              "term": {
                "title_exact": "\"this is a test search phrase\""
              }
            },
            {
              "query_string": {
                "fields": [
                  "title"
                ],
                "query": "\"this is a test search phrase\""
              }
            },
            {
              "nested": {
                "path": "product.versions",
                "query": {
                  "query_string": {
                    "fields": [
                      "versions.page_content"
                    ],
                    "query": "\"this is a test search phrase\""
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

这里的问题是,尽管“ pages”索引仍然可以按需生成一个结果,但是“ documents”索引现在突然与其中具有该产品ID的每条记录匹配,从而提高了结果。

在这种情况下,“过滤器”和“必须”都做同样的事情。

老实说,它应该产生与上一个查询完全相同的结果。筛选器只能减少结果集,而不能增加结果集。

有人有什么想法吗?

谢谢

1 个答案:

答案 0 :(得分:1)

解决方案

在您的bool查询中添加minimum-should-match,以告诉查询仅在至少X个数量的should子句匹配时才返回文档。似乎默认值是0,这就是为什么结果基于filter查询,而should只分配scoring的原因。

添加minimum-should-match后的查询:

{
  "index": "documents"
}
{
  "size": 30,
  "query": {
    "dis_max": {
      "queries": {
        "bool": {
          "should": [
            {
              "term": {
                "title_exact": "\"this is a test search phrase\""
              }
            },
            {
              "query_string": {
                "fields": [
                  "title"
                ],
                "query": "\"this is a test search phrase\""
              }
            },
            {
              "nested": {
                "path": "product.versions",
                "query": {
                  "query_string": {
                    "fields": [
                      "versions.page_content"
                    ],
                    "query": "\"this is a test search phrase\""
                  }
                }
              }
            }
          ],
          "filter": [
            {
              "term": {
                "product.id": "a2c2c792-84ac-11e8-b4c6-005056a40c60"
              }
            }
          ],
          "minimum_should_match": 1
        }
      }
    }
  }
}

问题

此处简要说明:

  • filter-仅过滤文档,与scoring无关
  • should-如果匹配,则贡献给scoring。但是minimum-should-match将改变应退还文件的方式。
  • must-过滤文档并贡献给scoring

Bool query doc

在第二个document查询中,总匹配数基于filter子句,而should子句只会将scoring分配给匹配的文档,而不会t由于minimum-should-match(可能默认为0

而减少了结果