upgraded libs, moved to RestHighLevelClient

This commit is contained in:
Mathieu Fortin 2020-04-12 16:20:59 -04:00
parent eb739e3ab2
commit 745d5edda1
5 changed files with 107 additions and 133 deletions

View File

@ -25,6 +25,12 @@
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
@ -32,8 +38,8 @@
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
@ -49,15 +55,9 @@
</dependency>
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
<version>${jts.version}</version>
<exclusions>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</exclusion>
</exclusions>
<groupId>org.locationtech.jts</groupId>
<artifactId>jts-core</artifactId>
<version>1.16.1</version>
</dependency>
<dependency>
@ -66,12 +66,6 @@
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
@ -88,9 +82,9 @@
</dependencies>
<properties>
<spring-data-elasticsearch.version>3.0.8.RELEASE</spring-data-elasticsearch.version>
<spring-data-elasticsearch.version>3.2.6.RELEASE</spring-data-elasticsearch.version>
<jna.version>4.5.2</jna.version>
<elasticsearch.version>5.6.0</elasticsearch.version>
<elasticsearch.version>7.6.2</elasticsearch.version>
<fastjson.version>1.2.47</fastjson.version>
<spatial4j.version>0.6</spatial4j.version>
<jts.version>1.13</jts.version>

View File

