您的位置:首页 > 其它

Elasticsearch suggester搜索建议初步

2016-08-19 22:06 267 查看

环境

Elasticsearch 2.3.5

Elasticsearch-ik-plugin

实现

搜索建议的对象

假设有以下两个json对象,需要对其中tags字段进行搜索建议:

//对象Product1
{
"title": "Product1",
"description": "Product1 Description",
"tags": [
"山东",
"山东高新开发区",
"山东大学",
"two columns",
"wordpress"
]
}

//对象Product2
{
"title": "Product2",
"description": "Product2 Description",
"tags": [
"山东省",
"山东公安局",
"山东检察院",
"skrill",
"wordland"
]
}


设置索引mapping

建立索引suggester_ik_test和mapping,如下:

注意使用的suggester类型为completion

curl -XPUT "http://10.110.13.57:9200/suggester_ik_test?pretty" -d'
{
"mappings": {
"product": {
"properties": {
"description": {
"type": "string"
},
"tags": {
"type": "string"
},
"title": {
"type": "string"
},
"tag_suggest": {
"type": "completion",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"payloads": false
}
}
}
}
}'


索引数据

根据上述索引和mapping对json数据建立索引:

//Product1
curl -XPUT "http://10.110.13.57:9200/suggester_ik_test/product/1?pretty" -d'
{
"title": "Product1",
"description": "Product1 Description",
"tags": [
"山东",
"山东高新开发区",
"山东大学",
"two columns",
"wordpress"
],
"tag_suggest": {
"input": [
"山东",
"山东高新开发区",
"山东大学",
"two columns",
"wordpress"
]
}
}'

//Product2
curl -XPUT "http://10.110.13.57:9200/suggester_ik_test/product/2?pretty" -d'
{
"title": "Product2",
"description": "Product2 Description",
"tags": [
"山东省",
"山东公安局",
"山东检察院",
"skrill",
"wordland"
],
"tag_suggest": {
"input": [
"山东省",
"山东公安局",
"山东检察院",
"skrill",
"wordland"
]
}
}'


测试

搜索“山东”,查看是否有搜索建议提示生效:

curl -XPOST "http://10.110.13.57:9200/suggester_ik_test/_suggest?pretty" -d'
{
"product_suggest":{
"text":"山东",
"completion": {
"field" : "tag_suggest"
}
}
}'

//应该得到以下数据
{
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"product_suggest" : [ {
"text" : "山东",
"offset" : 0,
"length" : 2,
"options" : [ {
"text" : "山东",
"score" : 1.0
}, {
"text" : "山东公安局",
"score" : 1.0
}, {
"text" : "山东大学",
"score" : 1.0
}, {
"text" : "山东检察院",
"score" : 1.0
}, {
"text" : "山东省",
"score" : 1.0
} ]
} ]
}

参考资料

Quick and Dirty Autocomplete with Elasticsearch Completion Suggest
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: