弹性搜索如何创建同义词

时间:2017-01-14 08:56:00

标签: elasticsearch

我按照这样的文档:

# create the index:
curl -X PUT "http://localhost:9200/test" -d '
{
  "settings": {
    "analysis": {
      "filter": {
        "my_synonym_filter": {
          "type": "synonym", 
          "synonyms": [ 
            "british,english",
            "queen,monarch"
          ]
        }
      },
      "analyzer": {
        "my_synonyms": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "my_synonym_filter" 
          ]
        }
      }
    }
  }
}'

# testing the analyzer:
curl -X GET "http://localhost:9200/test/_analyze" -d '{"analyzer" : "my_synonyms", "text" : "Elizabeth is the English queen"}'

# create 2 records:
curl -X POST "http://localhost:9200/test/1" -d '{"ethnicity": "british"}'
curl -X POST "http://localhost:9200/test/2" -d '{"ethnicity": "english"}'

# check that you have 2:
curl -X POST "http://localhost:9200/test/_search" -d '{"query":{"match_all":{}}}'

# search:
curl -X POST "http://localhost:9200/test/_search" -d '{"query":{"query_string":{"query": "english"}}}'

并且如上一个curl命令所示,搜索英语但它只返回一个结果而不是2。 有关如何使同义词有效的任何想法吗?

1 个答案:

答案 0 :(得分:0)

我必须指定分析器:

curl -X POST http://localhost:9200/test/_search?pretty=true -d '{
  "query": {
    "simple_query_string" : {
        "query": "english",
        "analyzer": "my_synonyms"
    }
  }
}'