es9300修改数据

es9300修改数据


package com.z_esData;

import java.util.concurrent.ExecutionException;

import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class Test20200714 {
	public static void main(String[] args) {
		new Test20200714().doWork();
	}

	private void doWork() {
		Client client = ESUtils.getClient();
		JSONObject json = new JSONObject();
		json.put("dataType", "company");
		json.put("pname", "小草公司");
		json.put("sortTime", 155679788000L);
		json.put("companyType", "小公司");
		json.put("money", 5000);

		// addData(client,json);//添加数据

		// updateData(client,json);//修改指定字段数据01

		// updateDateOld(client,json);//修改指定字段数据02

		// getDataByFiled(client);//获取指定字段数据
		
		
		
		//添加字段
		JSONArray jsonArray=new JSONArray();
		JSONObject json01=new JSONObject();
		json01.put("filedName", "party");
		json01.put("filedType", "string");
		json01.put("ifAnalyzed", "not_analyzed");
		jsonArray.add(json01);
		
		addFiled(client,"company",jsonArray);
	}

	/**
	 * Date:2020年7月14日上午10:29:09
	 */
	private void addFiled(Client client,String indexName,JSONArray jsonArray) {
		XContentBuilder builder = null;

		try {
			builder = XContentFactory.jsonBuilder().startObject().startObject(indexName).startObject("properties");
			 
			for (Object obj : jsonArray) {
				JSONObject jsonObj = JSONObject.parseObject(obj.toString());
				String code = jsonObj.getString("filedName");//字段名称
				String fieldType = jsonObj.getString("filedType");//类型:string、long、int等等
				String storeType = jsonObj.getString("ifAnalyzed");//是否分词 not_analyzed
				
				if (fieldType.equals("date"))
					fieldType = "long";
				builder.startObject(code);
				builder.field("type", fieldType);
				if (storeType != null && storeType.length() > 0)
					builder.field("index", storeType);
				builder.endObject();
			}
			builder.endObject();
			builder.endObject();
			builder.endObject();
			// System.out.println("builder=" + builder.string());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (builder == null)
			return;
		PutMappingRequest mapping = Requests.putMappingRequest(indexName).type(indexName).source(builder);
		client.admin().indices().putMapping(mapping).actionGet();
	}

	/**
	 * 根据指定字段获取数据
	 */
	private void getDataByFiled(Client client) {

		SearchResponse searchResponse = client.prepareSearch("company").setTypes("company")
				.setFetchSource(new String[] { "pname", "money" }, null)
				.setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("companyType", "大公司"))).get();
		SearchHits hits = searchResponse.getHits();
		for (SearchHit s : hits) {
			String jsonStr = s.getSourceAsString();
			JSONObject json = JSONObject.parseObject(jsonStr);
			System.out.println(json.toJSONString());
		}

	}

	/**
	 * 老方法 根据id,修改制定字段值-01
	 */
	private void updateDateOld(Client client, JSONObject json) {
		UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("company", "company", "AXNK-217Y3J-1g4c5GUc");
		updateRequestBuilder.setDoc(json.toJSONString());
		updateRequestBuilder.execute().actionGet();
	}

	/*
	 * 根据id,修改制定字段值-02
	 */
	private void updateData(Client client, JSONObject json) {
		
		JSONObject json = new JSONObject();
		json.put("title", title);
		json.put("lastUpdateDate", new Date());
		json.put("syncStatus", null);

		Client client = ESUtils.getClient();

		UpdateRequest request = new UpdateRequest();
		request.index(weidu) // 索引名
				.type(weidu) // 类型
				.id(esId) // id
				.doc(JSON.toJSONString(json, SerializerFeature.WriteMapNullValue));// 要修改的字段及字段值
		UpdateResponse response = client.update(request).get();
	}
/*
	 * 根据id,修改制定字段值-03
	 */
private void UpdateEs(String weidu, String esId, String title) throws Exception {

		Client client = ESUtils.getClient();

		UpdateRequest request = new UpdateRequest();
		request.index(weidu) // 索引名
				.type(weidu) // 类型
				.id(esId)// id
				.doc(XContentFactory.jsonBuilder().startObject().field("title", title).endObject());// 要修改的字段及字段值
		UpdateResponse response = client.update(request).get();
		

	}

	private void addData(Client client, JSONObject json) {

		client.prepareIndex("company", "company").setSource(json.toJSONString()).execute().actionGet();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值