Es array object update multi-process conflict

Es structure

{
  "_index": "my_index",
  "_type": "my_type",
  "_id": "1",
  "_version": 26,
  "found": true,
  "_source": {
    "group": "fans",
    "user": [
      {
        "first": "John",
        "last": "Smith"
      },
      {
        "first": "Alice",
        "last": "White"
      }
    ]
  }
}

 


}


==============================
1. Use version control ==
@Test
public void getOneById() {
Client client= elasticsearchTemplate .getClient();
GetResponse getResponse = elasticsearchTemplate.getClient()
.prepareGet() // Prepare to perform the get operation, and then actually perform the get operation. (Difference from direct get)
.setIndex("my_index") // to
query.setType("my_type")
.setId("1")
.get();
long l= getResponse.getVersion();
String sss= getResponse.getSourceAsString();
System.out.println("sss");
EsService esService= new EsService();

String a= getResponse.getSourceAsString();
Supper supper= JSONObject.parseObject(a,Supper.class);
System.out.println(supper.getUser().size());
Use use=new Use();
use.setLast("sbbbbvvs");
use.setFirst("ss");
supper.getUser().add(use);
insertSentimentById("my_index","my_type", supper,"1");
 /* 方法二利用 postscript
HttpEntity entity = new StringEntity(
"{\n" +
" \"script\" : {\n" +
" \"inline\": \"ctx._source.user+=new_tag\",\n" +
" \"params\" : {\n" +
" \"new_tag\" :[{\n" +
" \"first\":\"ddfdd\"\n" +
" ,\"last\":\"22f22\"\n" +
" },\n" +
" {\n" +
" \"first\":\"ddfdf\"\n"+
" ,\"last\":\"2f222\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
esService.putSync("my_index","my_type","1",entity);*/

}





public void insertSentimentById( String indexName,String type, Supper sentiment,String id ) {
//IndexQuery indexQuery = new IndexQueryBuilder().withIndexName(indexName).withType(type).withId(sentiment.getId().toString()).withObject(sentiment).build();
String json= JSONObject.toJSONString(sentiment);
long v=12;
boolean insertFlag= false;
while(true){
try {
elasticsearchTemplate.getClient().prepareIndex(indexName, type).setId(id).setVersion(v).setSource(json).execute().actionGet();
insertFlag=true;
}catch (Exception e){
System.out.println(e.getMessage());;
v=v+1;
}
if(insertFlag){
break;
}
logger.info("===="+v);
}
}
====================================================================================================================================
2、利用post+script

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>rest</artifactId>
<version>5.0.0-rc1</version>
</dependency>
import java.io.IOException;
import java.util.Collections;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseListener;
import org.elasticsearch.client.RestClient;
import org.springframework.stereotype.Service;

/**
* Created by Administrator on 2018/4/25.
*/
public class EsService {

private RestClient restClient = null;
public boolean putSync(String index, String type, String id, HttpEntity entity) {
restClient = RestClient.builder(new HttpHost("192.168.1.3", 9200, "http")).build();
Response indexResponse = null;
try {
indexResponse = restClient.performRequest(
"POST",
"/" + index + "/" + type + "/" + id + "/_update",
Collections.<String, String>emptyMap(),
entity);
} catch (IOException e) {
e.printStackTrace();
}
if (restClient != null) {
try {
restClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return (indexResponse != null);
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324853699&siteId=291194637