---
title: "Elasticsearch Suggest 插件使用"
author: "maxrocray"
date: "2012-12-14T17:18:02Z"
tags: []
categories: ["编程语言"]
slug: "elasticsearch-suggest"
draft: false
lastmod: "2012-12-14T17:18:30Z"
id: 1748172
url: "https://maxrocray.iteye.com/blog/1748172"
---

插件作用： 用于做搜索提示，如搜索 ’中‘ 时， 提示’中国‘，’中华‘，’中央‘等以’中‘开头的词。

 

安装： plugin -url https://github.com/downloads/spinscale/elasticsearch-suggest-plugin/elasticsearch-suggest-0.0.4-0.19.0.zip -install suggest 

  安装时，需要注意ES版本与插件版本的一致性。

 

编程示例：

 

```
//通过索引字段field及索引index中获取size个以keyword开头的提示单词。
```

```
public List<String> findSuggestions(String field, String keyword,
```

```
			Integer size, String... indices) throws Exception {
		List<String> suggests = null;

		suggests = new SuggestRequestBuilder(getClient()).setIndices(indices)
				.field(field).term(keyword).size(size).execute().actionGet()
				.getSuggestions();
		closeClient();
		return suggests;
	}
```

 

调用时， 如 List&lt;String&gt; suggestwords  = findSuggestions("username", "刘", 5, "userIndex");

即从索引userIndex中的username字段找5个以刘开头的名字。