elasticsearch - 如何从elasticsearch 6.1搜索中排除字段?

标签 elasticsearch elasticsearch-6

我有一个包含多个字段的索引。我想根据所有字段中搜索字符串的存在进行过滤,除了一个 - 用户评论 .
我正在做的查询搜索是

{
    "from": offset,
    "size": limit,
    "_source": [
      "document_title"
    ],
    "query": {
      "function_score": {
        "query": {
          "bool": {
            "must":
            {
              "query_string": {
                "query": "#{query}"
              }
            }
          }
        }
      }
    }
  }

尽管查询字符串正在搜索所有字段,并在 中为我提供了具有匹配字符串的文档用户评论场也是如此。但是,我想针对所有不包含 的字段进行查询。用户评论 field 。
白名单是一个非常大的列表,而且字段的名称是动态的,因此使用 fields 参数提及白名单字段列表是不可行的。
"query_string": {
                    "query": "#{query}",
                    "fields": [
                      "document_title",
                      "field2"
                    ]
                  }

任何人都可以提出一个关于如何从搜索中排除字段的想法吗?

最佳答案

有一种方法可以让它工作,它不是很漂亮,但可以完成工作。您可以使用 boost 实现您的目标。和 multifield query_string的参数, bool 查询以结合分数和设置 min_score :

POST my-query-string/doc/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "query": "#{query}",
            "type": "most_fields",
            "boost": 1
          }
        },
        {
          "query_string": {
            "fields": [
              "comments"
            ],
            "query": "#{query}",
            "boost": -1
          }
        }
      ]
    }
  },
  "min_score": 0.00001
}
那么引擎盖下会发生什么?
假设您有以下一组文档:
PUT my-query-string/doc/1
{
  "title": "Prodigy in Bristol",
  "text": "Prodigy in Bristol",
  "comments": "Prodigy in Bristol"
}
PUT my-query-string/doc/2
{
  "title": "Prodigy in Birmigham",
  "text": "Prodigy in Birmigham",
  "comments": "And also in Bristol"
}
PUT my-query-string/doc/3
{
  "title": "Prodigy in Birmigham",
  "text": "Prodigy in Birmigham and Bristol",
  "comments": "And also in Cardiff"
}
PUT my-query-string/doc/4
{
  "title": "Prodigy in Birmigham",
  "text": "Prodigy in Birmigham",
  "comments": "And also in Cardiff"
}
在您的搜索请求中,您只想查看文档 1 和 3,但您的原始查询将返回 1、2 和 3。
在 Elasticsearch 中,搜索结果按 relevance _score 排序,分数越大越好。
所以让我们尝试 boost"comments"字段,因此它对相关性分数的影响被忽略。我们可以通过将两个查询与 should 结合来实现这一点。并使用负值 boost :
POST my-query-string/doc/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "query": "Bristol"
          }
        },
        {
          "query_string": {
            "fields": [
              "comments"
            ],
            "query": "Bristol",
            "boost": -1
          }
        }
      ]
    }
  }
}
这将为我们提供以下输出:
{
  "hits": {
    "total": 3,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "my-query-string",
        "_type": "doc",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "title": "Prodigy in Birmigham",
          "text": "Prodigy in Birmigham and Bristol",
          "comments": "And also in Cardiff"
        }
      },
      {
        "_index": "my-query-string",
        "_type": "doc",
        "_id": "2",
        "_score": 0,
        "_source": {
          "title": "Prodigy in Birmigham",
          "text": "Prodigy in Birmigham",
          "comments": "And also in Bristol"
        }
      },
      {
        "_index": "my-query-string",
        "_type": "doc",
        "_id": "1",
        "_score": 0,
        "_source": {
          "title": "Prodigy in Bristol",
          "text": "Prodigy in Bristol",
          "comments": "Prodigy in Bristol",
          "discount_percent": 10
        }
      }
    ]
  }
}
文档 2 受到了惩罚,但文档 1 也受到了惩罚,尽管它是我们想要的匹配项。为什么会这样?
以下是 Elasticsearch 的计算方式 _score在这种情况下:

_score = max(title:"Bristol", text:"Bristol", comments:"Bristol") - comments:"Bristol"


文档 1 匹配 comments:"Bristol"部分,它也恰好是最好的分数。根据我们的公式,结果分数为 0。
我们实际上想要做的是提升第一个子句(带有“所有”字段)更多 如果匹配更多字段。
我们可以提升吗query_string匹配更多字段?
我们可以,query_stringmultifield模式有一个 type参数就是这样做的。查询将如下所示:
POST my-query-string/doc/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "type": "most_fields",
            "query": "Bristol"
          }
        },
        {
          "query_string": {
            "fields": [
              "comments"
            ],
            "query": "Bristol",
            "boost": -1
          }
        }
      ]
    }
  }
}
这将为我们提供以下输出:
{
  "hits": {
    "total": 3,
    "max_score": 0.57536423,
    "hits": [
      {
        "_index": "my-query-string",
        "_type": "doc",
        "_id": "1",
        "_score": 0.57536423,
        "_source": {
          "title": "Prodigy in Bristol",
          "text": "Prodigy in Bristol",
          "comments": "Prodigy in Bristol",
          "discount_percent": 10
        }
      },
      {
        "_index": "my-query-string",
        "_type": "doc",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "title": "Prodigy in Birmigham",
          "text": "Prodigy in Birmigham and Bristol",
          "comments": "And also in Cardiff"
        }
      },
      {
        "_index": "my-query-string",
        "_type": "doc",
        "_id": "2",
        "_score": 0,
        "_source": {
          "title": "Prodigy in Birmigham",
          "text": "Prodigy in Birmigham",
          "comments": "And also in Bristol"
        }
      }
    ]
  }
}
如您所见,不需要的文档 2 位于底部,其得分为 0。这是这次得分的计算方式:

_score = sum(title:"Bristol", text:"Bristol", comments:"Bristol") - comments:"Bristol"


所以文档匹配"Bristol"在任何领域都被选中。 comments:"Bristol" 的相关性得分被淘汰了,只有匹配的文档 title:"Bristol"text:"Bristol"得到了 _score > 0。
我们可以过滤掉那些分数不理想的结果吗?
是的,我们可以,使用 min_score :
POST my-query-string/doc/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "query": "Bristol",
            "type": "most_fields",
            "boost": 1
          }
        },
        {
          "query_string": {
            "fields": [
              "comments"
            ],
            "query": "Bristol",
            "boost": -1
          }
        }
      ]
    }
  },
  "min_score": 0.00001
}
这将起作用(在我们的例子中),因为当且仅当 "Bristol" 文档的分数将为 0匹配字段 "comments" only 并且不匹配任何其他字段。
输出将是:
{
  "hits": {
    "total": 2,
    "max_score": 0.57536423,
    "hits": [
      {
        "_index": "my-query-string",
        "_type": "doc",
        "_id": "1",
        "_score": 0.57536423,
        "_source": {
          "title": "Prodigy in Bristol",
          "text": "Prodigy in Bristol",
          "comments": "Prodigy in Bristol",
          "discount_percent": 10
        }
      },
      {
        "_index": "my-query-string",
        "_type": "doc",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "title": "Prodigy in Birmigham",
          "text": "Prodigy in Birmigham and Bristol",
          "comments": "And also in Cardiff"
        }
      }
    ]
  }
}
可以以不同的方式完成吗?
当然。我实际上不建议使用 _score调整,因为这是一个非常复杂的问题。
我建议获取现有映射并构建一个字段列表以预先运行查询,这将使代码更加简单明了。
答案中提出的原始解决方案(保留历史记录)
最初建议使用这种查询,其意图与上述解决方案完全相同:
POST my-query-string/doc/_search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": {
            "query_string": {
              "fields" : ["*", "comments^0"],
              "query": "#{query}"
            }
          }
        }
      }
    }
  },
  "min_score": 0.00001
}
唯一的问题是,如果索引包含任何数值,这部分:
"fields": ["*"]
引发错误,因为文本查询字符串不能应用于数字。

关于elasticsearch - 如何从elasticsearch 6.1搜索中排除字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52757277/

相关文章:

elasticsearch - “United States”不是[“United”,“States”]

elasticsearch-6 - Elasticsearch : Exact match for multiple fields

elasticsearch - 重新使用内置语言过滤器?

elasticsearch - 连字符上的打破字段值

php - 将原始的Elasticsearch-php库集成到Laravel 4.2中吗?

c# - 带有附加 TermAggregation 的 TermAggregation 在 AggregationDictionary 中不起作用

elasticsearch - 如何在嵌入式ElasticSearch中记录所有查询?

elasticsearch - 根据数组 ElasticSearch 中的项目过滤文档

java - 基于_source字段搜索查询elasticsearch

elasticsearch - Grafana没有从本地Elastic Search加载数据