微信号:weixin888
我在用 Elasticsearch 做搜索期间总共写了五篇大家可以关联着看,希望可以解决大家开发中遇到的一些问题
举例,我们使用Google的时候,Google会提示我们
Suggesters API 来实现这个功能
curl -XPUT "localhost:9200/test_index?pretty=true" -H 'content-type: application/json' --data '
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"title": {
"type": "completion",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"content": {
"type": "completion",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"status": {
"type": "keyword"
}
}
}
}
'
curl -XPOST "localhost:9200/test_index/_doc/?pretty=true" -H 'content-type: application/json' --data '
{
"id": 1,
"title": "春节碰到自己的前男友怎么办?",
"content": "如题",
"status": "1"
}
'
curl -XPOST "localhost:9200/test_index/_doc/?pretty=true" -H 'content-type: application/json' --data '
{
"id": 1,
"title": "春节碰到自己的前女友怎么办?",
"content": "如题",
"status": "1"
}
'
curl -XPOST "localhost:9200/test_index/_doc/?pretty=true" -H 'content-type: application/json' --data '
{
"id": 1,
"title": "春节碰到自己的老师怎么办?",
"content": "如题",
"status": "1"
}
'
curl -XPOST "localhost:9200/test_index/_doc/?pretty=true" -H 'content-type: application/json' --data '
{
"id": 1,
"title": "春节碰到自己的老师怎么办?我应该打招呼吗?",
"content": "如题",
"status": "1"
}
'
curl -XPOST "localhost:9200/test_index/_doc/_search?pretty=true" -H 'content-type: application/json' --data '
{
"suggest": {
"suggest_1": {
"text": "春节碰",
"completion": {
"field": "title",
"skip_duplicates": true
}
}
}
}
'
## response
{
"took" : 2,
"timed_out" : false,
"_shards" :{
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" :{
"total" :{
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" :[ ]
},
"suggest" :{
"suggest_1" :[
{
"text" : "春节碰",
"offset" : 0,
"length" : 3,
"options" :[
{
"text" : "春节碰到自己的前女友怎么办?",
"_index" : "test_index",
"_type" : "_doc",
"_id" : "eOZ-bX8BVW9cjfXV7UG_",
"_score" : 1.0,
"_source" :{
"id" : 1,
"title" : "春节碰到自己的前女友怎么办?",
"content" : "如题",
"status" : "1"
}
},
{
"text" : "春节碰到自己的前男友怎么办?",
"_index" : "test_index",
"_type" : "_doc",
"_id" : "d-Z-bX8BVW9cjfXV7UGS",
"_score" : 1.0,
"_source" :{
"id" : 1,
"title" : "春节碰到自己的前男友怎么办?",
"content" : "如题",
"status" : "1"
}
},
{
"text" : "春节碰到自己的老师怎么办?",
"_index" : "test_index",
"_type" : "_doc",
"_id" : "eeZ-bX8BVW9cjfXV7UHr",
"_score" : 1.0,
"_source" :{
"id" : 1,
"title" : "春节碰到自己的老师怎么办?",
"content" : "如题",
"status" : "1"
}
},
{
"text" : "春节碰到自己的老师怎么办?我应该打招呼吗?",
"_index" : "test_index",
"_type" : "_doc",
"_id" : "euZ-bX8BVW9cjfXV7kEg",
"_score" : 1.0,
"_source" :{
"id" : 1,
"title" : "春节碰到自己的老师怎么办?我应该打招呼吗?",
"content" : "如题",
"status" : "1"
}
}
]
}
]
}
}