|
| 1 | +/* |
| 2 | + * Copyright 2013 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.elasticsearch.core.aggregation; |
| 17 | + |
| 18 | +import org.elasticsearch.action.search.SearchResponse; |
| 19 | +import org.elasticsearch.search.aggregations.Aggregations; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; |
| 25 | +import org.springframework.data.elasticsearch.core.ResultsExtractor; |
| 26 | +import org.springframework.data.elasticsearch.core.facet.ArticleEntity; |
| 27 | +import org.springframework.data.elasticsearch.core.facet.ArticleEntityBuilder; |
| 28 | +import org.springframework.data.elasticsearch.core.query.IndexQuery; |
| 29 | +import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; |
| 30 | +import org.springframework.data.elasticsearch.core.query.SearchQuery; |
| 31 | +import org.springframework.test.context.ContextConfiguration; |
| 32 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 33 | + |
| 34 | +import static org.elasticsearch.action.search.SearchType.COUNT; |
| 35 | +import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; |
| 36 | +import static org.elasticsearch.search.aggregations.AggregationBuilders.terms; |
| 37 | +import static org.hamcrest.Matchers.is; |
| 38 | +import static org.hamcrest.Matchers.notNullValue; |
| 39 | +import static org.junit.Assert.assertThat; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author Rizwan Idrees |
| 43 | + * @author Mohsin Husen |
| 44 | + * @author Jonathan Yan |
| 45 | + * @author Artur Konczak |
| 46 | + */ |
| 47 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 48 | +@ContextConfiguration("classpath:elasticsearch-template-test.xml") |
| 49 | +public class ElasticsearchTemplateAggregationTests { |
| 50 | + |
| 51 | + public static final String RIZWAN_IDREES = "Rizwan Idrees"; |
| 52 | + public static final String MOHSIN_HUSEN = "Mohsin Husen"; |
| 53 | + public static final String JONATHAN_YAN = "Jonathan Yan"; |
| 54 | + public static final String ARTUR_KONCZAK = "Artur Konczak"; |
| 55 | + public static final int YEAR_2002 = 2002; |
| 56 | + public static final int YEAR_2001 = 2001; |
| 57 | + public static final int YEAR_2000 = 2000; |
| 58 | + @Autowired |
| 59 | + private ElasticsearchTemplate elasticsearchTemplate; |
| 60 | + |
| 61 | + @Before |
| 62 | + public void before() { |
| 63 | + elasticsearchTemplate.deleteIndex(ArticleEntity.class); |
| 64 | + elasticsearchTemplate.createIndex(ArticleEntity.class); |
| 65 | + elasticsearchTemplate.putMapping(ArticleEntity.class); |
| 66 | + elasticsearchTemplate.refresh(ArticleEntity.class, true); |
| 67 | + |
| 68 | + IndexQuery article1 = new ArticleEntityBuilder("1").title("article four").subject("computing").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addAuthor(MOHSIN_HUSEN).addAuthor(JONATHAN_YAN).score(10).buildIndex(); |
| 69 | + IndexQuery article2 = new ArticleEntityBuilder("2").title("article three").subject("computing").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addAuthor(MOHSIN_HUSEN).addPublishedYear(YEAR_2000).score(20).buildIndex(); |
| 70 | + IndexQuery article3 = new ArticleEntityBuilder("3").title("article two").subject("computing").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addPublishedYear(YEAR_2001).addPublishedYear(YEAR_2000).score(30).buildIndex(); |
| 71 | + IndexQuery article4 = new ArticleEntityBuilder("4").title("article one").subject("accounting").addAuthor(RIZWAN_IDREES).addPublishedYear(YEAR_2002).addPublishedYear(YEAR_2001).addPublishedYear(YEAR_2000).score(40).buildIndex(); |
| 72 | + |
| 73 | + elasticsearchTemplate.index(article1); |
| 74 | + elasticsearchTemplate.index(article2); |
| 75 | + elasticsearchTemplate.index(article3); |
| 76 | + elasticsearchTemplate.index(article4); |
| 77 | + elasticsearchTemplate.refresh(ArticleEntity.class, true); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void shouldReturnAggregatedResponseForGivenSearchQuery() { |
| 82 | + // given |
| 83 | + SearchQuery searchQuery = new NativeSearchQueryBuilder() |
| 84 | + .withQuery(matchAllQuery()) |
| 85 | + .withSearchType(COUNT) |
| 86 | + .withIndices("articles").withTypes("article") |
| 87 | + .addAggregation(terms("subjects").field("subject")) |
| 88 | + .build(); |
| 89 | + // when |
| 90 | + Aggregations aggregations = elasticsearchTemplate.query(searchQuery, new ResultsExtractor<Aggregations>() { |
| 91 | + @Override |
| 92 | + public Aggregations extract(SearchResponse response) { |
| 93 | + return response.getAggregations(); |
| 94 | + } |
| 95 | + }); |
| 96 | + // then |
| 97 | + assertThat(aggregations, is(notNullValue())); |
| 98 | + assertThat(aggregations.asMap().get("subjects"), is(notNullValue())); |
| 99 | + } |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | + |
0 commit comments