我刚打酱油去了,不好意思

询问一个Elasticsearch关于ansj的分词问题

Elasticsearch | 作者 a2615381 | 发布于2019年01月28日 | 阅读数:2148

通过命令行  我发出一个分词测试请求
curl -XGET 'http://localhost:9200/modelgoods/_analyze?analyzer=index_ansj&pretty' -d '多乐士'
{
  "tokens" : [
    {
      "token" : "多乐士",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "nr",
      "position" : 0
    },
    {
      "token" : "多乐",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "nrt",
      "position" : 1
    },
    {
      "token" : "乐",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "a",
      "position" : 3
    },
    {
      "token" : "士",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "ng",
      "position" : 4
    }
  ]
}
可以看到分词为 多乐士,多乐,乐,士
 
但是用ansj的分词java代码测试 却得不到这样的结果

str="多乐士"; 
List<Term> listTerm = IndexAnalysis.parse(str).getTerms();
结果只有    多乐士     nr
 
 
请问 ansj的java代码怎么写才能得到和 命令行一样的分词结果
 
已邀请:

寻z - elk

赞同来自:

看看java代码是不是用的query_ansj

rochy - rochy_he

赞同来自:

setAnalyzer("index_ansj")

rochy - rochy_he

赞同来自:

// 获取 TransportClient 实例 
TransportClient client = new TransportClient();

// 构建 AnalyzeRequest
AnalyzeRequest request = new AnalyzeRequest()
.text("this is a test") // 指定你需要 analyse 的文本
.analyzer("ik_smart"); // 指定你需要使用的 analyzer 名,大小写敏感

List<AnalyzeResponse.AnalyzeToken> tokens = client.admin().indices().analyze(request).actionGet().getTokens();
for (AnalyzeResponse.AnalyzeToken token : tokens){
System.out.println(token.getTerm()); // 在控制台输出
}
 
参考:https://elasticsearch.cn/question/643

要回复问题请先登录注册