How to replace greek letter synonyms using search_analyzer in Elasticsearch

旧街凉风 提交于 2020-05-31 03:21:06

问题


I wish to improve search in an ES index by searching for greek letter synonyms (α as alpha). In this post they use a 'regular' analyzer that would require to re-index all of the data.

My question is how to accomplish this synonym search using a search_analyzer only.

Thanks!

Here is an example of two entries and a search query, I would like this single query to return both docs

PUT test_ind/_doc/2
{
    "title" : "α" 
}

PUT test_ind/_doc/1
{
    "title" : "alpha"       
}

POST test_ind/_search
{
  "query": {
    "term": {
    "title": "alpha"

  }}
}

expected output:

{
  "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" : "α"
        }
      }
    ]
  }
}

回答1:


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"
      }
    }
  }
}

And if your index already exists you need to add the analyzer (no reindexing required) as shown here

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


来源:https://stackoverflow.com/questions/61761287/how-to-replace-greek-letter-synonyms-using-search-analyzer-in-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!