Last active
August 29, 2015 14:18
-
-
Save hkorte/64e630ec579c15697c6d to your computer and use it in GitHub Desktop.
Elasticsearch: Java API allows to create invalid queries by adding a top_hits aggregation without fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sandbox; | |
import org.elasticsearch.action.search.SearchResponse; | |
import org.elasticsearch.index.query.QueryBuilders; | |
import org.elasticsearch.search.aggregations.AggregationBuilders; | |
import org.elasticsearch.test.ElasticsearchIntegrationTest; | |
import org.junit.Before; | |
import org.junit.Test; | |
/** | |
* Created by hkorte on 2015-04-07. | |
* <p> | |
* This test is failing, because a SearchPhaseExecutionException is thrown | |
* in line 47 for a top_hits aggregation without fields defined like that: | |
* <code>AggregationBuilders.topHits("topHitsAgg").setNoFields()</code> | |
* </p> | |
* <p> | |
* IMHO this is misleading. I see two options to avoid this confusion: | |
* <ul> | |
* <li>the top_hits API allows setting "fields" to an empty array</li> | |
* <li>the Java API does not offer this method</li> | |
* </ul> | |
* </p> | |
*/ | |
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE) | |
public class TopHitsAggWithoutFieldsTest extends ElasticsearchIntegrationTest { | |
@Override | |
@Before | |
public void setUp() throws Exception { | |
super.setUp(); | |
createIndex("test"); | |
ensureGreen("test"); | |
} | |
@Test | |
public void testTopHitsAggWithoutFields() throws Exception { | |
SearchResponse sr; | |
sr = client().prepareSearch().setQuery(QueryBuilders.matchAllQuery()) | |
.addAggregation(AggregationBuilders.topHits("topHitsAgg").setSize(1)).get(); | |
sr = client().prepareSearch().setQuery(QueryBuilders.matchAllQuery()) | |
.addAggregation(AggregationBuilders.topHits("topHitsAgg").setFetchSource("", "*").setSize(1)).get(); | |
sr = client().prepareSearch().setQuery(QueryBuilders.matchAllQuery()) | |
.addAggregation(AggregationBuilders.topHits("topHitsAgg").setNoFields().setSize(1)).get(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment