【问题标题】:How to replace greek letter synonyms using search_analyzer in Elasticsearch如何在 Elasticsearch 中使用 search_analyzer 替换希腊字母同义词
【发布时间】:2020-05-12 20:26:04
【问题描述】:

我希望通过搜索希腊字母同义词(α 作为 alpha)来改进 ES 索引中的搜索。在this post 中,他们使用需要重新索引所有数据的“常规”分析器。

我的问题是如何仅使用 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" : "α"
        }
      }
    ]
  }
}

【问题讨论】:

  • 您可以在搜索或索引中使用分析器。但从长远来看,索引时间会快得多。您不需要重新索引所有数据。您只需要添加另一个使用分析器的字段并将该字段包含在您的搜索中。那么你只是添加一个字段,这是直截了当的
  • 请举例说明一两个值(文档)和一两个查询输入
  • @AlkisKalogeris 添加了示例。我可以使用分析器来完成此操作,但不能仅在使用 search_analyzer 时完成

标签: elasticsearch synonym


【解决方案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"
      }
    }
  }
}

如果你的索引已经存在,你需要添加分析器(不需要重新索引),如图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

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 2017-03-09
    • 2021-07-16
    相关资源
    最近更新 更多