1. 程式人生 > >Elasticsearch IK 同義詞

Elasticsearch IK 同義詞

同義詞配置

step 1

elasticserach.yml 最後一行新增:
index.analysis.analyzer.default.type: ik

step 2

elasticsearch-2.3.1/config目錄下面,存放synonyms.txt

其中,synonyms.txt 編碼格式為’utf-8’,內容為:

    #Example:
    ipod, i-pod, i pod
    foozball , foosball
    universe , cosmos
    西紅柿, 番茄
    馬鈴薯, 土豆
    aa, bb

step 3

新建立索引型別設定

curl -XPUT localhost:9200/test/_mapping?pretty -d '
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "jt_cn": {
            "type": "custom",
            "use_smart": "true",
            "tokenizer": "ik_smart",
            "filter": ["jt_tfr","jt_sfr"],
            "char_filter"
: ["jt_cfr"] }, "ik_smart": { "type": "ik", "use_smart": "true" }, "ik_max_word": { "type": "ik", "use_smart": "false" } }, "filter": { "jt_tfr": { "type": "stop", "stopwords"
: [" "] }, "jt_sfr": { "type": "synonym", "synonyms_path": "synonyms.txt" } }, "char_filter": { "jt_cfr": { "type": "mapping", "mappings": [ "| => \\|" ] } } } } }, "mappings": { "solution": { "properties": { "title": { "include_in_all": true, "analyzer": "jt_cn", "term_vector": "with_positions_offsets", "boost": 8, "store": true, "type": "string" } } } } } '

step 4

curl -XPUT localhost:9200/test/solution/1 -d '
{
    "title": "番茄"
}
'
curl -XPUT localhost:9200/test/solution/2 -d '
{
    "title": "西紅柿"
}
'

step 5

  curl -XPOST 'localhost:9200/test/solution/_search?pretty' -d '
   {
     "query": {
       "query_string": {
         "title": {
           "query": "西紅柿",
           "analyzer": "jt_cn"
         }
       }
     },
     "highlight": {
       "pre_tags": [
         "<tag1>",
         "<tag2>"
       ],
       "post_tags": [
         "</tag1>",
         "</tag2>"
       ],
       "fields": {
         "title": {}
       }
     }
   }
   '

step 6

   {
     "took": 3,
     "timed_out": false,
     "_shards": {
       "total": 5,
       "successful": 5,
       "failed": 0
     },
     "hits": {
       "total": 2,
       "max_score": 0.4500804,
       "hits": [
         {
           "_index": "test",
           "_type": "solution",
           "_id": "1",
           "_score": 0.4500804,
           "_source": {
             "title": "西紅柿"
           }
         },
         {
           "_index": "test",
           "_type": "solution",
           "_id": "2",
           "_score": 0.36006433,
           "_source": {
             "title": "番茄"
           }
         }
       ]
     }
   }

動態更新同義詞檔案