天天看点

Elasticsearch2.1.0安装中文分词插件ik1.6

    1、到github网站下载源代码,网站地址为: https://github.com/medcl/elasticsearch-analysis-ik    2 、将解压目录文件中config/ik文件夹复制到ES安装目录config文件夹下。    3、将打包得到的zip文件解压, 复制 elasticsearch-analysis-ik-1.6.0.jar、 httpclient-4.4.1.jar、httpcore-4.4.1.jar(仓库拷贝) 到ES安装目录的lib目录下。    4、在ES的 plugins目录下建立ik文件夹,将打包文件中的plugin-descriptor.properties(elasticsearch-analysis-ik-master\src\main\resources )拷贝进去 重新启动elasticsearch服务,这样就完成配置了,收入命令: [html]  view plain copy

  1. curl -XPOST  "http://localhost:9200/userinfo/_analyze?analyzer=ik&pretty=true&text=我是中国人"  

测试结果如下: [html]  view plain copy

  1. {  
  2. tokens: [  
  3. {  
  4. token: text  
  5. start_offset: 2  
  6. end_offset: 6  
  7. type: ENGLISH  
  8. position: 1  
  9. }  
  10. {  
  11. token: 我  
  12. start_offset: 9  
  13. end_offset: 10  
  14. type: CN_CHAR  
  15. position: 2  
  16. }  
  17. {  
  18. token: 中国人  
  19. start_offset: 11  
  20. end_offset: 14  
  21. type: CN_WORD  
  22. position: 3  
  23. }  
  24. {  
  25. token: 中国  
  26. start_offset: 11  
  27. end_offset: 13  
  28. type: CN_WORD  
  29. position: 4  
  30. }  
  31. {  
  32. token: 国人  
  33. start_offset: 12  
  34. end_offset: 14  
  35. type: CN_WORD  
  36. position: 5  
  37. }  
  38. ]  
  39. }  

Quick Example

1.create a index

curl -XPUT http://localhost:9200/index      

2.create a mapping

curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
{
    "fulltext": {
             "_all": {
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_max_word",
            "term_vector": "no",
            "store": "false"
        },
        "properties": {
            "content": {
                "type": "string",
                "store": "no",
                "term_vector": "with_positions_offsets",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word",
                "include_in_all": "true",
                "boost": 8
            }
        }
    }
}'      

3.index some docs

curl -XPOST http://localhost:9200/index/fulltext/1 -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
'      
curl -XPOST http://localhost:9200/index/fulltext/2 -d'
{"content":"公安部:各地校车将享最高路权"}
'      
curl -XPOST http://localhost:9200/index/fulltext/3 -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'      
curl -XPOST http://localhost:9200/index/fulltext/4 -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
'      

4.query with highlighting

curl -XPOST http://localhost:9200/index/fulltext/_search  -d'
{
    "query" : { "term" : { "content" : "中国" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}
'      

继续阅读