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();
}
}
版权声明:本文为weixin_37630333原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。