标题:
es6.2.4学习学习----ik分词器java 更新2
[打印本页]
作者:
look_w
时间:
2019-3-8 19:27
标题:
es6.2.4学习学习----ik分词器java 更新2
1、首先新建索引ik_java
ik_java索引包含
title
使用standard分词器
title.title_ik_max_word
使用ik_max_word分词器
title.title_ik_smart
使用ik_smart分词器
java代码为
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient
.builder(new HttpHost("192.168.16.21", 9200, "http"), new HttpHost("192.168.16.22", 9200, "http")));
// 创建索引
createIndex(client);
client.close();
}
public static void createIndex(RestHighLevelClient client) throws IOException {
CreateIndexRequest req = new CreateIndexRequest("ik_java");
req.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2));
XContentBuilder mappings = JsonXContent.contentBuilder().startObject()
.startObject("properties")
.startObject("title")//默认使用standard分词器
.field("type", "text")
.startObject("fields")
.startObject("title_ik_smart")
.field("type", "text")
.field("analyzer", "ik_smart")//使用ik_smart分词器
.endObject()
.startObject("title_ik_max_word")
.field("type", "text")
.field("analyzer", "ik_max_word")//使用ik_max_word分词器
.endObject()
.endObject()
.endObject()
.endObject().endObject();
req.mapping("doc", mappings);
client.indices().create(req);
}
对应DSL
PUT ik_java
{
"mappings": {
"doc": {
"properties": {
"title": {
"type": "text",
"fields": {
"title_ik_max_word": {
"type": "text",
"analyzer": "ik_max_word"
},
"title_ik_smart": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
}
},
"settings": {
"number_of_shards": "3",
"number_of_replicas": "2"
}
}
2、添加文档到索引中
添加
“
好好学习,天天向上
”,
“
学和习,有什么区别
”,
“
es的分词该怎么学的
”
三个文档
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient
.builder(new HttpHost("192.168.16.21", 9200, "http"), new HttpHost("192.168.16.22", 9200, "http")));
//添加文档
indexDoc(client, "好好学习,天天向上");
indexDoc(client, "学和习,有什么区别");
indexDoc(client, "es的分词该怎么学的");
client.close();
}
public static void indexDoc(RestHighLevelClient client, String title) throws IOException {
IndexRequest req = new IndexRequest("ik_java", "doc");
XContentBuilder contentBuilder = XContentFactory.jsonBuilder().startObject()
.field("title", title)
.endObject();
req.source(contentBuilder);
client.index(req);
}
对应DSL
POST /ik_java/doc
{
"title":"好好学习,天天向上"
}
3、查询
查询ik_java中与“
学习
”相关的文档
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient
.builder(new HttpHost("192.168.16.21", 9200, "http"), new HttpHost("192.168.16.22", 9200, "http")));
//查询文档
searchDoc(client, "title", "学习");
searchDoc(client, "title.title_ik_smart", "学习");
searchDoc(client, "title.title_ik_max_word", "学习");
client.close();
}
public static void searchDoc(RestHighLevelClient client, String field, String value) throws IOException {
SearchRequest req = new SearchRequest("ik_java");
SearchSourceBuilder ssb = new SearchSourceBuilder();
ssb.query(QueryBuilders.matchQuery(field, value));//查询field包含value的文档
req.source(ssb);
SearchResponse resp = client.search(req);
System.out.println(field+" : "+resp);
}
对应DSL
GET /ik_java/doc/_search
{
"query": {
"match": {
"title": "学习"
}
}
}
GET /ik_java/doc/_search
{
"query": {
"match": {
"title.title_ik_smart": "学习"
}
}
}
GET /ik_java/doc/_search
{
"query": {
"match": {
"title.title_ik_max_word": "学习"
}
}
}
查询结果:
title匹配到3个文档
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0