接着上一篇文章elasticsearch SuggestionBuilder应用实例,我们使用elasticsearch本生的SuggestionBuilder,效果并不好。我们来使用https://github.com/spinscale/elasticsearch-suggest-plugin插件。
1.先从https://github.com/spinscale/elasticsearch-suggest-plugin
2.安装elasticsearch-suggest-plugin。根据自己elasticsearch版本号,选择相应的elasticsearch-suggest-plugin版本
bin/plugin -install de.spinscale/elasticsearch-plugin-suggest/0.90.1-0.7
3.使用方法。将elasticsearch-plugin-suggest-0.90.1-0.7.jar拷贝到WEB-INF/lib中,使用maven工程的:
<dependency>
<groupId>de.spinscale</groupId>
<artifactId>elasticsearch-suggest-plugin</artifactId>
<version>0.90.1-0.7</version>
</dependency>
代码实例:
import de.spinscale.elasticsearch.client.action.suggest.SuggestRequestBuilder;
Client client = ESServiceSingleton2.getTransportClient();
List<String> suggestions = new SuggestRequestBuilder(client)
.field("content")//查询field
.term(q)//提示关键字
.size(n)//返回结果数
.similarity(0.5f)//相似度
.execute().actionGet().suggestions();
for(String s: suggestions){
System.out.println(s);
}
//注意导入的类名
结果:
//输入 “生活”,返回5条
业余生活
人民生活
生活
生活中
生活区
呵呵,看来结果不错。
参考资料:
https://github.com/spinscale/elasticsearch-suggest-plugin