【问题标题】:How to make elastic search more flexible?如何让弹性搜索更灵活?
【发布时间】:2020-02-16 20:28:50
【问题描述】:

我目前正在使用这个 elasticsearch DSL 查询:

{
    "_source": [
        "title",
        "bench",
        "id_",
        "court",
        "date"
    ],
    "size": 15,
    "from": 0,
    "query": {
        "bool": {
            "must": {
                "multi_match": {
                    "query": "i r coelho",
                    "fields": [
                        "title",
                        "content"
                    ]
                }
            },
            "filter": [],
            "should": {
                "multi_match": {
                    "query": "i r coelho",
                    "fields": [
                        "title.standard^16",
                        "content.standard"
                    ]
                }
            }
        }
    },
    "highlight": {
        "pre_tags": [
            "<tag1>"
        ],
        "post_tags": [
            "</tag1>"
        ],
        "fields": {
            "content": {}
        }
    }
}

这就是正在发生的事情。如果我搜索 I.r coelho它会返回正确的结果。但是,如果我搜索 I R coelho(没有句点),那么它会返回不同的结果。我该如何防止这种情况发生?即使有额外的句点、空格、逗号等,我也希望搜索行为相同。

映射

{
    "courts_2": {
        "mappings": {
            "properties": {
                "author": {
                    "type": "text",
                    "analyzer": "my_analyzer"
                },
                "bench": {
                    "type": "text",
                    "analyzer": "my_analyzer"
                },
                "citation": {
                    "type": "text"
                },
                "content": {
                    "type": "text",
                    "fields": {
                        "standard": {
                            "type": "text"
                        }
                    },
                    "analyzer": "my_analyzer"
                },
                "court": {
                    "type": "text"
                },
                "date": {
                    "type": "text"
                },
                "id_": {
                    "type": "text"
                },
                "title": {
                    "type": "text",
                    "fields": {
                        "standard": {
                            "type": "text"
                        }
                    },
                    "analyzer": "my_analyzer"
                },
                "verdict": {
                    "type": "text"
                }
            }
        }
    }
}

设置:

{
    "courts_2": {
        "settings": {
            "index": {
                "highlight": {
                    "max_analyzed_offset": "19000000"
                },
                "number_of_shards": "5",
                "provided_name": "courts_2",
                "creation_date": "1581094116992",
                "analysis": {
                    "filter": {
                        "my_metaphone": {
                            "replace": "true",
                            "type": "phonetic",
                            "encoder": "metaphone"
                        }
                    },
                    "analyzer": {
                        "my_analyzer": {
                            "filter": [
                                "lowercase",
                                "my_metaphone"
                            ],
                            "tokenizer": "standard"
                        }
                    }
                },
                "number_of_replicas": "1",
                "uuid": "MZSecLIVQy6jiI6YmqOGLg",
                "version": {
                    "created": "7010199"
                }
            }
        }
    }
}

编辑 以下是I.R coelho 的结果来自my analyzer - { "tokens": [ { "token": "IR", "start_offset": 0, "end_offset": 3, "type": "<ALPHANUM>", "position": 0 }, { "token": "KLH", "start_offset": 4, "end_offset": 10, "type": "<ALPHANUM>", "position": 1 } ] }

标准分析仪:

{
    "tokens": [
        {
            "token": "i.r",
            "start_offset": 0,
            "end_offset": 3,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "coelho",
            "start_offset": 4,
            "end_offset": 10,
            "type": "<ALPHANUM>",
            "position": 1
        }
    ]
}

【问题讨论】:

  • 我们需要知道你的索引的映射,你能把它贴出来吗?
  • 另外,与您的问题无关,在 mustshould 布尔块中都有 multi_match 查询有什么意义?
  • 嗨!我在映射中进行了编辑。至于逻辑,我真的不知道。我从其他人那里继承了此代码,以实现快速周转项目。如果有错误,请随时纠正。 @glenacota
  • 您正在使用名为my_analyzer 的自定义分析器。你能发布它的定义吗? (GET &lt;your_index&gt;/_settings)
  • 我很抱歉没有包括在内。我已经编辑了它。@glenacota

标签: elasticsearch elasticsearch-plugin


【解决方案1】:

您在搜索 I.r coelhoI R coelho 时出现不同行为的原因是您在同一字段上使用了不同的分析器,即 my_analyzer 用于 titlecontent (must块),standard(默认)用于title.standardcontent.standardshould 块)。

这两个分析器生成不同的标记,因此在您搜索 I.r coelho(例如,使用标准分析器的 2 个标记)或 I R coelho(例如,使用标准分析器的 3 个标记)时确定不同的分数。您可以使用analyze API 测试分析器的行为(请参阅Elastic Documentation)。

你必须决定这是否是你想要的行为。

更新(要求 OP 澄清后)

_analyze 查询的结果证实了这一假设:这两个分析器会导致不同的分数贡献,并且随后会根据您的查询是否包含符号字符而产生不同的结果。

如果您不希望查询结果受到点或大写/小写等符号的影响,则需要重新考虑要应用哪些分析器。当前使用的将永远无法满足您的要求。如果我正确理解您的要求,simple built-in analyzer 应该是适合您用例的那个。

简而言之,(1) 您应该考虑将 standard 内置分析器替换为 simple 之一,(2) 您应该决定是否希望您的查询根据以下条件对命中应用不同的分数不同的分析器(即,titlecontent 字段值上的语音自定义一个,以及它们各自子字段上的simple 一个)。

【讨论】:

  • 嗯是有道理的。我尝试使用 _analyze API 并使用它返回的两个分析器 I R coelho 作为三个令牌。这不是我想要的行为。我想要的是,I.R coelhoI R coelho 都会出现相同的结果。我该怎么办?
  • 当您使用standardmy_analyzer 分析器分析文本“I.R coelho”时会发生什么?
  • 我很抱歉回复晚了。我已经编辑了细节。谢谢!
  • 更新我的答案,但我只是提供了更多元素供您做出您的决定,因为您的问题没有唯一的答案。我希望我能帮助你更好地理解权衡
猜你喜欢
  • 2021-08-20
  • 1970-01-01
  • 2011-06-11
  • 2012-02-04
  • 2021-11-23
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 2018-10-26
相关资源
最近更新 更多