fixed formatting according to eclipse formatter
This commit is contained in:
parent
1ed9c7db32
commit
40aa75eec1
|
@ -17,9 +17,12 @@ public class Config {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
RestHighLevelClient client() {
|
RestHighLevelClient client() {
|
||||||
ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build();
|
ClientConfiguration clientConfiguration = ClientConfiguration.builder()
|
||||||
|
.connectedTo("localhost:9200")
|
||||||
|
.build();
|
||||||
|
|
||||||
return RestClients.create(clientConfiguration).rest();
|
return RestClients.create(clientConfiguration)
|
||||||
|
.rest();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
@ -8,11 +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:
|
* This Manual test requires: Elasticsearch instance running on localhost:9200.
|
||||||
* Elasticsearch instance running on localhost:9200.
|
|
||||||
*
|
*
|
||||||
* The following docker command can be used:
|
* The following docker command can be used: docker run -d --name es761 -p
|
||||||
* docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
* 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
|
|
|
@ -37,11 +37,10 @@ import org.springframework.data.elasticsearch.client.ClientConfiguration;
|
||||||
import org.springframework.data.elasticsearch.client.RestClients;
|
import org.springframework.data.elasticsearch.client.RestClients;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This Manual test requires:
|
* This Manual test requires: Elasticsearch instance running on localhost:9200.
|
||||||
* Elasticsearch instance running on localhost:9200.
|
|
||||||
*
|
*
|
||||||
* The following docker command can be used:
|
* The following docker command can be used: docker run -d --name es761 -p
|
||||||
* docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
* 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
||||||
*/
|
*/
|
||||||
public class ElasticSearchManualTest {
|
public class ElasticSearchManualTest {
|
||||||
private List<Person> listOfPersons = new ArrayList<>();
|
private List<Person> listOfPersons = new ArrayList<>();
|
||||||
|
@ -54,8 +53,11 @@ public class ElasticSearchManualTest {
|
||||||
listOfPersons.add(person1);
|
listOfPersons.add(person1);
|
||||||
listOfPersons.add(person2);
|
listOfPersons.add(person2);
|
||||||
|
|
||||||
ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build();
|
ClientConfiguration clientConfiguration = ClientConfiguration.builder()
|
||||||
client = RestClients.create(clientConfiguration).rest();
|
.connectedTo("localhost:9200")
|
||||||
|
.build();
|
||||||
|
client = RestClients.create(clientConfiguration)
|
||||||
|
.rest();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -100,8 +102,10 @@ public class ElasticSearchManualTest {
|
||||||
public void givenSearchRequest_whenMatchAll_thenReturnAllResults() throws Exception {
|
public void givenSearchRequest_whenMatchAll_thenReturnAllResults() throws Exception {
|
||||||
SearchRequest searchRequest = new SearchRequest();
|
SearchRequest searchRequest = new SearchRequest();
|
||||||
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
SearchHit[] searchHits = response.getHits().getHits();
|
SearchHit[] searchHits = response.getHits()
|
||||||
List<Person> results = Arrays.stream(searchHits).map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class))
|
.getHits();
|
||||||
|
List<Person> results = Arrays.stream(searchHits)
|
||||||
|
.map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
results.forEach(System.out::println);
|
results.forEach(System.out::println);
|
||||||
|
@ -109,8 +113,9 @@ public class ElasticSearchManualTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSearchParameters_thenReturnResults() throws Exception {
|
public void givenSearchParameters_thenReturnResults() throws Exception {
|
||||||
SearchSourceBuilder builder =
|
SearchSourceBuilder builder = new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age")
|
||||||
new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age").from(5).to(15));
|
.from(5)
|
||||||
|
.to(15));
|
||||||
|
|
||||||
SearchRequest searchRequest = new SearchRequest();
|
SearchRequest searchRequest = new SearchRequest();
|
||||||
searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH);
|
searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH);
|
||||||
|
@ -136,10 +141,12 @@ public class ElasticSearchManualTest {
|
||||||
response2.getHits();
|
response2.getHits();
|
||||||
response3.getHits();
|
response3.getHits();
|
||||||
|
|
||||||
final List<Person> results =
|
final List<Person> results = Stream.of(response.getHits()
|
||||||
Stream.of(response.getHits().getHits(),
|
.getHits(),
|
||||||
response2.getHits().getHits(),
|
response2.getHits()
|
||||||
response3.getHits().getHits())
|
.getHits(),
|
||||||
|
response3.getHits()
|
||||||
|
.getHits())
|
||||||
.flatMap(Arrays::stream)
|
.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());
|
||||||
|
@ -149,8 +156,12 @@ public class ElasticSearchManualTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException {
|
public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException {
|
||||||
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("fullName", "Test")
|
XContentBuilder builder = XContentFactory.jsonBuilder()
|
||||||
.field("salary", "11500").field("age", "10").endObject();
|
.startObject()
|
||||||
|
.field("fullName", "Test")
|
||||||
|
.field("salary", "11500")
|
||||||
|
.field("age", "10")
|
||||||
|
.endObject();
|
||||||
|
|
||||||
IndexRequest indexRequest = new IndexRequest("people");
|
IndexRequest indexRequest = new IndexRequest("people");
|
||||||
indexRequest.source(builder);
|
indexRequest.source(builder);
|
||||||
|
|
|
@ -39,11 +39,10 @@ import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This Manual test requires:
|
* This Manual test requires: Elasticsearch instance running on localhost:9200.
|
||||||
* Elasticsearch instance running on localhost:9200.
|
|
||||||
*
|
*
|
||||||
* The following docker command can be used:
|
* The following docker command can be used: docker run -d --name es761 -p
|
||||||
* docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
* 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
|
@ -61,7 +60,8 @@ public class GeoQueriesManualTest {
|
||||||
CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD);
|
CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD);
|
||||||
req.mapping(jsonObject, XContentType.JSON);
|
req.mapping(jsonObject, XContentType.JSON);
|
||||||
|
|
||||||
client.indices().create(req, RequestOptions.DEFAULT);
|
client.indices()
|
||||||
|
.create(req, RequestOptions.DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -74,13 +74,13 @@ public class GeoQueriesManualTest {
|
||||||
String tajMahalId = response.getId();
|
String tajMahalId = response.getId();
|
||||||
|
|
||||||
RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
|
RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
|
||||||
client.indices().refresh(refreshRequest, RequestOptions.DEFAULT);
|
client.indices()
|
||||||
|
.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);
|
||||||
|
|
||||||
GeoShapeQueryBuilder qb = QueryBuilders.geoShapeQuery("region",
|
GeoShapeQueryBuilder qb = QueryBuilders.geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight).buildGeometry());
|
||||||
new EnvelopeBuilder(topLeft, bottomRight).buildGeometry());
|
|
||||||
qb.relation(ShapeRelation.INTERSECTS);
|
qb.relation(ShapeRelation.INTERSECTS);
|
||||||
|
|
||||||
SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
|
SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
|
||||||
|
@ -89,7 +89,9 @@ public class GeoQueriesManualTest {
|
||||||
|
|
||||||
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
|
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId)
|
List<String> ids = Arrays.stream(searchResponse.getHits()
|
||||||
|
.getHits())
|
||||||
|
.map(SearchHit::getId)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
assertTrue(ids.contains(tajMahalId));
|
assertTrue(ids.contains(tajMahalId));
|
||||||
|
@ -106,9 +108,11 @@ public class GeoQueriesManualTest {
|
||||||
String pyramidsOfGizaId = response.getId();
|
String pyramidsOfGizaId = response.getId();
|
||||||
|
|
||||||
RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
|
RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
|
||||||
client.indices().refresh(refreshRequest, RequestOptions.DEFAULT);
|
client.indices()
|
||||||
|
.refresh(refreshRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location").setCorners(31, 30, 28, 32);
|
QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location")
|
||||||
|
.setCorners(31, 30, 28, 32);
|
||||||
|
|
||||||
SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
|
SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
|
||||||
SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD);
|
SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD);
|
||||||
|
@ -116,7 +120,9 @@ public class GeoQueriesManualTest {
|
||||||
|
|
||||||
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
|
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId)
|
List<String> ids = Arrays.stream(searchResponse.getHits()
|
||||||
|
.getHits())
|
||||||
|
.map(SearchHit::getId)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
assertTrue(ids.contains(pyramidsOfGizaId));
|
assertTrue(ids.contains(pyramidsOfGizaId));
|
||||||
}
|
}
|
||||||
|
@ -132,10 +138,10 @@ public class GeoQueriesManualTest {
|
||||||
String lighthouseOfAlexandriaId = response.getId();
|
String lighthouseOfAlexandriaId = response.getId();
|
||||||
|
|
||||||
RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
|
RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
|
||||||
client.indices().refresh(refreshRequest, RequestOptions.DEFAULT);
|
client.indices()
|
||||||
|
.refresh(refreshRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
QueryBuilder qb =
|
QueryBuilder qb = QueryBuilders.geoDistanceQuery("location")
|
||||||
QueryBuilders.geoDistanceQuery("location")
|
|
||||||
.point(29.976, 31.131)
|
.point(29.976, 31.131)
|
||||||
.distance(10, DistanceUnit.MILES);
|
.distance(10, DistanceUnit.MILES);
|
||||||
|
|
||||||
|
@ -145,7 +151,9 @@ public class GeoQueriesManualTest {
|
||||||
|
|
||||||
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
|
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId)
|
List<String> ids = Arrays.stream(searchResponse.getHits()
|
||||||
|
.getHits())
|
||||||
|
.map(SearchHit::getId)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
assertTrue(ids.contains(lighthouseOfAlexandriaId));
|
assertTrue(ids.contains(lighthouseOfAlexandriaId));
|
||||||
}
|
}
|
||||||
|
@ -161,7 +169,8 @@ public class GeoQueriesManualTest {
|
||||||
String greatRannOfKutchid = response.getId();
|
String greatRannOfKutchid = response.getId();
|
||||||
|
|
||||||
RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
|
RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
|
||||||
client.indices().refresh(refreshRequest, RequestOptions.DEFAULT);
|
client.indices()
|
||||||
|
.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));
|
||||||
|
@ -175,7 +184,9 @@ public class GeoQueriesManualTest {
|
||||||
|
|
||||||
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
|
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId)
|
List<String> ids = Arrays.stream(searchResponse.getHits()
|
||||||
|
.getHits())
|
||||||
|
.map(SearchHit::getId)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
assertTrue(ids.contains(greatRannOfKutchid));
|
assertTrue(ids.contains(greatRannOfKutchid));
|
||||||
}
|
}
|
||||||
|
@ -183,6 +194,7 @@ public class GeoQueriesManualTest {
|
||||||
@After
|
@After
|
||||||
public void destroy() throws Exception {
|
public void destroy() throws Exception {
|
||||||
DeleteIndexRequest deleteIndex = new DeleteIndexRequest(WONDERS_OF_WORLD);
|
DeleteIndexRequest deleteIndex = new DeleteIndexRequest(WONDERS_OF_WORLD);
|
||||||
client.indices().delete(deleteIndex, RequestOptions.DEFAULT);
|
client.indices()
|
||||||
|
.delete(deleteIndex, RequestOptions.DEFAULT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,11 +31,10 @@ import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This Manual test requires:
|
* This Manual test requires: Elasticsearch instance running on localhost:9200.
|
||||||
* Elasticsearch instance running on localhost:9200.
|
|
||||||
*
|
*
|
||||||
* The following docker command can be used:
|
* The following docker command can be used: docker run -d --name es761 -p
|
||||||
* docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
* 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
|
@ -91,29 +90,25 @@ public class ElasticSearchManualTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() {
|
public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() {
|
||||||
final Page<Article> articleByAuthorName = articleRepository.findByAuthorsName(johnSmith.getName(),
|
final Page<Article> articleByAuthorName = articleRepository.findByAuthorsName(johnSmith.getName(), PageRequest.of(0, 10));
|
||||||
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 = articleRepository.findByAuthorsNameUsingCustomQuery("Smith",
|
final Page<Article> articleByAuthorName = articleRepository.findByAuthorsNameUsingCustomQuery("Smith", PageRequest.of(0, 10));
|
||||||
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 = articleRepository.findByFilteredTagQuery("elasticsearch",
|
final Page<Article> articleByAuthorName = articleRepository.findByFilteredTagQuery("elasticsearch", PageRequest.of(0, 10));
|
||||||
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 = articleRepository.findByAuthorsNameAndFilteredTagQuery("Doe",
|
final Page<Article> articleByAuthorName = articleRepository.findByAuthorsNameAndFilteredTagQuery("Doe", "elasticsearch", PageRequest.of(0, 10));
|
||||||
"elasticsearch", PageRequest.of(0, 10));
|
|
||||||
assertEquals(2L, articleByAuthorName.getTotalElements());
|
assertEquals(2L, articleByAuthorName.getTotalElements());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,50 +117,52 @@ public class ElasticSearchManualTest {
|
||||||
final Query searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*"))
|
final Query searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*"))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final SearchHits<Article> articles =
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
|
||||||
|
|
||||||
assertEquals(1, articles.getTotalHits());
|
assertEquals(1, articles.getTotalHits());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() {
|
public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() {
|
||||||
final Query searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")).build();
|
final Query searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch"))
|
||||||
|
.build();
|
||||||
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
|
||||||
assertEquals(1, articles.getTotalHits());
|
assertEquals(1, articles.getTotalHits());
|
||||||
|
|
||||||
final Article article = articles.getSearchHit(0).getContent();
|
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);
|
||||||
articleRepository.save(article);
|
articleRepository.save(article);
|
||||||
|
|
||||||
assertEquals(newTitle, articleRepository.findById(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 Query searchQuery = new NativeSearchQueryBuilder()
|
final Query searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%"))
|
||||||
.withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build();
|
.build();
|
||||||
final SearchHits<Article> articles =
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
|
||||||
|
|
||||||
assertEquals(1, articles.getTotalHits());
|
assertEquals(1, articles.getTotalHits());
|
||||||
final long count = articleRepository.count();
|
final long count = articleRepository.count();
|
||||||
|
|
||||||
articleRepository.delete(articles.getSearchHit(0).getContent());
|
articleRepository.delete(articles.getSearchHit(0)
|
||||||
|
.getContent());
|
||||||
|
|
||||||
assertEquals(count - 1, articleRepository.count());
|
assertEquals(count - 1, articleRepository.count());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
|
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
|
||||||
final Query 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 SearchHits<Article> articles =
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
|
||||||
assertEquals(1, articles.getTotalHits());
|
assertEquals(1, articles.getTotalHits());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,11 +48,10 @@ import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This Manual test requires:
|
* This Manual test requires: Elasticsearch instance running on localhost:9200.
|
||||||
* Elasticsearch instance running on localhost:9200.
|
|
||||||
*
|
*
|
||||||
* The following docker command can be used:
|
* The following docker command can be used: docker run -d --name es761 -p
|
||||||
* docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
* 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
|
@ -100,51 +99,45 @@ public class ElasticSearchQueryManualTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
|
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||||
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
|
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(Operator.AND))
|
||||||
.withQuery(matchQuery("title", "Search engines").operator(Operator.AND)).build();
|
.build();
|
||||||
final SearchHits<Article> articles =
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
|
||||||
assertEquals(1, articles.getTotalHits());
|
assertEquals(1, articles.getTotalHits());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() {
|
public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||||
final NativeSearchQuery searchQuery =
|
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions"))
|
||||||
new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions"))
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final SearchHits<Article> articles =
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
|
||||||
|
|
||||||
assertEquals(1, articles.getTotalHits());
|
assertEquals(1, articles.getTotalHits());
|
||||||
assertEquals("Search engines", articles.getSearchHit(0).getContent().getTitle());
|
assertEquals("Search engines", articles.getSearchHit(0)
|
||||||
|
.getContent()
|
||||||
|
.getTitle());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
|
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||||
final NativeSearchQuery searchQuery =
|
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data"))
|
||||||
new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data"))
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final SearchHits<Article> articles =
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
|
||||||
|
|
||||||
assertEquals(3, articles.getTotalHits());
|
assertEquals(3, articles.getTotalHits());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() {
|
public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() {
|
||||||
NativeSearchQuery 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();
|
||||||
|
|
||||||
SearchHits<Article> articles =
|
SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
|
||||||
|
|
||||||
assertEquals(1, articles.getTotalHits());
|
assertEquals(1, articles.getTotalHits());
|
||||||
|
|
||||||
searchQuery = new NativeSearchQueryBuilder()
|
searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About"))
|
||||||
.withQuery(matchQuery("title.verbatim", "Second Article About"))
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
@ -153,10 +146,10 @@ public class ElasticSearchQueryManualTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() {
|
public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() {
|
||||||
final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")),
|
final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), ScoreMode.None);
|
||||||
ScoreMode.None);
|
|
||||||
|
|
||||||
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
|
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder)
|
||||||
|
.build();
|
||||||
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
|
||||||
assertEquals(2, articles.getTotalHits());
|
assertEquals(2, articles.getTotalHits());
|
||||||
|
@ -164,44 +157,53 @@ public class ElasticSearchQueryManualTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception {
|
public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception {
|
||||||
final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title");
|
final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags")
|
||||||
|
.field("title");
|
||||||
|
|
||||||
final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
|
final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
|
||||||
final SearchRequest searchRequest = new SearchRequest("blog").source(builder);
|
final SearchRequest searchRequest = new SearchRequest("blog").source(builder);
|
||||||
|
|
||||||
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
final Map<String, Aggregation> results = response.getAggregations().asMap();
|
final Map<String, Aggregation> results = response.getAggregations()
|
||||||
|
.asMap();
|
||||||
final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");
|
final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");
|
||||||
|
|
||||||
final List<String> keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString).sorted()
|
final List<String> keys = topTags.getBuckets()
|
||||||
|
.stream()
|
||||||
|
.map(MultiBucketsAggregation.Bucket::getKeyAsString)
|
||||||
|
.sorted()
|
||||||
.collect(toList());
|
.collect(toList());
|
||||||
assertEquals(
|
assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys);
|
||||||
asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception {
|
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception {
|
||||||
final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags")
|
final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags")
|
||||||
|
.field("tags")
|
||||||
.order(BucketOrder.count(false));
|
.order(BucketOrder.count(false));
|
||||||
|
|
||||||
final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
|
final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
|
||||||
final SearchRequest searchRequest = new SearchRequest().indices("blog").source(builder);
|
final SearchRequest searchRequest = new SearchRequest().indices("blog")
|
||||||
|
.source(builder);
|
||||||
|
|
||||||
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
final Map<String, Aggregation> results = response.getAggregations().asMap();
|
final Map<String, Aggregation> results = response.getAggregations()
|
||||||
|
.asMap();
|
||||||
final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");
|
final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");
|
||||||
|
|
||||||
final List<String> keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString)
|
final List<String> keys = topTags.getBuckets()
|
||||||
|
.stream()
|
||||||
|
.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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() {
|
public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() {
|
||||||
final NativeSearchQuery 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 SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
|
||||||
|
@ -210,9 +212,9 @@ public class ElasticSearchQueryManualTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
|
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
|
||||||
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
|
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "spring date elasticserch").operator(Operator.AND)
|
||||||
.withQuery(
|
.fuzziness(Fuzziness.ONE)
|
||||||
matchQuery("title", "spring date elasticserch").operator(Operator.AND).fuzziness(Fuzziness.ONE).prefixLength(3))
|
.prefixLength(3))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
@ -222,9 +224,9 @@ public class ElasticSearchQueryManualTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
|
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
|
||||||
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
|
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(multiMatchQuery("tutorial").field("title")
|
||||||
.withQuery(
|
.field("tags")
|
||||||
multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS))
|
.type(MultiMatchQueryBuilder.Type.BEST_FIELDS))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
@ -234,11 +236,11 @@ public class ElasticSearchQueryManualTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() {
|
public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() {
|
||||||
final QueryBuilder builder = boolQuery()
|
final QueryBuilder builder = boolQuery().must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None))
|
||||||
.must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None))
|
|
||||||
.filter(termQuery("tags", "elasticsearch"));
|
.filter(termQuery("tags", "elasticsearch"));
|
||||||
|
|
||||||
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
|
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder)
|
||||||
|
.build();
|
||||||
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
|
||||||
assertEquals(2, articles.getTotalHits());
|
assertEquals(2, articles.getTotalHits());
|
||||||
|
|
Loading…
Reference in New Issue