gpt4 book ai didi

elasticsearch - 如何在查询时而不是在 Elasticsearch 中的索引时应用同义词

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

根据 the elasticsearch reference documentation , 可以:

Expansion can be applied either at index time or at query time. Each has advantages (⬆)︎ and disadvantages (⬇)︎. When to use which comes down to performance versus flexibility.

优点和缺点都很有意义,对于我的特定用途,我想在查询时使用同义词。我的用例是,我想让系统中的管理员用户管理这些同义词,而不必在更新时重新索引所有内容。另外,我想在不关闭和重新打开索引的情况下执行此操作。

我认为这是可能的主要原因是这个优势:

(⬆)︎ Synonym rules can be updated without reindexing documents.

但是,我找不到任何描述如何在查询时而非索引时应用同义词的文档。

使用一个具体示例,如果我执行以下操作(从 the reference 中窃取并稍作修改的示例),这似乎会在索引时应用同义词:

/* NOTE: This was all run against elasticsearch 1.5 (if that matters; documentation is identical in 2.x) */

// Create our synonyms filter and analyzer on the index
PUT my_synonyms_test
{
"settings": {
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym",
"synonyms": [
"queen,monarch"
]
}
},
"analyzer": {
"my_synonyms": {
"tokenizer": "standard",
"filter": [
"lowercase",
"my_synonym_filter"
]
}
}
}
}
}

// Create a mapping that uses this analyzer
PUT my_synonyms_test/rulers/_mapping
{
"properties": {
"name": {
"type": "string"
},
"title": {
"type": "string",
"analyzer": "my_synonyms"
}
}
}

// Some data
PUT my_synonyms_test/rulers/1
{
"name": "Elizabeth II",
"title": "Queen"
}

// A query which utilises the synonyms
GET my_synonyms_test/rulers/_search
{
"query": {
"match": {
"title": "monarch"
}
}
}

// And we get our expected result back:
{
"took": 42,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.4142135,
"hits": [
{
"_index": "my_synonyms_test",
"_type": "rulers",
"_id": "1",
"_score": 1.4142135,
"_source": {
"name": "Elizabeth II",
"title": "Queen"
}
}
]
}
}

所以我的问题是:如何修改上面的示例,以便在查询时 使用同义词?

或者我完全找错了树,你能指点我别的地方吗?我看过类似问题的答案中提到的插件,如 https://stackoverflow.com/a/34210587/2240218https://stackoverflow.com/a/18481495/2240218但它们似乎都有几年历史且无人维护,所以我宁愿避免使用这些。

最佳答案

只需在映射中使用 search_analyzer 而不是 analyzer,您的同义词分析器将仅在搜索时使用

PUT my_synonyms_test/rulers/_mapping
{
"properties": {
"name": {
"type": "string"
},
"title": {
"type": "string",
"search_analyzer": "my_synonyms" <--- change this
}
}
}

关于elasticsearch - 如何在查询时而不是在 Elasticsearch 中的索引时应用同义词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42071623/

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