Elasticsearch IK 同义词

本文介绍如何在Elasticsearch中配置同义词,包括修改配置文件、创建同义词文件、设置索引分析器及动态更新同义词等步骤,通过实例展示了配置过程及其效果。

同义词配置

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
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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"
        }
      }
    }
  }
}
'
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

step 4

curl -XPUT localhost:9200/test/solution/1 -d '
{
    "title": "番茄"
}
'
curl -XPUT localhost:9200/test/solution/2 -d '
{
    "title": "西红柿"
}
'
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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": {}
       }
     }
   }
   '
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

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": "番茄"
           }
         }
       ]
     }
   }
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

动态更新同义词文件

手动动态更新方法

自动动态更新插件

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值