Refactor elastic (#3467)
This commit is contained in:
parent
b1520c93fd
commit
d64468eddc
@ -11,10 +11,9 @@ public class Person {
|
|||||||
private Date dateOfBirth;
|
private Date dateOfBirth;
|
||||||
|
|
||||||
public Person() {
|
public Person() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Person(int age, String fullName, Date dateOfBirth) {
|
Person(int age, String fullName, Date dateOfBirth) {
|
||||||
super();
|
super();
|
||||||
this.age = age;
|
this.age = age;
|
||||||
this.fullName = fullName;
|
this.fullName = fullName;
|
||||||
|
@ -19,6 +19,7 @@ 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 static org.elasticsearch.node.NodeBuilder.nodeBuilder;
|
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
@ -78,12 +79,9 @@ public class ElasticSearchManualTest {
|
|||||||
SearchHit[] searchHits = response
|
SearchHit[] searchHits = response
|
||||||
.getHits()
|
.getHits()
|
||||||
.getHits();
|
.getHits();
|
||||||
List<Person> results = new ArrayList<>();
|
List<Person> results = Arrays.stream(searchHits)
|
||||||
for (SearchHit hit : searchHits) {
|
.map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class))
|
||||||
String sourceAsString = hit.getSourceAsString();
|
.collect(Collectors.toList());
|
||||||
Person person = JSON.parseObject(sourceAsString, Person.class);
|
|
||||||
results.add(person);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -125,11 +123,10 @@ public class ElasticSearchManualTest {
|
|||||||
.actionGet();
|
.actionGet();
|
||||||
response2.getHits();
|
response2.getHits();
|
||||||
response3.getHits();
|
response3.getHits();
|
||||||
List<SearchHit> searchHits = Arrays.asList(response
|
|
||||||
.getHits()
|
final List<Person> results = Arrays.stream(response.getHits().getHits())
|
||||||
.getHits());
|
.map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class))
|
||||||
final List<Person> results = new ArrayList<>();
|
.collect(Collectors.toList());
|
||||||
searchHits.forEach(hit -> results.add(JSON.parseObject(hit.getSourceAsString(), Person.class)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
package com.baeldung.elasticsearch;
|
package com.baeldung.elasticsearch;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import com.baeldung.spring.data.es.config.Config;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
|
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
|
||||||
import org.elasticsearch.action.index.IndexResponse;
|
import org.elasticsearch.action.index.IndexResponse;
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
@ -15,6 +10,7 @@ import org.elasticsearch.common.geo.builders.ShapeBuilder;
|
|||||||
import org.elasticsearch.common.unit.DistanceUnit;
|
import org.elasticsearch.common.unit.DistanceUnit;
|
||||||
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.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -24,14 +20,19 @@ 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 java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
public class GeoQueriesTest {
|
public class GeoQueriesTest {
|
||||||
|
|
||||||
public static final String WONDERS_OF_WORLD = "wonders-of-world";
|
private static final String WONDERS_OF_WORLD = "wonders-of-world";
|
||||||
public static final String WONDERS = "Wonders";
|
private static final String WONDERS = "Wonders";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ElasticsearchTemplate elasticsearchTemplate;
|
private ElasticsearchTemplate elasticsearchTemplate;
|
||||||
|
|
||||||
@ -44,39 +45,37 @@ public class GeoQueriesTest {
|
|||||||
CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD);
|
CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD);
|
||||||
req.mapping(WONDERS, jsonObject);
|
req.mapping(WONDERS, jsonObject);
|
||||||
client.admin()
|
client.admin()
|
||||||
.indices()
|
.indices()
|
||||||
.create(req)
|
.create(req)
|
||||||
.actionGet();
|
.actionGet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() {
|
public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() {
|
||||||
String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,25],[80.1,30.2]]}}";
|
String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,25],[80.1,30.2]]}}";
|
||||||
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS)
|
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS)
|
||||||
.setSource(jsonObject)
|
.setSource(jsonObject)
|
||||||
.get();
|
.get();
|
||||||
String tajMahalId = response.getId();
|
String tajMahalId = response.getId();
|
||||||
client.admin()
|
client.admin()
|
||||||
.indices()
|
.indices()
|
||||||
.prepareRefresh(WONDERS_OF_WORLD)
|
.prepareRefresh(WONDERS_OF_WORLD)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
QueryBuilder qb = QueryBuilders.geoShapeQuery("region", ShapeBuilder.newEnvelope()
|
QueryBuilder qb = QueryBuilders.geoShapeQuery("region", ShapeBuilder.newEnvelope()
|
||||||
.topLeft(74.00, 24.0)
|
.topLeft(74.00, 24.0)
|
||||||
.bottomRight(81.1, 31.2))
|
.bottomRight(81.1, 31.2))
|
||||||
.relation(ShapeRelation.WITHIN);
|
.relation(ShapeRelation.WITHIN);
|
||||||
|
|
||||||
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD)
|
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD)
|
||||||
.setTypes(WONDERS)
|
.setTypes(WONDERS)
|
||||||
.setQuery(qb)
|
.setQuery(qb)
|
||||||
.execute()
|
.execute()
|
||||||
.actionGet();
|
.actionGet();
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits()
|
List<String> ids = Arrays.stream(searchResponse.getHits()
|
||||||
.getHits())
|
.getHits())
|
||||||
.map(hit -> {
|
.map(SearchHit::getId)
|
||||||
return hit.getId();
|
.collect(Collectors.toList());
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
assertTrue(ids.contains(tajMahalId));
|
assertTrue(ids.contains(tajMahalId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,29 +83,27 @@ public class GeoQueriesTest {
|
|||||||
public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() {
|
public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() {
|
||||||
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)
|
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS)
|
||||||
.setSource(jsonObject)
|
.setSource(jsonObject)
|
||||||
.get();
|
.get();
|
||||||
String pyramidsOfGizaId = response.getId();
|
String pyramidsOfGizaId = response.getId();
|
||||||
client.admin()
|
client.admin()
|
||||||
.indices()
|
.indices()
|
||||||
.prepareRefresh(WONDERS_OF_WORLD)
|
.prepareRefresh(WONDERS_OF_WORLD)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location")
|
QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location")
|
||||||
.bottomLeft(28, 30)
|
.bottomLeft(28, 30)
|
||||||
.topRight(31, 32);
|
.topRight(31, 32);
|
||||||
|
|
||||||
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD)
|
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD)
|
||||||
.setTypes(WONDERS)
|
.setTypes(WONDERS)
|
||||||
.setQuery(qb)
|
.setQuery(qb)
|
||||||
.execute()
|
.execute()
|
||||||
.actionGet();
|
.actionGet();
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits()
|
List<String> ids = Arrays.stream(searchResponse.getHits()
|
||||||
.getHits())
|
.getHits())
|
||||||
.map(hit -> {
|
.map(SearchHit::getId)
|
||||||
return hit.getId();
|
.collect(Collectors.toList());
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
assertTrue(ids.contains(pyramidsOfGizaId));
|
assertTrue(ids.contains(pyramidsOfGizaId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,29 +111,27 @@ public class GeoQueriesTest {
|
|||||||
public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() {
|
public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() {
|
||||||
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)
|
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS)
|
||||||
.setSource(jsonObject)
|
.setSource(jsonObject)
|
||||||
.get();
|
.get();
|
||||||
String lighthouseOfAlexandriaId = response.getId();
|
String lighthouseOfAlexandriaId = response.getId();
|
||||||
client.admin()
|
client.admin()
|
||||||
.indices()
|
.indices()
|
||||||
.prepareRefresh(WONDERS_OF_WORLD)
|
.prepareRefresh(WONDERS_OF_WORLD)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
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)
|
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD)
|
||||||
.setTypes(WONDERS)
|
.setTypes(WONDERS)
|
||||||
.setQuery(qb)
|
.setQuery(qb)
|
||||||
.execute()
|
.execute()
|
||||||
.actionGet();
|
.actionGet();
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits()
|
List<String> ids = Arrays.stream(searchResponse.getHits()
|
||||||
.getHits())
|
.getHits())
|
||||||
.map(hit -> {
|
.map(SearchHit::getId)
|
||||||
return hit.getId();
|
.collect(Collectors.toList());
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
assertTrue(ids.contains(lighthouseOfAlexandriaId));
|
assertTrue(ids.contains(lighthouseOfAlexandriaId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,30 +139,28 @@ public class GeoQueriesTest {
|
|||||||
public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() {
|
public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() {
|
||||||
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)
|
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS)
|
||||||
.setSource(jsonObject)
|
.setSource(jsonObject)
|
||||||
.get();
|
.get();
|
||||||
String greatRannOfKutchid = response.getId();
|
String greatRannOfKutchid = response.getId();
|
||||||
client.admin()
|
client.admin()
|
||||||
.indices()
|
.indices()
|
||||||
.prepareRefresh(WONDERS_OF_WORLD)
|
.prepareRefresh(WONDERS_OF_WORLD)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
QueryBuilder qb = QueryBuilders.geoPolygonQuery("location")
|
QueryBuilder qb = QueryBuilders.geoPolygonQuery("location")
|
||||||
.addPoint(22.733, 68.859)
|
.addPoint(22.733, 68.859)
|
||||||
.addPoint(24.733, 68.859)
|
.addPoint(24.733, 68.859)
|
||||||
.addPoint(23, 70.859);
|
.addPoint(23, 70.859);
|
||||||
|
|
||||||
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD)
|
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD)
|
||||||
.setTypes(WONDERS)
|
.setTypes(WONDERS)
|
||||||
.setQuery(qb)
|
.setQuery(qb)
|
||||||
.execute()
|
.execute()
|
||||||
.actionGet();
|
.actionGet();
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits()
|
List<String> ids = Arrays.stream(searchResponse.getHits()
|
||||||
.getHits())
|
.getHits())
|
||||||
.map(hit -> {
|
.map(SearchHit::getId)
|
||||||
return hit.getId();
|
.collect(Collectors.toList());
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
assertTrue(ids.contains(greatRannOfKutchid));
|
assertTrue(ids.contains(greatRannOfKutchid));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,5 +168,4 @@ public class GeoQueriesTest {
|
|||||||
public void destroy() {
|
public void destroy() {
|
||||||
elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD);
|
elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,9 @@
|
|||||||
package com.baeldung.spring.data.es;
|
package com.baeldung.spring.data.es;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import com.baeldung.spring.data.es.config.Config;
|
||||||
import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND;
|
import com.baeldung.spring.data.es.model.Article;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery;
|
import com.baeldung.spring.data.es.model.Author;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
import com.baeldung.spring.data.es.service.ArticleService;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.regexpQuery;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
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;
|
||||||
@ -22,10 +16,15 @@ 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 java.util.List;
|
||||||
import com.baeldung.spring.data.es.model.Article;
|
|
||||||
import com.baeldung.spring.data.es.model.Author;
|
import static java.util.Arrays.asList;
|
||||||
import com.baeldung.spring.data.es.service.ArticleService;
|
import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.regexpQuery;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
@ -72,21 +71,24 @@ public class ElasticSearchIntegrationTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() {
|
public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() {
|
||||||
|
|
||||||
final Page<Article> articleByAuthorName = articleService.findByAuthorName(johnSmith.getName(), new PageRequest(0, 10));
|
final Page<Article> articleByAuthorName = articleService
|
||||||
|
.findByAuthorName(johnSmith.getName(), new PageRequest(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("John Smith", new PageRequest(0, 10));
|
final Page<Article> articleByAuthorName = articleService
|
||||||
|
.findByAuthorNameUsingCustomQuery("John Smith", new PageRequest(0, 10));
|
||||||
assertEquals(3L, articleByAuthorName.getTotalElements());
|
assertEquals(3L, articleByAuthorName.getTotalElements());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() {
|
public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() {
|
||||||
|
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*"))
|
||||||
|
.build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
|
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
@ -112,7 +114,8 @@ public class ElasticSearchIntegrationTest {
|
|||||||
|
|
||||||
final String articleTitle = "Spring Data Elasticsearch";
|
final String articleTitle = "Spring Data Elasticsearch";
|
||||||
|
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
final long count = articleService.count();
|
final long count = articleService.count();
|
||||||
@ -124,7 +127,8 @@ public class ElasticSearchIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
|
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title", "Search engines").operator(AND)).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,9 @@
|
|||||||
package com.baeldung.spring.data.es;
|
package com.baeldung.spring.data.es;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import com.baeldung.spring.data.es.config.Config;
|
||||||
import static java.util.stream.Collectors.toList;
|
import com.baeldung.spring.data.es.model.Article;
|
||||||
import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND;
|
import com.baeldung.spring.data.es.model.Author;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
|
import com.baeldung.spring.data.es.service.ArticleService;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;
|
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;
|
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.nestedQuery;
|
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.common.unit.Fuzziness;
|
import org.elasticsearch.common.unit.Fuzziness;
|
||||||
@ -22,6 +11,7 @@ import org.elasticsearch.index.query.MultiMatchQueryBuilder;
|
|||||||
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.bucket.MultiBucketsAggregation;
|
||||||
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
|
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
|
||||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||||
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
|
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
|
||||||
@ -35,10 +25,20 @@ 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 java.util.Collections;
|
||||||
import com.baeldung.spring.data.es.model.Article;
|
import java.util.List;
|
||||||
import com.baeldung.spring.data.es.model.Author;
|
import java.util.Map;
|
||||||
import com.baeldung.spring.data.es.service.ArticleService;
|
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
import static java.util.stream.Collectors.toList;
|
||||||
|
import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.nestedQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
@ -86,14 +86,16 @@ public class ElasticSearchQueryIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
|
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title", "Search engines").operator(AND)).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() {
|
public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title", "Engines Solutions")).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
assertEquals("Search engines", articles.get(0).getTitle());
|
assertEquals("Search engines", articles.get(0).getTitle());
|
||||||
@ -101,18 +103,21 @@ public class ElasticSearchQueryIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
|
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title", "elasticsearch data")).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(3, articles.size());
|
assertEquals(3, articles.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() {
|
public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() {
|
||||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build();
|
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build();
|
||||||
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
|
|
||||||
searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")).build();
|
searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About"))
|
||||||
|
.build();
|
||||||
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(0, articles.size());
|
assertEquals(0, articles.size());
|
||||||
}
|
}
|
||||||
@ -130,38 +135,48 @@ public class ElasticSearchQueryIntegrationTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() {
|
public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() {
|
||||||
final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("title");
|
final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("title");
|
||||||
final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet();
|
final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation)
|
||||||
|
.execute().actionGet();
|
||||||
|
|
||||||
final Map<String, Aggregation> results = response.getAggregations().asMap();
|
final Map<String, Aggregation> results = response.getAggregations().asMap();
|
||||||
final StringTerms topTags = (StringTerms) results.get("top_tags");
|
final StringTerms topTags = (StringTerms) results.get("top_tags");
|
||||||
|
|
||||||
final List<String> keys = topTags.getBuckets().stream().map(b -> b.getKeyAsString()).collect(toList());
|
final List<String> keys = topTags.getBuckets().stream()
|
||||||
Collections.sort(keys);
|
.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
|
@Test
|
||||||
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() {
|
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() {
|
||||||
final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags").order(Terms.Order.aggregation("_count", false));
|
final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags")
|
||||||
final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet();
|
.order(Terms.Order.aggregation("_count", false));
|
||||||
|
final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation)
|
||||||
|
.execute().actionGet();
|
||||||
|
|
||||||
final Map<String, Aggregation> results = response.getAggregations().asMap();
|
final Map<String, Aggregation> results = response.getAggregations().asMap();
|
||||||
final StringTerms topTags = (StringTerms) results.get("top_tags");
|
final StringTerms topTags = (StringTerms) results.get("top_tags");
|
||||||
|
|
||||||
final List<String> keys = topTags.getBuckets().stream().map(b -> b.getKeyAsString()).collect(toList());
|
final List<String> keys = topTags.getBuckets().stream()
|
||||||
|
.map(MultiBucketsAggregation.Bucket::getKeyAsString)
|
||||||
|
.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 SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
|
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE).prefixLength(3)).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE)
|
||||||
|
.prefixLength(3)).build();
|
||||||
|
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
@ -169,7 +184,9 @@ public class ElasticSearchQueryIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
|
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(multiMatchQuery("tutorial").field("title").field("tags")
|
||||||
|
.type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build();
|
||||||
|
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(2, articles.size());
|
assertEquals(2, articles.size());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user