update spring data elasticsearch

This commit is contained in:
DOHA
2018-07-28 02:27:47 +03:00
parent 387b92b09a
commit bac0c0714c
11 changed files with 208 additions and 194 deletions

View File

@@ -1,45 +1,49 @@
package com.baeldung.elasticsearch;
import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.elasticsearch.search.SearchHit;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.elasticsearch.action.DocWriteResponse.Result;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
public class ElasticSearchManualTest {
private List<Person> listOfPersons = new ArrayList<>();
private Client client = null;
@Before
public void setUp() {
public void setUp() throws UnknownHostException {
Person person1 = new Person(10, "John Doe", new Date());
Person person2 = new Person(25, "Janette Doe", new Date());
listOfPersons.add(person1);
listOfPersons.add(person2);
Node node = nodeBuilder()
.clusterName("elasticsearch")
.client(true)
.node();
client = node.client();
client = new PreBuiltTransportClient(Settings.builder().put("client.transport.sniff", true)
.put("cluster.name","elasticsearch").build())
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
}
@Test
@@ -47,11 +51,12 @@ public class ElasticSearchManualTest {
String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}";
IndexResponse response = client
.prepareIndex("people", "Doe")
.setSource(jsonObject)
.setSource(jsonObject, XContentType.JSON)
.get();
String index = response.getIndex();
String type = response.getType();
assertTrue(response.isCreated());
assertEquals(Result.CREATED, response.getResult());
assertEquals(index, "people");
assertEquals(type, "Doe");
}
@@ -61,13 +66,14 @@ public class ElasticSearchManualTest {
String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}";
IndexResponse response = client
.prepareIndex("people", "Doe")
.setSource(jsonObject)
.setSource(jsonObject, XContentType.JSON)
.get();
String id = response.getId();
DeleteResponse deleteResponse = client
.prepareDelete("people", "Doe", id)
.get();
assertTrue(deleteResponse.isFound());
assertEquals(Result.DELETED,deleteResponse.getResult());
}
@Test
@@ -142,6 +148,7 @@ public class ElasticSearchManualTest {
.prepareIndex("people", "Doe")
.setSource(builder)
.get();
assertTrue(response.isCreated());
assertEquals(Result.CREATED, response.getResult());
}
}