fixed format
This commit is contained in:
parent
2c5c556fed
commit
58e44705b2
|
@ -79,7 +79,6 @@
|
||||||
<properties>
|
<properties>
|
||||||
<spring-data-elasticsearch.version>3.2.6.RELEASE</spring-data-elasticsearch.version>
|
<spring-data-elasticsearch.version>3.2.6.RELEASE</spring-data-elasticsearch.version>
|
||||||
<jna.version>4.5.2</jna.version>
|
<jna.version>4.5.2</jna.version>
|
||||||
<elasticsearch.version>6.8.8</elasticsearch.version>
|
|
||||||
<fastjson.version>1.2.47</fastjson.version>
|
<fastjson.version>1.2.47</fastjson.version>
|
||||||
<spatial4j.version>0.6</spatial4j.version>
|
<spatial4j.version>0.6</spatial4j.version>
|
||||||
<jts.version>1.13</jts.version>
|
<jts.version>1.13</jts.version>
|
||||||
|
|
|
@ -46,183 +46,178 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* This Manual test requires:
|
* This Manual test requires: * Elasticsearch instance running on host * with
|
||||||
* * Elasticsearch instance running on host
|
* cluster name = elasticsearch
|
||||||
* * with cluster name = elasticsearch
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
public class ElasticSearchQueryManualTest {
|
public class ElasticSearchQueryManualTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ElasticsearchRestTemplate elasticsearchTemplate;
|
private ElasticsearchRestTemplate elasticsearchTemplate;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ArticleService articleService;
|
private ArticleService articleService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RestHighLevelClient client;
|
private RestHighLevelClient client;
|
||||||
|
|
||||||
private final Author johnSmith = new Author("John Smith");
|
private final Author johnSmith = new Author("John Smith");
|
||||||
private final Author johnDoe = new Author("John Doe");
|
private final Author johnDoe = new Author("John Doe");
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() {
|
public void before() {
|
||||||
elasticsearchTemplate.deleteIndex(Article.class);
|
elasticsearchTemplate.deleteIndex(Article.class);
|
||||||
elasticsearchTemplate.createIndex(Article.class);
|
elasticsearchTemplate.createIndex(Article.class);
|
||||||
elasticsearchTemplate.putMapping(Article.class);
|
elasticsearchTemplate.putMapping(Article.class);
|
||||||
elasticsearchTemplate.refresh(Article.class);
|
elasticsearchTemplate.refresh(Article.class);
|
||||||
|
|
||||||
Article article = new Article("Spring Data Elasticsearch");
|
Article article = new Article("Spring Data Elasticsearch");
|
||||||
article.setAuthors(asList(johnSmith, johnDoe));
|
article.setAuthors(asList(johnSmith, johnDoe));
|
||||||
article.setTags("elasticsearch", "spring data");
|
article.setTags("elasticsearch", "spring data");
|
||||||
articleService.save(article);
|
articleService.save(article);
|
||||||
|
|
||||||
article = new Article("Search engines");
|
article = new Article("Search engines");
|
||||||
article.setAuthors(asList(johnDoe));
|
article.setAuthors(asList(johnDoe));
|
||||||
article.setTags("search engines", "tutorial");
|
article.setTags("search engines", "tutorial");
|
||||||
articleService.save(article);
|
articleService.save(article);
|
||||||
|
|
||||||
article = new Article("Second Article About Elasticsearch");
|
article = new Article("Second Article About Elasticsearch");
|
||||||
article.setAuthors(asList(johnSmith));
|
article.setAuthors(asList(johnSmith));
|
||||||
article.setTags("elasticsearch", "spring data");
|
article.setTags("elasticsearch", "spring data");
|
||||||
articleService.save(article);
|
articleService.save(article);
|
||||||
|
|
||||||
article = new Article("Elasticsearch Tutorial");
|
article = new Article("Elasticsearch Tutorial");
|
||||||
article.setAuthors(asList(johnDoe));
|
article.setAuthors(asList(johnDoe));
|
||||||
article.setTags("elasticsearch");
|
article.setTags("elasticsearch");
|
||||||
articleService.save(article);
|
articleService.save(article);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
|
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
.withQuery(matchQuery("title", "Search engines").operator(AND)).build();
|
.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()
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions"))
|
||||||
.withQuery(matchQuery("title", "Engines Solutions")).build();
|
.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());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
|
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data"))
|
||||||
.withQuery(matchQuery("title", "elasticsearch data")).build();
|
.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()
|
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
.withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build();
|
.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"))
|
searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About"))
|
||||||
.build();
|
.build();
|
||||||
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(0, articles.size());
|
assertEquals(0, articles.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() {
|
public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() {
|
||||||
final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), ScoreMode.None);
|
final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")),
|
||||||
|
ScoreMode.None);
|
||||||
|
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).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());
|
||||||
}
|
}
|
||||||
|
|
||||||
@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 SearchRequest searchRequest = new SearchRequest("blog")
|
|
||||||
.types("article")
|
|
||||||
.source(builder);
|
|
||||||
|
|
||||||
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
|
||||||
|
final SearchRequest searchRequest = new SearchRequest("blog").types("article").source(builder);
|
||||||
final Map<String, Aggregation> results = response.getAggregations().asMap();
|
|
||||||
final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");
|
|
||||||
|
|
||||||
final List<String> keys = topTags.getBuckets().stream()
|
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
.map(MultiBucketsAggregation.Bucket::getKeyAsString)
|
|
||||||
.sorted()
|
|
||||||
.collect(toList());
|
|
||||||
assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
final Map<String, Aggregation> results = response.getAggregations().asMap();
|
||||||
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception {
|
final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");
|
||||||
final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags")
|
|
||||||
.order(BucketOrder.count(false));
|
|
||||||
|
|
||||||
final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
|
final List<String> keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString).sorted()
|
||||||
final SearchRequest searchRequest = new SearchRequest()
|
.collect(toList());
|
||||||
.indices("blog")
|
assertEquals(
|
||||||
.types("article")
|
asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys);
|
||||||
.source(builder);
|
}
|
||||||
|
|
||||||
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
@Test
|
||||||
|
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception {
|
||||||
|
final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags")
|
||||||
|
.order(BucketOrder.count(false));
|
||||||
|
|
||||||
final Map<String, Aggregation> results = response.getAggregations().asMap();
|
final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
|
||||||
final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");
|
final SearchRequest searchRequest = new SearchRequest().indices("blog").types("article").source(builder);
|
||||||
|
|
||||||
final List<String> keys = topTags.getBuckets().stream()
|
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
.map(MultiBucketsAggregation.Bucket::getKeyAsString)
|
|
||||||
.collect(toList());
|
|
||||||
assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
final Map<String, Aggregation> results = response.getAggregations().asMap();
|
||||||
public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() {
|
final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
|
||||||
.withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build();
|
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
|
||||||
assertEquals(1, articles.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
final List<String> keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString)
|
||||||
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
|
.collect(toList());
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
|
||||||
.withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE)
|
}
|
||||||
.prefixLength(3)).build();
|
|
||||||
|
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
@Test
|
||||||
assertEquals(1, articles.size());
|
public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() {
|
||||||
}
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build();
|
||||||
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
|
assertEquals(1, articles.size());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
|
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
.withQuery(multiMatchQuery("tutorial").field("title").field("tags")
|
.withQuery(
|
||||||
.type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build();
|
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(2, articles.size());
|
assertEquals(1, articles.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() {
|
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
|
||||||
final QueryBuilder builder = boolQuery().must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None))
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
.filter(termQuery("tags", "elasticsearch"));
|
.withQuery(
|
||||||
|
multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS))
|
||||||
|
.build();
|
||||||
|
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder)
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
.build();
|
assertEquals(2, articles.size());
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
}
|
||||||
|
|
||||||
assertEquals(2, articles.size());
|
@Test
|
||||||
}
|
public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() {
|
||||||
|
final QueryBuilder builder = boolQuery()
|
||||||
|
.must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None))
|
||||||
|
.filter(termQuery("tags", "elasticsearch"));
|
||||||
|
|
||||||
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
|
||||||
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
|
|
||||||
|
assertEquals(2, articles.size());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue