elasticsearch中文分词器的安装和体验

因为本人使用的elasticsearch的版本为5.2.1,相对应的中文分词器的下载地址为https://github.com/medcl/elasticsearch-analysis-ik/tree/v5.2.1.(请根据自己使用版本的不同进行下载)

安装其实挺简单,编译后解压缩到elasticsearch的安装目录,以下是我的安装目录.

unzip -d /usr/local/elasticsearch/plugins/ik/ elasticsearch-analysis-ik-5.2.1.zip

然后重启elasticsearch.

我们还是插入四条数据,检索全部如下

http://192.168.5.182:9200/ecommerce/toothpaste/_search

{

"took": 123,
"timed_out": false,
"_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
},
"hits": {
    "total": 4,
    "max_score": 1,
    "hits": [
        {
            "_index": "ecommerce",
            "_type": "toothpaste",
            "_id": "2",
            "_score": 1,
            "_source": {
                "name": "佳洁士牙膏",
                "desc": "有效防蛀",
                "price": 25,
                "producer": "佳洁士生产商",
                "tags": [
                    "防蛀"
                ]
            }
        },
        {
            "_index": "ecommerce",
            "_type": "toothpaste",
            "_id": "4",
            "_score": 1,
            "_source": {
                "name": "特别牙膏",
                "desc": "特别美白",
                "price": 50,
                "producer": "特别牙膏生产商",
                "tags": [
                    "美白",
                    "防蛀"
                ]
            }
        },
        {
            "_index": "ecommerce",
            "_type": "toothpaste",
            "_id": "1",
            "_score": 1,
            "_source": {
                "name": "高露洁牙膏",
                "desc": "高效美白",
                "price": 30,
                "producer": "高露洁生产商",
                "tags": [
                    "美白",
                    "防蛀"
                ]
            }
        },
        {
            "_index": "ecommerce",
            "_type": "toothpaste",
            "_id": "3",
            "_score": 1,
            "_source": {
                "name": "中华牙膏",
                "desc": "草本植物",
                "price": 40,
                "producer": "中华生产商",
                "tags": [
                    "清新"
                ]
            }
        }
    ]
}

}

全文检索

[root@host2 bin]# curl -XGET 'http://192.168.5.182:9200/ecommerce/toothpaste/_search' -d'

{

"query":{
     "match":{
          "producer":"牙膏生产商"
     }
}

}'
{"took":76,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":4,"max_score":3.6265414,"hits":[{"_index":"ecommerce","_type":"toothpaste","_id":"4","_score":3.6265414,"_source":{

"name":"特别牙膏",
"desc":"特别美白",
"price":50,
"producer":"特别牙膏生产商",
"tags":["美白","防蛀"]

}},{"_index":"ecommerce","_type":"toothpaste","_id":"3","_score":1.7306128,"_source":{

"name":"中华牙膏",
"desc":"草本植物",
"price":40,
"producer":"中华生产商",
"tags":["清新"]

}},{"_index":"ecommerce","_type":"toothpaste","_id":"2","_score":1.6805282,"_source":{

"name":"佳洁士牙膏",
"desc":"有效防蛀",
"price":25,
"producer":"佳洁士生产商",
"tags":["防蛀"]

}},{"_index":"ecommerce","_type":"toothpaste","_id":"1","_score":1.5775073,"_source":{

"name":"高露洁牙膏",
"desc":"高效美白",
"price":30,
"producer":"高露洁生产商",
"tags":["美白","防蛀"]

}}]}}

根据以上结果可以看出,中文分词检索成功,所有有生产商的都会被检索出来,而由"牙膏生产商"检索出的特别牙膏生产商排第一,为最为匹配的.

上一篇:elasticsearch的restful API和Java API


下一篇:@Compenent,@Autowired,@PostConstruct自实现