Elasticsearch 分词,Elasticsearch Java API 分词 操作
- Elasticsearch教程(一),全程直播(小白级别)
- Elasticsearch教程(二),IK分词器安装
- Elasticsearch教程(三),IK分词器安装 (极速版)
- Elasticsearch安装(四), elasticsearch head 插件安装和使用。
- Elasticsearch教程(五) elasticsearch Mapping的创建
- Elasticsearch教程(六) elasticsearch Client创建
- Elasticsearch教程(七) elasticsearch Insert 插入数据(Java)
- Elasticsearch教程(八) elasticsearch delete 删除数据(Java)
- Elasticsearch教程(九) elasticsearch 查询数据 | 分页查询
- Elasticsearch权威指南-中文.pdf
本站有一个新开发的分词工具:Analyzer在线工具,IK Analyzer—在线分词器工具 。就是下面的代码实现的。
Java API的实现方式:
String content = "我是中国人,我爱中国。";
AnalyzeResponse response = ESTools.client.admin().indices()
.prepareAnalyze(content)//内容
.setAnalyzer("ik")//指定分词器
.execute().actionGet();//执行
List<AnalyzeToken> tokens = response.getTokens();
String result = JSONArray.fromObject(tokens).toString();
System.out.println(result);
分词结果为:
[
{
"endOffset": 1,
"position": 0,
"startOffset": 0,
"term": "我",
"type": "CN_CHAR"
},
{
"endOffset": 5,
"position": 1,
"startOffset": 2,
"term": "中国人",
"type": "CN_WORD"
},
{
"endOffset": 4,
"position": 2,
"startOffset": 2,
"term": "中国",
"type": "CN_WORD"
},
{
"endOffset": 5,
"position": 3,
"startOffset": 3,
"term": "国人",
"type": "CN_WORD"
},
{
"endOffset": 7,
"position": 4,
"startOffset": 6,
"term": "我",
"type": "CN_CHAR"
},
{
"endOffset": 8,
"position": 5,
"startOffset": 7,
"term": "爱",
"type": "CN_CHAR"
},
{
"endOffset": 10,
"position": 6,
"startOffset": 8,
"term": "中国",
"type": "CN_WORD"
}
]
HTTP API 方式:
http://192.168.0.1:9200/_analyze?analyzer=ik&pretty=true&text=我是中国人,我爱中国
返回的结果为:
{
"tokens" : [ {
"token" : "我",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_CHAR",
"position" : 0
}, {
"token" : "中国人",
"start_offset" : 2,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 1
}, {
"token" : "中国",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 2
}, {
"token" : "国人",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 3
}, {
"token" : "我",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_CHAR",
"position" : 4
}, {
"token" : "爱",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_CHAR",
"position" : 5
}, {
"token" : "中国",
"start_offset" : 8,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 6
} ]
}
都是可以的。
版权所属:SO JSON在线解析
原文地址:https://www.sojson.com/blog/125.html
转载时必须以链接形式注明原始出处及本声明。
本文主题:
如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。