您的位置:首页 > 编程语言 > Java开发

指定分词器测试分词结果

2015-09-29 13:58 561 查看
有时需要指定es的分词器,测试分词粒度,获取分词结果,封装后的java-api如下:

/**
* 使用指定的分词器获取分词结果
* @param index 索引
* @param analyzer 分词器
* @param text 要分词的内容
* @return 分词结果
*/
public List<String> getAnalyzeResults(String index, String analyzer, String text) {
AdminClient adminClient = this.client.admin();
IndicesAdminClient indicesAdminClient = adminClient.indices();
AnalyzeRequestBuilder analyzeRequestBuilder = indicesAdminClient.prepareAnalyze(index, text);
analyzeRequestBuilder.setAnalyzer(analyzer);
AnalyzeResponse response = analyzeRequestBuilder.execute().actionGet();
List<AnalyzeToken> analyzeTokens = response.getTokens();
List<String> results = new ArrayList<String>();
for (AnalyzeToken token : analyzeTokens) {
results.add(token.getTerm());
}
return results;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java elasticsearch