Spring Boot + RestHighLevelClient + IK 分词器
开发流程
使用Postman 操作
127.0.0.1:9200/_analyze
请求体:
{
"analyzer":"ik_max_word",
"text":"我是中国人"
}
响应体:
{
"tokens": [
{
"token": "我",
"start_offset": 0,
"end_offset": 1,
"type": "CN_CHAR",
"position": 0
},
{
"token": "是",
"start_offset": 1,
"end_offset": 2,
"type": "CN_CHAR",
"position": 1
},
{
"token": "中国人",
"start_offset": 2,
"end_offset": 5,
"type": "CN_WORD",
"position": 2
},
{
"token": "中国",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 3
},
{
"token": "国人",
"start_offset": 3,
"end_offset": 5,
"type": "CN_WORD",
"position": 4
}
]
}
使用 Java 链接ES,发送请求,获取响应流程
实操代码设置
AnalyzeRequest analyzeRequest = new AnalyzeRequest(INDEX_NAME);
analyzeRequest.text("我爱中国","我喜欢中国");
analyzeRequest.analyzer("ik_smart");
try {
AnalyzeResponse analyzeResponse = restHighLevelClient.indices().analyze(analyzeRequest, RequestOptions.DEFAULT);
List<AnalyzeResponse.AnalyzeToken> tokens = analyzeResponse.getTokens();
tokens.forEach(token -> {
if (!"<NUM>".equals(token.getType()) || token.getTerm().length() > 2) {
String term = token.getTerm();
System.out.prinlnt(term )
}
});
} catch (IOException e) {
e.printStackTrace();
}