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);
} |