如何在Elasticsearch中在查询时间而不是索引时间应用同义词
根据 elasticsearch参考文档,可以执行以下操作:
According to the elasticsearch reference documentation, it is possible to:
可以在索引时间或查询时间应用扩展。每个都有优点(⬆)︎和缺点(⬇)︎。何时使用取决于性能与灵活性。
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.
优点和缺点都有意义,我想针对我的特定用途在查询时使用同义词。我的用例是,我希望允许系统中的管理员用户管理这些同义词,而不必在更新时重新索引所有内容。另外,我也希望不关闭并重新打开索引。
The advantages and disadvantages all make sense and for my specific use I want to make use of synonyms at query time. My use case is that I want to allow admin users in my system to curate these synonyms without having to reindex everything on an update. Also, I'd like to do it without closing and reopening the index.
我认为这是可能的主要原因是此优势:
The main reason I believe this is possible is this advantage:
( ⬆)︎无需重新编制文档索引就可以更新同义词规则。
(⬆)︎ Synonym rules can be updated without reindexing documents.
但是,我找不到描述如何在查询时间而不是索引时间应用同义词。
However, I can't find any documentation describing how to apply synonyms at query time instead of index time.
要使用一个具体的示例,如果我执行以下操作(示例被盗,并从参考),看来这将适用索引时的同义词:
To use a concrete example, if I do the following (example stolen and slightly modified from the reference), it seems like this would apply the synonyms at index time:
/* 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"
}
}
]
}
}
所以我的问题是:如何修改上面的示例,以便在查询时间使用使用同义词?
So my question is: how could I amend the above example so that I would be using the synonyms at query time?
或者我是完全在错误的树上吠叫吗?我已经看过在类似问题的答案中提到的插件,例如 https://stackoverflow.com/a/34210587/2240218 和 https://stackoverflow.com/a/18481495/2240218 ,但它们似乎都已经存在了几年,并且
Or am I barking up completely the wrong tree and can you point me somewhere else please? I've looked at plugins mentioned in answers to similar questions like https://stackoverflow.com/a/34210587/2240218 and https://stackoverflow.com/a/18481495/2240218 but they all seem to be a couple of years old and unmaintained, so I'd prefer to avoid these.
只需使用 search_analyzer
而不是映射中的分析器
,您的同义词分析器将仅在搜索时使用
Simply use search_analyzer
instead of analyzer
in your mapping and your synonym analyzer will only be used at search time
PUT my_synonyms_test/rulers/_mapping
{
"properties": {
"name": {
"type": "string"
},
"title": {
"type": "string",
"search_analyzer": "my_synonyms" <--- change this
}
}
}