java程序 设置elasticsearch7.2 mapping分词器
@Override
public void createIndex(String belonging) throws IOException {
if (belonging!=null&&!"".equals(belonging)) {
if (!existsIndex("knowledge"+belonging)) {
CreateIndexRequest request = new CreateIndexRequest("knowledge"+belonging);
request.settings(Settings.builder()
.put("index.number_of_shards", 3)
.put("index.number_of_replicas", 2)
);
toBulidMapping(request);
//request.mapping(String.valueOf(mapping));
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
log.info("索引创建结查:" + response.isAcknowledged());
} else {
log.warn("索引:{},已经存在,不能再创建。", "knowledge"+belonging);
}
}
}
private static void toBulidMapping(CreateIndexRequest request){
String[] source=new String[]{
"id", "type=integer",
"title", "type=text,analyzer=ik_max_word",
"summary", "type=text,analyzer=ik_max_word",
"source", "type=text",
"author", "type=text,analyzer=ik_max_word",
"belonging", "type=text",
"documentId", "type=integer",
"status", "type=keyword",
"createby", "type=integer",
"createtime", "type=date",
"approveby", "type=integer",
"approvetime", "type=date",
"topping", "type=keyword",
"toppingtime", "type=date",
"textStr","type=text,analyzer=ik_max_word"
};
request.mapping("_doc",source);
}
public boolean existsIndex(String index) throws IOException {
GetIndexRequest request = new GetIndexRequest();
request.indices(index);
log.info("source:" + request.toString());
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
log.debug("existsIndex: " + exists);
return exists;
}
关键点:
源码关键点:



