Merge pull request #9323 from mathieufortin01/master

BAEL-3676 Fix failing manual test in sping-data-elasticsearch module
This commit is contained in:
Loredana Crusoveanu 2020-06-01 09:06:10 +03:00 committed by GitHub
commit b2670e6e98
10 changed files with 419 additions and 491 deletions

View File

@ -15,13 +15,7 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId> <artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version> <version>${spring.version}</version>
</dependency> </dependency>
@ -36,6 +30,7 @@
<artifactId>elasticsearch</artifactId> <artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version> <version>${elasticsearch.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
@ -49,8 +44,8 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.vividsolutions</groupId> <groupId>org.locationtech.jts</groupId>
<artifactId>jts</artifactId> <artifactId>jts-core</artifactId>
<version>${jts.version}</version> <version>${jts.version}</version>
<exclusions> <exclusions>
<exclusion> <exclusion>
@ -60,41 +55,19 @@
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>
<version>${spring.version}</version> <version>${spring.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>${jna.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
<spring-data-elasticsearch.version>3.0.8.RELEASE</spring-data-elasticsearch.version> <spring-data-elasticsearch.version>4.0.0.RELEASE</spring-data-elasticsearch.version>
<jna.version>4.5.2</jna.version> <elasticsearch.version>7.6.2</elasticsearch.version>
<elasticsearch.version>5.6.0</elasticsearch.version>
<fastjson.version>1.2.47</fastjson.version> <fastjson.version>1.2.47</fastjson.version>
<spatial4j.version>0.6</spatial4j.version> <spatial4j.version>0.7</spatial4j.version>
<jts.version>1.13</jts.version> <jts.version>1.15.0</jts.version>
<log4j.version>2.9.1</log4j.version>
</properties> </properties>
</project> </project>

View File

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

View File

@ -1,7 +1,12 @@
package com.baeldung.spring.data.es.model; package com.baeldung.spring.data.es.model;
import static org.springframework.data.elasticsearch.annotations.FieldType.Text;
import org.springframework.data.elasticsearch.annotations.Field;
public class Author { public class Author {
@Field(type = Text)
private String name; private String name;
public Author() { public Author() {

View File

@ -1,28 +0,0 @@
package com.baeldung.spring.data.es.service;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.baeldung.spring.data.es.model.Article;
public interface ArticleService {
Article save(Article article);
Optional<Article> findOne(String id);
Iterable<Article> findAll();
Page<Article> findByAuthorName(String name, Pageable pageable);
Page<Article> findByAuthorNameUsingCustomQuery(String name, Pageable pageable);
Page<Article> findByFilteredTagQuery(String tag, Pageable pageable);
Page<Article> findByAuthorsNameAndFilteredTagQuery(String name, String tag, Pageable pageable);
long count();
void delete(Article article);
}

View File

@ -1,67 +0,0 @@
package com.baeldung.spring.data.es.service;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.baeldung.spring.data.es.model.Article;
import com.baeldung.spring.data.es.repository.ArticleRepository;
@Service
public class ArticleServiceImpl implements ArticleService {
private final ArticleRepository articleRepository;
@Autowired
public ArticleServiceImpl(ArticleRepository articleRepository) {
this.articleRepository = articleRepository;
}
@Override
public Article save(Article article) {
return articleRepository.save(article);
}
@Override
public Optional<Article> findOne(String id) {
return articleRepository.findById(id);
}
@Override
public Iterable<Article> findAll() {
return articleRepository.findAll();
}
@Override
public Page<Article> findByAuthorName(String name, Pageable pageable) {
return articleRepository.findByAuthorsName(name, pageable);
}
@Override
public Page<Article> findByAuthorNameUsingCustomQuery(String name, Pageable pageable) {
return articleRepository.findByAuthorsNameUsingCustomQuery(name, pageable);
}
@Override
public Page<Article> findByFilteredTagQuery(String tag, Pageable pageable) {
return articleRepository.findByFilteredTagQuery(tag, pageable);
}
@Override
public Page<Article> findByAuthorsNameAndFilteredTagQuery(String name, String tag, Pageable pageable) {
return articleRepository.findByAuthorsNameAndFilteredTagQuery(name, tag, pageable);
}
@Override
public long count() {
return articleRepository.count();
}
@Override
public void delete(Article article) {
articleRepository.delete(article);
}
}

View File

@ -8,10 +8,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.spring.data.es.config.Config; import com.baeldung.spring.data.es.config.Config;
/** /**
* This Manual test requires: Elasticsearch instance running on localhost:9200.
* *
* This Manual test requires: * The following docker command can be used: docker run -d --name es762 -p
* * Elasticsearch instance running on host * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2
*
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class) @ContextConfiguration(classes = Config.class)

View File

@ -3,43 +3,48 @@ package com.baeldung.elasticsearch;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.DocWriteResponse.Result; import org.elasticsearch.action.DocWriteResponse.Result;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client; import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import com.alibaba.fastjson.JSON; import org.springframework.data.elasticsearch.client.RestClients;
/** /**
* This Manual test requires: Elasticsearch instance running on localhost:9200.
* *
* This Manual test requires: * The following docker command can be used: docker run -d --name es762 -p
* * Elasticsearch instance running on host * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2
* * with cluster name = elasticsearch
*
*/ */
public class ElasticSearchManualTest { public class ElasticSearchManualTest {
private List<Person> listOfPersons = new ArrayList<>(); private List<Person> listOfPersons = new ArrayList<>();
private Client client = null; private RestHighLevelClient client = null;
@Before @Before
public void setUp() throws UnknownHostException { public void setUp() throws UnknownHostException {
@ -48,113 +53,120 @@ public class ElasticSearchManualTest {
listOfPersons.add(person1); listOfPersons.add(person1);
listOfPersons.add(person2); listOfPersons.add(person2);
client = new PreBuiltTransportClient(Settings.builder().put("client.transport.sniff", true) ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.put("cluster.name","elasticsearch").build()) .connectedTo("localhost:9200")
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); .build();
client = RestClients.create(clientConfiguration)
.rest();
} }
@Test @Test
public void givenJsonString_whenJavaObject_thenIndexDocument() { public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception {
String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}";
IndexResponse response = client IndexRequest request = new IndexRequest("people");
.prepareIndex("people", "Doe") request.source(jsonObject, XContentType.JSON);
.setSource(jsonObject, XContentType.JSON)
.get(); IndexResponse response = client.index(request, RequestOptions.DEFAULT);
String index = response.getIndex(); String index = response.getIndex();
String type = response.getType(); long version = response.getVersion();
assertEquals(Result.CREATED, response.getResult()); assertEquals(Result.CREATED, response.getResult());
assertEquals(index, "people"); assertEquals(1, version);
assertEquals(type, "Doe"); assertEquals("people", index);
} }
@Test @Test
public void givenDocumentId_whenJavaObject_thenDeleteDocument() { public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception {
String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}";
IndexResponse response = client IndexRequest indexRequest = new IndexRequest("people");
.prepareIndex("people", "Doe") indexRequest.source(jsonObject, XContentType.JSON);
.setSource(jsonObject, XContentType.JSON)
.get(); IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
String id = response.getId(); String id = response.getId();
DeleteResponse deleteResponse = client
.prepareDelete("people", "Doe", id) GetRequest getRequest = new GetRequest("people");
.get(); getRequest.id(id);
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(getResponse.getSourceAsString());
DeleteRequest deleteRequest = new DeleteRequest("people");
deleteRequest.id(id);
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
assertEquals(Result.DELETED, deleteResponse.getResult()); assertEquals(Result.DELETED, deleteResponse.getResult());
} }
@Test @Test
public void givenSearchRequest_whenMatchAll_thenReturnAllResults() { public void givenSearchRequest_whenMatchAll_thenReturnAllResults() throws Exception {
SearchResponse response = client SearchRequest searchRequest = new SearchRequest();
.prepareSearch() SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
.execute() SearchHit[] searchHits = response.getHits()
.actionGet();
SearchHit[] searchHits = response
.getHits()
.getHits(); .getHits();
List<Person> results = Arrays.stream(searchHits) List<Person> results = Arrays.stream(searchHits)
.map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class))
.collect(Collectors.toList()); .collect(Collectors.toList());
results.forEach(System.out::println);
} }
@Test @Test
public void givenSearchParameters_thenReturnResults() { public void givenSearchParameters_thenReturnResults() throws Exception {
SearchResponse response = client SearchSourceBuilder builder = new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age")
.prepareSearch()
.setTypes()
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setPostFilter(QueryBuilders
.rangeQuery("age")
.from(5) .from(5)
.to(15)) .to(15));
.setFrom(0)
.setSize(60)
.setExplain(true)
.execute()
.actionGet();
SearchResponse response2 = client SearchRequest searchRequest = new SearchRequest();
.prepareSearch() searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH);
.setTypes() searchRequest.source(builder);
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setPostFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")) SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
.setFrom(0)
.setSize(60) builder = new SearchSourceBuilder().postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette"));
.setExplain(true)
.execute() searchRequest = new SearchRequest();
.actionGet(); 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*"));
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(); response2.getHits();
response3.getHits(); response3.getHits();
final List<Person> results = Arrays.stream(response.getHits().getHits()) final List<Person> results = Stream.of(response.getHits()
.getHits(),
response2.getHits()
.getHits(),
response3.getHits()
.getHits())
.flatMap(Arrays::stream)
.map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class))
.collect(Collectors.toList()); .collect(Collectors.toList());
results.forEach(System.out::println);
} }
@Test @Test
public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException { public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException {
XContentBuilder builder = XContentFactory XContentBuilder builder = XContentFactory.jsonBuilder()
.jsonBuilder()
.startObject() .startObject()
.field("fullName", "Test") .field("fullName", "Test")
.field("salary", "11500") .field("salary", "11500")
.field("age", "10") .field("age", "10")
.endObject(); .endObject();
IndexResponse response = client
.prepareIndex("people", "Doe") IndexRequest indexRequest = new IndexRequest("people");
.setSource(builder) indexRequest.source(builder);
.get();
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
assertEquals(Result.CREATED, response.getResult()); assertEquals(Result.CREATED, response.getResult());
} }

View File

@ -1,4 +1,5 @@
package com.baeldung.elasticsearch; package com.baeldung.elasticsearch;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.IOException; import java.io.IOException;
@ -7,87 +8,86 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import com.baeldung.spring.data.es.config.Config;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.ShapeRelation; 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.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.GeoShapeQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.locationtech.jts.geom.Coordinate;
import org.springframework.beans.factory.annotation.Autowired; 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.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.spring.data.es.config.Config;
import com.vividsolutions.jts.geom.Coordinate;
/** /**
* This Manual test requires: Elasticsearch instance running on localhost:9200.
* *
* This Manual test requires: * The following docker command can be used: docker run -d --name es762 -p
* * Elasticsearch instance running on host * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2
* * with cluster name = elasticsearch
* * and further configurations
*
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class) @ContextConfiguration(classes = Config.class)
public class GeoQueriesManualTest { public class GeoQueriesManualTest {
private static final String WONDERS_OF_WORLD = "wonders-of-world"; private static final String WONDERS_OF_WORLD = "wonders-of-world";
private static final String WONDERS = "Wonders";
@Autowired @Autowired
private ElasticsearchTemplate elasticsearchTemplate; private RestHighLevelClient client;
@Autowired
private Client client;
@Before @Before
public void setUp() { public void setUp() throws Exception {
String jsonObject = "{\"Wonders\":{\"properties\":{\"name\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"region\":{\"type\":\"geo_shape\",\"tree\":\"quadtree\",\"precision\":\"1m\"},\"location\":{\"type\":\"geo_point\"}}}}"; String jsonObject = "{\"properties\":{\"name\":{\"type\":\"text\",\"index\":false},\"region\":{\"type\":\"geo_shape\"},\"location\":{\"type\":\"geo_point\"}}}";
CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD); CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD);
req.mapping(WONDERS, jsonObject, XContentType.JSON); req.mapping(jsonObject, XContentType.JSON);
client.admin()
.indices() client.indices()
.create(req) .create(req, RequestOptions.DEFAULT);
.actionGet();
} }
@Test @Test
public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() throws IOException { public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() throws IOException {
String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,30.2],[80.1, 25]]}}"; String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,30.2],[80.1, 25]]}}";
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD);
.setSource(jsonObject, XContentType.JSON) indexRequest.source(jsonObject, XContentType.JSON);
.get(); IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
String tajMahalId = response.getId(); String tajMahalId = response.getId();
client.admin()
.indices() RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
.prepareRefresh(WONDERS_OF_WORLD) client.indices()
.get(); .refresh(refreshRequest, RequestOptions.DEFAULT);
Coordinate topLeft = new Coordinate(74, 31.2); Coordinate topLeft = new Coordinate(74, 31.2);
Coordinate bottomRight = new Coordinate(81.1, 24); Coordinate bottomRight = new Coordinate(81.1, 24);
QueryBuilder qb = QueryBuilders
.geoShapeQuery("region", ShapeBuilders.newEnvelope(topLeft, bottomRight))
.relation(ShapeRelation.WITHIN);
GeoShapeQueryBuilder qb = QueryBuilders.geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight).buildGeometry());
qb.relation(ShapeRelation.INTERSECTS);
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
.setTypes(WONDERS) SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD);
.setQuery(qb) searchRequest.source(source);
.execute()
.actionGet(); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
List<String> ids = Arrays.stream(searchResponse.getHits() List<String> ids = Arrays.stream(searchResponse.getHits()
.getHits()) .getHits())
@ -98,25 +98,28 @@ public class GeoQueriesManualTest {
} }
@Test @Test
public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() { public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() throws Exception {
String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}"; String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}";
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS)
.setSource(jsonObject, XContentType.JSON) IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD);
.get(); indexRequest.source(jsonObject, XContentType.JSON);
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
String pyramidsOfGizaId = response.getId(); String pyramidsOfGizaId = response.getId();
client.admin()
.indices() RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
.prepareRefresh(WONDERS_OF_WORLD) client.indices()
.get(); .refresh(refreshRequest, RequestOptions.DEFAULT);
QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location") QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location")
.setCorners(31, 30, 28, 32); .setCorners(31, 30, 28, 32);
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
.setTypes(WONDERS) SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD);
.setQuery(qb) searchRequest.source(source);
.execute()
.actionGet(); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
List<String> ids = Arrays.stream(searchResponse.getHits() List<String> ids = Arrays.stream(searchResponse.getHits()
.getHits()) .getHits())
.map(SearchHit::getId) .map(SearchHit::getId)
@ -125,26 +128,29 @@ public class GeoQueriesManualTest {
} }
@Test @Test
public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() { public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() throws Exception {
String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}"; String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}";
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS)
.setSource(jsonObject, XContentType.JSON) IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD);
.get(); indexRequest.source(jsonObject, XContentType.JSON);
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
String lighthouseOfAlexandriaId = response.getId(); String lighthouseOfAlexandriaId = response.getId();
client.admin()
.indices() RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
.prepareRefresh(WONDERS_OF_WORLD) client.indices()
.get(); .refresh(refreshRequest, RequestOptions.DEFAULT);
QueryBuilder qb = QueryBuilders.geoDistanceQuery("location") QueryBuilder qb = QueryBuilders.geoDistanceQuery("location")
.point(29.976, 31.131) .point(29.976, 31.131)
.distance(10, DistanceUnit.MILES); .distance(10, DistanceUnit.MILES);
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
.setTypes(WONDERS) SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD);
.setQuery(qb) searchRequest.source(source);
.execute()
.actionGet(); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
List<String> ids = Arrays.stream(searchResponse.getHits() List<String> ids = Arrays.stream(searchResponse.getHits()
.getHits()) .getHits())
.map(SearchHit::getId) .map(SearchHit::getId)
@ -153,16 +159,18 @@ public class GeoQueriesManualTest {
} }
@Test @Test
public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() { public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() throws Exception {
String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}"; String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}";
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS)
.setSource(jsonObject, XContentType.JSON) IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD);
.get(); indexRequest.source(jsonObject, XContentType.JSON);
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
String greatRannOfKutchid = response.getId(); String greatRannOfKutchid = response.getId();
client.admin()
.indices() RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
.prepareRefresh(WONDERS_OF_WORLD) client.indices()
.get(); .refresh(refreshRequest, RequestOptions.DEFAULT);
List<GeoPoint> allPoints = new ArrayList<GeoPoint>(); List<GeoPoint> allPoints = new ArrayList<GeoPoint>();
allPoints.add(new GeoPoint(22.733, 68.859)); allPoints.add(new GeoPoint(22.733, 68.859));
@ -170,11 +178,12 @@ public class GeoQueriesManualTest {
allPoints.add(new GeoPoint(23, 70.859)); allPoints.add(new GeoPoint(23, 70.859));
QueryBuilder qb = QueryBuilders.geoPolygonQuery("location", allPoints); QueryBuilder qb = QueryBuilders.geoPolygonQuery("location", allPoints);
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
.setTypes(WONDERS) SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD);
.setQuery(qb) searchRequest.source(source);
.execute()
.actionGet(); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
List<String> ids = Arrays.stream(searchResponse.getHits() List<String> ids = Arrays.stream(searchResponse.getHits()
.getHits()) .getHits())
.map(SearchHit::getId) .map(SearchHit::getId)
@ -183,7 +192,9 @@ public class GeoQueriesManualTest {
} }
@After @After
public void destroy() { public void destroy() throws Exception {
elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD); DeleteIndexRequest deleteIndex = new DeleteIndexRequest(WONDERS_OF_WORLD);
client.indices()
.delete(deleteIndex, RequestOptions.DEFAULT);
} }
} }

View File

@ -10,68 +10,71 @@ import static org.junit.Assert.assertNotNull;
import java.util.List; import java.util.List;
import com.baeldung.spring.data.es.config.Config;
import com.baeldung.spring.data.es.model.Article;
import com.baeldung.spring.data.es.model.Author;
import com.baeldung.spring.data.es.repository.ArticleRepository;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery; import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.spring.data.es.config.Config;
import com.baeldung.spring.data.es.model.Article;
import com.baeldung.spring.data.es.model.Author;
import com.baeldung.spring.data.es.service.ArticleService;
/** /**
* This Manual test requires: Elasticsearch instance running on localhost:9200.
* *
* This Manual test requires: * The following docker command can be used: docker run -d --name es762 -p
* * Elasticsearch instance running on host * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2
* * with cluster name = elasticsearch
*
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class) @ContextConfiguration(classes = Config.class)
public class ElasticSearchManualTest { public class ElasticSearchManualTest {
@Autowired @Autowired
private ElasticsearchTemplate elasticsearchTemplate; private ElasticsearchRestTemplate elasticsearchTemplate;
@Autowired @Autowired
private ArticleService articleService; private ArticleRepository articleRepository;
private final Author johnSmith = new Author("John Smith"); private final Author johnSmith = new Author("John Smith");
private final Author johnDoe = new Author("John Doe"); private final Author johnDoe = new Author("John Doe");
@Before @Before
public void before() { public void before() {
elasticsearchTemplate.deleteIndex(Article.class);
elasticsearchTemplate.createIndex(Article.class);
// don't call putMapping() to test the default mappings
Article article = new Article("Spring Data Elasticsearch"); Article article = new Article("Spring Data Elasticsearch");
article.setAuthors(asList(johnSmith, johnDoe)); article.setAuthors(asList(johnSmith, johnDoe));
article.setTags("elasticsearch", "spring data"); article.setTags("elasticsearch", "spring data");
articleService.save(article); articleRepository.save(article);
article = new Article("Search engines"); article = new Article("Search engines");
article.setAuthors(asList(johnDoe)); article.setAuthors(asList(johnDoe));
article.setTags("search engines", "tutorial"); article.setTags("search engines", "tutorial");
articleService.save(article); articleRepository.save(article);
article = new Article("Second Article About Elasticsearch"); article = new Article("Second Article About Elasticsearch");
article.setAuthors(asList(johnSmith)); article.setAuthors(asList(johnSmith));
article.setTags("elasticsearch", "spring data"); article.setTags("elasticsearch", "spring data");
articleService.save(article); articleRepository.save(article);
article = new Article("Elasticsearch Tutorial"); article = new Article("Elasticsearch Tutorial");
article.setAuthors(asList(johnDoe)); article.setAuthors(asList(johnDoe));
article.setTags("elasticsearch"); article.setTags("elasticsearch");
articleService.save(article); articleRepository.save(article);
}
@After
public void after() {
articleRepository.deleteAll();
} }
@Test @Test
@ -81,82 +84,85 @@ public class ElasticSearchManualTest {
Article article = new Article("Making Search Elastic"); Article article = new Article("Making Search Elastic");
article.setAuthors(authors); article.setAuthors(authors);
article = articleService.save(article); article = articleRepository.save(article);
assertNotNull(article.getId()); assertNotNull(article.getId());
} }
@Test @Test
public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() { public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() {
final Page<Article> articleByAuthorName = articleRepository.findByAuthorsName(johnSmith.getName(), PageRequest.of(0, 10));
final Page<Article> articleByAuthorName = articleService
.findByAuthorName(johnSmith.getName(), PageRequest.of(0, 10));
assertEquals(2L, articleByAuthorName.getTotalElements()); assertEquals(2L, articleByAuthorName.getTotalElements());
} }
@Test @Test
public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() { public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() {
final Page<Article> articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("Smith", PageRequest.of(0, 10)); final Page<Article> articleByAuthorName = articleRepository.findByAuthorsNameUsingCustomQuery("Smith", PageRequest.of(0, 10));
assertEquals(2L, articleByAuthorName.getTotalElements()); assertEquals(2L, articleByAuthorName.getTotalElements());
} }
@Test @Test
public void givenTagFilterQuery_whenSearchByTag_thenArticleIsFound() { public void givenTagFilterQuery_whenSearchByTag_thenArticleIsFound() {
final Page<Article> articleByAuthorName = articleService.findByFilteredTagQuery("elasticsearch", PageRequest.of(0, 10)); final Page<Article> articleByAuthorName = articleRepository.findByFilteredTagQuery("elasticsearch", PageRequest.of(0, 10));
assertEquals(3L, articleByAuthorName.getTotalElements()); assertEquals(3L, articleByAuthorName.getTotalElements());
} }
@Test @Test
public void givenTagFilterQuery_whenSearchByAuthorsName_thenArticleIsFound() { public void givenTagFilterQuery_whenSearchByAuthorsName_thenArticleIsFound() {
final Page<Article> articleByAuthorName = articleService.findByAuthorsNameAndFilteredTagQuery("Doe", "elasticsearch", PageRequest.of(0, 10)); final Page<Article> articleByAuthorName = articleRepository.findByAuthorsNameAndFilteredTagQuery("Doe", "elasticsearch", PageRequest.of(0, 10));
assertEquals(2L, articleByAuthorName.getTotalElements()); assertEquals(2L, articleByAuthorName.getTotalElements());
} }
@Test @Test
public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() { public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() {
final Query searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*"))
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*"))
.build(); .build();
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(1, articles.size()); final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.getTotalHits());
} }
@Test @Test
public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() { public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() {
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")).build(); final Query searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch"))
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); .build();
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.size()); assertEquals(1, articles.getTotalHits());
final Article article = articles.get(0); final Article article = articles.getSearchHit(0)
.getContent();
final String newTitle = "Getting started with Search Engines"; final String newTitle = "Getting started with Search Engines";
article.setTitle(newTitle); article.setTitle(newTitle);
articleService.save(article); articleRepository.save(article);
assertEquals(newTitle, articleService.findOne(article.getId()).get().getTitle()); assertEquals(newTitle, articleRepository.findById(article.getId())
.get()
.getTitle());
} }
@Test @Test
public void givenSavedDoc_whenDelete_thenRemovedFromIndex() { public void givenSavedDoc_whenDelete_thenRemovedFromIndex() {
final String articleTitle = "Spring Data Elasticsearch"; final String articleTitle = "Spring Data Elasticsearch";
final SearchQuery searchQuery = new NativeSearchQueryBuilder() final Query searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%"))
.withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); .build();
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.size());
final long count = articleService.count();
articleService.delete(articles.get(0)); assertEquals(1, articles.getTotalHits());
final long count = articleRepository.count();
assertEquals(count - 1, articleService.count()); articleRepository.delete(articles.getSearchHit(0)
.getContent());
assertEquals(count - 1, articleRepository.count());
} }
@Test @Test
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() { public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
final SearchQuery searchQuery = new NativeSearchQueryBuilder() final Query searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND))
.withQuery(matchQuery("title", "Search engines").operator(AND)).build(); .build();
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.size()); assertEquals(1, articles.getTotalHits());
} }
} }

View File

@ -2,7 +2,6 @@ package com.baeldung.spring.data.es;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static org.elasticsearch.index.query.Operator.AND;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery; import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery; import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
@ -14,142 +13,164 @@ import static org.junit.Assert.assertEquals;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.baeldung.spring.data.es.config.Config;
import com.baeldung.spring.data.es.model.Article;
import com.baeldung.spring.data.es.model.Author;
import com.baeldung.spring.data.es.repository.ArticleRepository;
import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.MultiMatchQueryBuilder; import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders; 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.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.spring.data.es.config.Config;
import com.baeldung.spring.data.es.model.Article;
import com.baeldung.spring.data.es.model.Author;
import com.baeldung.spring.data.es.service.ArticleService;
/** /**
* This Manual test requires: Elasticsearch instance running on localhost:9200.
* *
* This Manual test requires: * The following docker command can be used: docker run -d --name es762 -p
* * Elasticsearch instance running on host * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2
* * with cluster name = elasticsearch
*
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class) @ContextConfiguration(classes = Config.class)
public class ElasticSearchQueryManualTest { public class ElasticSearchQueryManualTest {
@Autowired @Autowired
private ElasticsearchTemplate elasticsearchTemplate; private ElasticsearchRestTemplate elasticsearchTemplate;
@Autowired @Autowired
private ArticleService articleService; private ArticleRepository articleRepository;
@Autowired @Autowired
private Client client; private RestHighLevelClient client;
private final Author johnSmith = new Author("John Smith"); private final Author johnSmith = new Author("John Smith");
private final Author johnDoe = new Author("John Doe"); private final Author johnDoe = new Author("John Doe");
@Before @Before
public void before() { public void before() {
elasticsearchTemplate.deleteIndex(Article.class);
elasticsearchTemplate.createIndex(Article.class);
elasticsearchTemplate.putMapping(Article.class);
elasticsearchTemplate.refresh(Article.class);
Article article = new Article("Spring Data Elasticsearch"); Article article = new Article("Spring Data Elasticsearch");
article.setAuthors(asList(johnSmith, johnDoe)); article.setAuthors(asList(johnSmith, johnDoe));
article.setTags("elasticsearch", "spring data"); article.setTags("elasticsearch", "spring data");
articleService.save(article); articleRepository.save(article);
article = new Article("Search engines"); article = new Article("Search engines");
article.setAuthors(asList(johnDoe)); article.setAuthors(asList(johnDoe));
article.setTags("search engines", "tutorial"); article.setTags("search engines", "tutorial");
articleService.save(article); articleRepository.save(article);
article = new Article("Second Article About Elasticsearch"); article = new Article("Second Article About Elasticsearch");
article.setAuthors(asList(johnSmith)); article.setAuthors(asList(johnSmith));
article.setTags("elasticsearch", "spring data"); article.setTags("elasticsearch", "spring data");
articleService.save(article); articleRepository.save(article);
article = new Article("Elasticsearch Tutorial"); article = new Article("Elasticsearch Tutorial");
article.setAuthors(asList(johnDoe)); article.setAuthors(asList(johnDoe));
article.setTags("elasticsearch"); article.setTags("elasticsearch");
articleService.save(article); articleRepository.save(article);
}
@After
public void after() {
articleRepository.deleteAll();
} }
@Test @Test
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
final SearchQuery searchQuery = new NativeSearchQueryBuilder() final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(Operator.AND))
.withQuery(matchQuery("title", "Search engines").operator(AND)).build(); .build();
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.size()); assertEquals(1, articles.getTotalHits());
} }
@Test @Test
public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() {
final SearchQuery searchQuery = new NativeSearchQueryBuilder() final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions"))
.withQuery(matchQuery("title", "Engines Solutions")).build(); .build();
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(1, articles.size()); final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals("Search engines", articles.get(0).getTitle());
assertEquals(1, articles.getTotalHits());
assertEquals("Search engines", articles.getSearchHit(0)
.getContent()
.getTitle());
} }
@Test @Test
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
final SearchQuery searchQuery = new NativeSearchQueryBuilder() final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data"))
.withQuery(matchQuery("title", "elasticsearch data")).build(); .build();
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(3, articles.size()); final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(3, articles.getTotalHits());
} }
@Test @Test
public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() { public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() {
SearchQuery searchQuery = new NativeSearchQueryBuilder() NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch"))
.withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build(); .build();
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(1, articles.size()); SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.getTotalHits());
searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")) searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About"))
.build(); .build();
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(0, articles.size()); articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(0, articles.getTotalHits());
} }
@Test @Test
public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() { public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() {
final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), ScoreMode.None); final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), ScoreMode.None);
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder)
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); .build();
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(2, articles.size()); assertEquals(2, articles.getTotalHits());
} }
@Test @Test
public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() { public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception {
final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags")
final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation) .field("title");
.execute().actionGet();
final Map<String, Aggregation> results = response.getAggregations().asMap(); final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
final StringTerms topTags = (StringTerms) results.get("top_tags"); final SearchRequest searchRequest = new SearchRequest("blog").source(builder);
final List<String> keys = topTags.getBuckets().stream() final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
final Map<String, Aggregation> results = response.getAggregations()
.asMap();
final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");
final List<String> keys = topTags.getBuckets()
.stream()
.map(MultiBucketsAggregation.Bucket::getKeyAsString) .map(MultiBucketsAggregation.Bucket::getKeyAsString)
.sorted() .sorted()
.collect(toList()); .collect(toList());
@ -157,16 +178,23 @@ public class ElasticSearchQueryManualTest {
} }
@Test @Test
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() { public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception {
final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags") final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags")
.order(Terms.Order.count(false)); .field("tags")
final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation) .order(BucketOrder.count(false));
.execute().actionGet();
final Map<String, Aggregation> results = response.getAggregations().asMap(); final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
final StringTerms topTags = (StringTerms) results.get("top_tags"); final SearchRequest searchRequest = new SearchRequest().indices("blog")
.source(builder);
final List<String> keys = topTags.getBuckets().stream() final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
final Map<String, Aggregation> results = response.getAggregations()
.asMap();
final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");
final List<String> keys = topTags.getBuckets()
.stream()
.map(MultiBucketsAggregation.Bucket::getKeyAsString) .map(MultiBucketsAggregation.Bucket::getKeyAsString)
.collect(toList()); .collect(toList());
assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys); assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
@ -174,30 +202,36 @@ public class ElasticSearchQueryManualTest {
@Test @Test
public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() { public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() {
final SearchQuery searchQuery = new NativeSearchQueryBuilder() final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1))
.withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build(); .build();
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(1, articles.size()); final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.getTotalHits());
} }
@Test @Test
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
final SearchQuery searchQuery = new NativeSearchQueryBuilder() final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "spring date elasticserch").operator(Operator.AND)
.withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE) .fuzziness(Fuzziness.ONE)
.prefixLength(3)).build(); .prefixLength(3))
.build();
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.size());
assertEquals(1, articles.getTotalHits());
} }
@Test @Test
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
final SearchQuery searchQuery = new NativeSearchQueryBuilder() final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(multiMatchQuery("tutorial").field("title")
.withQuery(multiMatchQuery("tutorial").field("title").field("tags") .field("tags")
.type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build(); .type(MultiMatchQueryBuilder.Type.BEST_FIELDS))
.build();
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(2, articles.size());
assertEquals(2, articles.getTotalHits());
} }
@Test @Test
@ -205,10 +239,10 @@ public class ElasticSearchQueryManualTest {
final QueryBuilder builder = boolQuery().must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None)) final QueryBuilder builder = boolQuery().must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None))
.filter(termQuery("tags", "elasticsearch")); .filter(termQuery("tags", "elasticsearch"));
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder) final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder)
.build(); .build();
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(2, articles.size()); assertEquals(2, articles.getTotalHits());
} }
} }