
需求
Java API获取Elasticsearch的分词结果.
版本
Elasticsearch 5.4 
 已安装ik分词器
测试
先创建一个索引:
curl -XPUT localhost:9200/bbb
返回结果:
{    "acknowledged":true,    "shards_acknowledged":true } 好了,现在es里面有一个bbb的索引了。
Java代码:
标准分词
public static void main(String[] args) {        TransportClient client = EsUtils.getSingleClient();        AnalyzeRequest analyzeRequest = new AnalyzeRequest("bbb")                .text("中华人民共和国国歌")                .analyzer("standard");        List<AnalyzeResponse.AnalyzeToken> tokens = client.admin().indices()                .analyze(analyzeRequest)                .actionGet()                .getTokens();        for (AnalyzeResponse.AnalyzeToken token : tokens) {            System.out.println(token.getTerm());        }    } 结果:
中华人民共和国国歌
ik_max_word分词
把.analyzer("standard");改成 .analyzer("ik_max_word"); 分词结果如下:
中华人民共和国中华人民中华华人人民共和国人民共和国共和国国歌
ik_smart分词
把.analyzer("standard");改成 .analyzer("ik_smart"); 分词结果如下:
中华人民共和国国歌













