Table of Contents

  1. 1.mapping
  2. 2 .添加测试数据
  3. 3.DSL
  4. 4.结果
  5. 5.代码

1.mapping

curl -XPUT 192.168.0.1:9200/person -d'  //新建一个persion的索引
{
   "mappings": {
      "person": { //这个是_type
         "properties": {
            "name": {
               "type": "string"
            }
            "tag_suggest": {
               "type": "completion",  //设置为completion才能被suggest捕获
               "index_analyzer": "ik",
               "search_analyzer": "ik",
               "payloads": false
            }
         }
      }
   }
}'

2 .添加测试数据

curl -XPUT  192.168.2.20:9200/person/person/1 -d'
{
"name": [
     "david",
    "jacky"
 ],
"tag_suggest": {
    "input": [
     "david",
     "jacky"
  ]
  }
}'
curl -XPUT 192.168.0.1:9200/person/person/1 -d'
{
"name": [
    "andy",
    "jackson"
],
 "tag_suggest": {
"input": [
     "andy",
    "jackson"
  ]
}
}'

3.DSL

curl -XPOST 192.168.0.1:9200/person/_suggest -d'
{
 "person_suggest":{
      "text":"jack",
     "completion": {
          "field" : "tag_suggest"
    }
}
}'

4.结果

{
 "_shards": {
  "total": 1,
  "successful": 1,
  "failed": 0
 },
 "person_suggest": [
  {
     "text": "word",
     "offset": 0,
     "length": 4,
     "options": [
        {
           "text": "jacky",
           "score": 1
        },
        {
           "text": "jackson",
           "score": 1
        }
     ]
  }
]
}

5.代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
CompletionSuggestionBuilder completionSuggestionBuilder = new
CompletionSuggestionBuilder("complete");
completionSuggestionBuilder.text(paramMap.get("text"));
completionSuggestionBuilder.field(paramMap.get("field"));
completionSuggestionBuilder.size(10);
IElasticsearchClient client = index.getIndexClient();
CompletionSuggestionBuilder completionSuggestion = completionSuggestionBuilder
SuggestResponse resp = client.prepareSuggest(realIndexName)
.addSuggestion(completionSuggestion).execute().actionGet();
List<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> list = response.getSuggest().getSuggestion("complete").getEntries();
List<String> suggestList = new ArrayList<String>();
if (list == null) {
return null;
} else {
for (Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option> e : list){
for (Suggest.Suggestion.Entry.Option option : e) {
suggestList.add(option.getText().toString());
}
}