@ -1,19 +1,13 @@
package com.baeldung.spring.data.es.config;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@Configuration
@ -21,30 +15,15 @@ import org.springframework.data.elasticsearch.repository.config.EnableElasticsea
@ComponentScan(basePackages = { "com.baeldung.spring.data.es.service" })
public class Config {
@Value("${elasticsearch.home:/usr/local/Cellar/elasticsearch/5.6.0}")
private String elasticsearchHome;
@Value("${elasticsearch.cluster.name:elasticsearch}")
private String clusterName;
@Bean
public Client client() {
TransportClient client = null;
try {
final Settings elasticsearchSettings = Settings.builder()
.put("client.transport.sniff", true)
.put("path.home", elasticsearchHome)
.put("cluster.name", clusterName).build();
client = new PreBuiltTransportClient(elasticsearchSettings);
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
return client;
RestHighLevelClient client() {
ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build();
return RestClients.create(clientConfiguration).rest();
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(client());
return new ElasticsearchRestTemplate(client());
}
}

View File

@ -3,7 +3,6 @@ package com.baeldung.elasticsearch;
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;
@ -11,24 +10,28 @@ import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.DocWriteResponse.Result;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
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.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
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.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Before;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
/**
*
@ -39,7 +42,7 @@ import com.alibaba.fastjson.JSON;
*/
public class ElasticSearchManualTest {
private List<Person> listOfPersons = new ArrayList<>();
private Client client = null;
private RestHighLevelClient client = null;
@Before
public void setUp() throws UnknownHostException {
@ -48,47 +51,44 @@ public class ElasticSearchManualTest {
listOfPersons.add(person1);
listOfPersons.add(person2);
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));
ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build();
client = RestClients.create(clientConfiguration).rest();
}
@Test
public void givenJsonString_whenJavaObject_thenIndexDocument() {
public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception {
String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}";
IndexResponse response = client
.prepareIndex("people", "Doe")
.setSource(jsonObject, XContentType.JSON)
.get();
IndexRequest request = new IndexRequest("people");
request.source(jsonObject, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
String index = response.getIndex();
String type = response.getType();
assertEquals(Result.CREATED, response.getResult());
assertEquals(index, "people");
assertEquals(type, "Doe");
}
@Test
public void givenDocumentId_whenJavaObject_thenDeleteDocument() {
public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception {
String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}";
IndexResponse response = client
.prepareIndex("people", "Doe")
.setSource(jsonObject, XContentType.JSON)
.get();
IndexRequest indexRequest = new IndexRequest("people");
indexRequest.source(jsonObject, XContentType.JSON);
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
String id = response.getId();
DeleteResponse deleteResponse = client
.prepareDelete("people", "Doe", id)
.get();
DeleteRequest deleteRequest = new DeleteRequest("people");
deleteRequest.id(id);
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
assertEquals(Result.DELETED,deleteResponse.getResult());
}
@Test
public void givenSearchRequest_whenMatchAll_thenReturnAllResults() {
SearchResponse response = client
.prepareSearch()
.execute()
.actionGet();
public void givenSearchRequest_whenMatchAll_thenReturnAllResults() throws Exception {
SearchRequest searchRequest = new SearchRequest();
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] searchHits = response
.getHits()
.getHits();
@ -98,42 +98,42 @@ public class ElasticSearchManualTest {
}
@Test
public void givenSearchParameters_thenReturnResults() {
SearchResponse response = client
.prepareSearch()
.setTypes()
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setPostFilter(QueryBuilders
.rangeQuery("age")
.from(5)
.to(15))
.setFrom(0)
.setSize(60)
.setExplain(true)
.execute()
.actionGet();
public void givenSearchParameters_thenReturnResults() throws Exception {
SearchSourceBuilder builder = new SearchSourceBuilder()
.postFilter(QueryBuilders.rangeQuery("age").from(5).to(15))
.from(0)
.size(60)
.explain(true);
SearchResponse response2 = client
.prepareSearch()
.setTypes()
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setPostFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette"))
.setFrom(0)
.setSize(60)
.setExplain(true)
.execute()
.actionGet();
SearchRequest searchRequest = new SearchRequest();
searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH);
searchRequest.source(builder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
builder = new SearchSourceBuilder()
.postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette"))
.from(0)
.size(60)
.explain(true);
searchRequest = new SearchRequest();
searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH);
searchRequest.source(builder);
SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT);
builder = new SearchSourceBuilder()
.postFilter(QueryBuilders.matchQuery("John", "Name*"))
.from(0)
.size(60)
.explain(true);
searchRequest = new SearchRequest();
searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH);
searchRequest.source(builder);
SearchResponse response3 = client.search(searchRequest, RequestOptions.DEFAULT);
SearchResponse response3 = client
.prepareSearch()
.setTypes()
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setPostFilter(QueryBuilders.matchQuery("John", "Name*"))
.setFrom(0)
.setSize(60)
.setExplain(true)
.execute()
.actionGet();
response2.getHits();
response3.getHits();
@ -151,10 +151,11 @@ public class ElasticSearchManualTest {
.field("salary", "11500")
.field("age", "10")
.endObject();
IndexResponse response = client
.prepareIndex("people", "Doe")
.setSource(builder)
.get();
IndexRequest indexRequest = new IndexRequest("people");
indexRequest.source(builder);
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
assertEquals(Result.CREATED, response.getResult());
}

View File

@ -1,4 +1,5 @@
package com.baeldung.elasticsearch;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
@ -12,8 +13,7 @@ import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.geo.builders.ShapeBuilders;
import org.elasticsearch.common.geo.builders.EnvelopeBuilder;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
@ -23,13 +23,13 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.locationtech.jts.geom.Coordinate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.spring.data.es.config.Config;
import com.vividsolutions.jts.geom.Coordinate;
/**
*
@ -76,12 +76,11 @@ public class GeoQueriesManualTest {
.prepareRefresh(WONDERS_OF_WORLD)
.get();
Coordinate topLeft =new Coordinate(74, 31.2);
Coordinate bottomRight =new Coordinate(81.1, 24);
Coordinate topLeft = new Coordinate(74, 31.2);
Coordinate bottomRight = new Coordinate(81.1, 24);
QueryBuilder qb = QueryBuilders
.geoShapeQuery("region", ShapeBuilders.newEnvelope(topLeft, bottomRight))
.relation(ShapeRelation.WITHIN);
.geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight).buildGeometry());
//.relation(ShapeRelation.WITHIN));
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD)
.setTypes(WONDERS)

View File

@ -22,6 +22,7 @@ import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
@ -159,7 +160,7 @@ public class ElasticSearchQueryManualTest {
@Test
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() {
final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags")
.order(Terms.Order.count(false));
.order(BucketOrder.count(false));
final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation)
.execute().actionGet();