我想在查询时使用不同的分析器来编写我的查询。
我从文档“Controlling Analysis”中了解到这是可能的:
[...] the full sequence at search time:
- The analyzer defined in the query itself, else
- The search_analyzer defined in the field mapping, else
- The analyzer defined in the field mapping, else
- The analyzer named default_search in the index settings, which defaults to
- The analyzer named default in the index settings, which defaults to
- The standard analyzer
但我不知道如何编写查询以便为不同的子句指定不同的分析器:
"query" => [
"bool" => [
"must" => [
{
"match": ["my_field": "My query"]
"<ANALYZER>": <ANALYZER_1>
}
],
"should" => [
{
"match": ["my_field": "My query"]
"<ANALYZER>": <ANALYZER_2>
}
]
]
]
我知道我可以索引两个或多个不同的字段,但我有很强的二级内存限制,我无法索引相同的信息 N 次。
谢谢
最佳答案
如果您还没有,您首先需要将自定义分析器映射到您的索引设置端点。
注意:如果索引存在并且正在运行,请务必先将其关闭。
POST/my_index/_close
然后将自定义分析器映射到设置端点。
PUT /my_index/_settings
{
"settings": {
"analysis": {
"analyzer": {
"custom_analyzer1": {
"type": "standard",
"stopwords_path": "stopwords/stopwords.txt"
},
"custom_analyzer2": {
"type": "standard",
"stopwords": ["stop", "words"]
}
}
}
}
}
再次打开索引。
POST/my_index/_open
现在您可以使用新的分析器查询您的索引。
GET /my_index/_search
{
"query": {
"bool": {
"should": [{
"match": {
"field_1": {
"query": "Hello world",
"analyzer": "custom_analyzer1"
}
}
}],
"must": [{
"match": {
"field_2": {
"query": "Stop words can be tough",
"analyzer": "custom_analyzer2"
}
}
}]
}
}
}
关于elasticsearch - 如何使用 Elasticsearch 在查询时指定不同的分析器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37754132/