首页 > 解决方案 > 如何在 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" : "α"
        }
      }
    ]
  }
}

标签: elasticsearchsynonym

解决方案


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

推荐阅读