Java对ElasticSearch 7.x的简单操作【一】

在网上搜了一大圈,得出的结果要么版本太老,要么压根不能用,没办法,只能自力更生,去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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值