Elasticsearch自动补齐建议-completion suggester

mapping 添加测试数据 DSL 结果 代码

欢迎访问本人博客:http://wangnan.tech

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.代码

        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>            list =         response.getSuggest().getSuggestion("complete").getEntries();
        List suggestList = new ArrayList();
        if (list == null) {
            return null;
        } else {
            for (Suggest.Suggestion.Entry e : list){
                for (Suggest.Suggestion.Entry.Option option : e) {
                    suggestList.add(option.getText().toString());
                }
            }

你可能感兴趣的:(Elasticsearch自动补齐建议-completion suggester)