elasticsearch - 如何在 Elasticsearch 中使用 search_analyzer 替换希腊字母同义词
问题描述
我希望通过搜索希腊字母同义词(α 作为 alpha)来改进 ES 索引中的搜索。在这篇文章中,他们使用需要重新索引所有数据的“常规”分析器。
我的问题是如何仅使用 search_analyzer 完成这个同义词搜索。
谢谢!
这是两个条目和一个搜索查询的示例,我希望这个单个查询返回两个文档
PUT test_ind/_doc/2
{
"title" : "α"
}
PUT test_ind/_doc/1
{
"title" : "alpha"
}
POST test_ind/_search
{
"query": {
"term": {
"title": "alpha"
}}
}
预期输出:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "test_ind",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"title" : "alpha"
}
},
{
"_index" : "test_ind",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"title" : "α"
}
}
]
}
}
解决方案
PUT test_ind
{
"settings": {
"analysis": {
"analyzer": {
"synonyms": {
"tokenizer": "whitespace",
"filter": [
"synonym"
]
}
},
"filter": {
"synonym": {
"type": "synonym",
"synonyms": [
"α,alpha"
]
}
}
}
}
}
PUT test_ind/_doc/2
{
"title" : "α"
}
PUT test_ind/_doc/1
{
"title" : "alpha"
}
POST test_ind/_search
{
"query": {
"match": {
"title": {
"query": "alpha",
"analyzer": "synonyms"
}
}
}
}
如果您的索引已经存在,您需要添加分析器(不需要重新索引),如此处所示
POST /test_ind/_close
PUT /test_ind/_settings
{
"analysis": {
"analyzer": {
"synonyms": {
"tokenizer": "whitespace",
"filter": [
"synonym"
]
}
},
"filter": {
"synonym": {
"type": "synonym",
"synonyms": [
"α,alpha"
]
}
}
}
}
POST /test_ind/_open
推荐阅读
- php - 如何在 Laravel 中添加自定义 ini_set
- php - Woocommerce - 根据产品类别停止运行代码
- linux - ksmserver-logout-greeter 意外关闭
- visual-studio-code - 如何将live sass编译器的路径设置为VsCode中的当前路径
- javascript - 变量不会显示变化
- windows - Windows 11 中 Visual Studio Code 中的多行编辑
- python - 解包 * 在 leetcode 上抛出语法错误
- azure - kubectl get pods 命令显示“ErrImageNeverPull”
- node.js - 在 Web 数据迁移期间如何组织 AWS S3 存储桶中的数据?
- node.js - 使用路由控制器和模型单独文件时无法从本地 mongoDB 获取集合