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