在网上搜了一大圈,得出的结果要么版本太老,要么压根不能用,没办法,只能自力更生,去ElasticSearch官网寻找答案。
一、创建索引
如果使用curl,按照如下操作。ElasticSearch 7.x不再需要type,所以在后面的json的mappings中,直接设置properties即可。
$ curl -X PUT 'localhost:9200/you_index_name' -d '
{
"mappings": {
"properties": {
"user": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}'
java代码,连接ElasticSearch:
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
创建索引:
/**
* 创建索引,并设置选项
*
* @param index 索引名称
*/
public CreateIndexResponse createIndex(String index) {
CreateIndexRequest request = new CreateIndexRequest(index);
Map<String, Object> user = new HashMap<String, Object>();
user.put("type", "text");
user.put("analyzer", "ik_max_word");
user.put("search_analyzer", "ik_max_word");
Map<String, Object> title = new HashMap<String, Object>();
title.put("type", "text");
title.put("analyzer", "ik_max_word");
title.put("search_analyzer", "ik_max_word");
Map<String, Object> desc = new HashMap<String, Object>();
desc.put("type", "text");
desc.put("analyzer", "ik_max_word");
desc.put("search_analyzer", "ik_max_word");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("title", title);
properties.put("user", user );
properties.put("desc", desc);
Map<String, Object> mapping = new HashMap<String, Object>();
mapping.put("properties", properties);
request.mapping(mapping);
try {
return client.indices().create(request, RequestOptions.DEFAULT);
} catch (IOException e) {
logger.error("Index Creat Error:{}", e.getMessage());
}
return null;
}
java操作有若干种方式,我选择的是以上方式,更多方法可以看这里https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.x/java-rest-high-create-index.html#_index_aliases
二、添加索引
如果是少量插入索引,可以使用以下代码
/**
* 插入索引
*
* @param index 索引名称
* @param pansouEs 要索引的内容,这里使用自己的类进行替换
*/
public IndexResponse insert(String index, PanSouES pansouEs) {
IndexRequest request = new IndexRequest(index);
request.id(pansouES.getPk()); // 设置id
String jsonString = JSON.toJSONString(pansouES);
request.source(jsonString, XContentType.JSON);
logger.info("Json: {}", jsonString);
try {
return client.index(request, RequestOptions.DEFAULT);
} catch (IOException e) {
logger.error("Index insert Error:{}", e.getMessage());
}
return null;
}
此操作可以使用多种方法,详情参见:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.x/java-rest-high-document-index.html
以上操作适用于插入少量索引,如果大量信息使用上述方法,总体耗时将会非常可观。为此,可以使用以下方法操作批量插入索引。
/**
* 插入索引
*
* @param index 索引名称
* @param pansouEsList 要索引的内容,替换PanSouES为你自己的类
*/
public BulkResponse insert(String index, List<PanSouES> pansouEsList) {
BulkRequest request = new BulkRequest();
for (PanSouES panSouES : pansouEsList) {
request.add(new IndexRequest(index).id(panSouES.getPk())
.source(JSON.toJSONString(panSouES), XContentType.JSON));
logger.info("Pk: {} Title: {}", panSouES.getPk(), panSouES.getTitle());
}
try {
return client.bulk(request, RequestOptions.DEFAULT);
} catch (IOException e) {
logger.error("Index insert Error:{}", e.getMessage());
}
return null;
}