gpt4 book ai didi

elasticsearch - Elasticsearch 中的查询与过滤器

转载 作者:行者123 更新时间:2023-11-29 02:55:59 25 4
gpt4 key购买 nike

我正在尝试索引一个文档,该文档具有三个字段 first_name、last_name、occupation of type "keyword"并且分别具有值 XYZ、ABC、DEF。

我已经使用过滤器编写了查询,以便与 AND 条件完全匹配,如下所示,

"query": {
"bool": {
"filter": [
{"term": {"first_name": "XYZ"}},
{"term": {"last_name": "ABC"}}
]
}
}

这必须返回一个文档,但什么也没有返回。

我有另一个相同操作的查询,

"query": {
"bool": {
"must": [
{"match": {"first_name": "XYZ"}},
{"match": {"last_name": "ABC"}}
]
}
}

这将返回一个文档。

根据Elasticsearch的文档,我了解到query和filter的区别在于filter不会对结果进行评分。我不确定为什么第一个查询不返回任何结果。我的理解正确吗?

最佳答案

正如文档所述,除了评分之外,查询和过滤器之间没有区别。当然这适用于查询和过滤器使用相同查询类型的情况。这里你使用了两种不同的类型 - termmatch . term 用于精确比较,而 match 用于分析并用作全文搜索。

请看下面的示例。

你的映射:

PUT /index_53053054
{
"mappings": {
"_doc": {
"properties": {
"first_name": {
"type": "text"
},
"last_name": {
"type": "text"
},
"occupation": {
"type": "keyword"
}
}
}
}
}

您的文档:

PUT index_53053054/_doc/1
{
"first_name": "XYZ",
"last_name": "ABC",
"occupation": "DEF"
}

过滤查询:

GET index_53053054/_search
{
"query": {
"bool": {
"filter": [
{
"match": {
"first_name": "XYZ"
}
},
{
"match": {
"last_name": "ABC"
}
},
{
"term": {
"occupation": "DEF"
}
}
]
}
}
}

和结果:

{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": [
{
"_index": "index_53053054",
"_type": "_doc",
"_id": "1",
"_score": 0,
"_source": {
"first_name": "XYZ",
"last_name": "ABC",
"occupation": "DEF"
}
}
]
}
}

类似必须查询:

GET index_53053054/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"first_name": "XYZ"
}
},
{
"match": {
"last_name": "ABC"
}
},
{
"term": {
"occupation": "DEF"
}
}
]
}
}
}

和响应:

{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.8630463,
"hits": [
{
"_index": "index_53053054",
"_type": "_doc",
"_id": "1",
"_score": 0.8630463,
"_source": {
"first_name": "XYZ",
"last_name": "ABC",
"occupation": "DEF"
}
}
]
}
}

如您所见,命中 几乎相同。唯一的区别是在 filter 中不计算分数,而在 must 查询中计算分数。

阅读更多:https://www.elastic.co/guide/en/elasticsearch/reference/6.4/query-filter-context.html

关于elasticsearch - Elasticsearch 中的查询与过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53053054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com