List of usage examples for org.apache.lucene.index MultiReader MultiReader
MultiReader
From source file:org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregatorTests.java
License:Apache License
public void testParsedAsFilter() throws IOException { IndexReader indexReader = new MultiReader(); IndexSearcher indexSearcher = newSearcher(indexReader); QueryBuilder filter = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("field", "foo")) .should(QueryBuilders.termQuery("field", "bar")); FiltersAggregationBuilder builder = new FiltersAggregationBuilder("test", filter); AggregatorFactory<?> factory = createAggregatorFactory(builder, indexSearcher, fieldType); assertThat(factory, Matchers.instanceOf(FiltersAggregatorFactory.class)); FiltersAggregatorFactory filtersFactory = (FiltersAggregatorFactory) factory; Query parsedQuery = filtersFactory.weights[0].getQuery(); assertThat(parsedQuery, Matchers.instanceOf(BooleanQuery.class)); assertEquals(2, ((BooleanQuery) parsedQuery).clauses().size()); // means the bool query has been parsed as a filter, if it was a query minShouldMatch would // be 0/* w w w .jav a 2 s . c o m*/ assertEquals(1, ((BooleanQuery) parsedQuery).getMinimumNumberShouldMatch()); }
From source file:org.elasticsearch.search.aggregations.bucket.significant.SignificantTermsAggregatorTests.java
License:Apache License
public void testParsedAsFilter() throws IOException { IndexReader indexReader = new MultiReader(); IndexSearcher indexSearcher = newSearcher(indexReader); QueryBuilder filter = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("field", "foo")) .should(QueryBuilders.termQuery("field", "bar")); SignificantTermsAggregationBuilder builder = new SignificantTermsAggregationBuilder("test", ValueType.STRING).field("field").backgroundFilter(filter); AggregatorFactory<?> factory = createAggregatorFactory(builder, indexSearcher, fieldType); assertThat(factory, Matchers.instanceOf(SignificantTermsAggregatorFactory.class)); SignificantTermsAggregatorFactory sigTermsFactory = (SignificantTermsAggregatorFactory) factory; Query parsedQuery = sigTermsFactory.filter; assertThat(parsedQuery, Matchers.instanceOf(BooleanQuery.class)); assertEquals(2, ((BooleanQuery) parsedQuery).clauses().size()); // means the bool query has been parsed as a filter, if it was a query minShouldMatch would // be 0/*w w w . j av a 2 s . c o m*/ assertEquals(1, ((BooleanQuery) parsedQuery).getMinimumNumberShouldMatch()); }
From source file:org.elasticsearch.search.aggregations.metrics.percentiles.hdr.HDRPercentileRanksAggregatorTests.java
License:Apache License
public void testEmpty() throws IOException { PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg") .field("field").method(PercentilesMethod.HDR).values(0.5); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); fieldType.setName("field"); try (IndexReader reader = new MultiReader()) { IndexSearcher searcher = new IndexSearcher(reader); PercentileRanks ranks = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); Percentile rank = ranks.iterator().next(); assertEquals(Double.NaN, rank.getPercent(), 0d); assertEquals(0.5, rank.getValue(), 0d); }/*from ww w .java 2 s. c o m*/ }
From source file:org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.TDigestPercentileRanksAggregatorTests.java
License:Apache License
public void testEmpty() throws IOException { PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg") .field("field").method(PercentilesMethod.TDIGEST).values(0.5); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); fieldType.setName("field"); try (IndexReader reader = new MultiReader()) { IndexSearcher searcher = new IndexSearcher(reader); PercentileRanks ranks = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); Percentile rank = ranks.iterator().next(); assertEquals(Double.NaN, rank.getPercent(), 0d); assertEquals(0.5, rank.getValue(), 0d); }/*from w w w. j ava 2 s . co m*/ }
From source file:org.elasticsearch.search.query.QueryPhaseTests.java
License:Apache License
public void testPostFilterDisablesCountOptimization() throws Exception { TestSearchContext context = new TestSearchContext(); context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery())); context.setSize(0);/*from w ww. j a va 2 s.c o m*/ final AtomicBoolean collected = new AtomicBoolean(); IndexSearcher contextSearcher = new IndexSearcher(new MultiReader()) { protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException { collected.set(true); super.search(leaves, weight, collector); } }; QueryPhase.execute(context, contextSearcher); assertEquals(0, context.queryResult().topDocs().totalHits); assertFalse(collected.get()); context.parsedPostFilter(new ParsedQuery(new MatchNoDocsQuery())); QueryPhase.execute(context, contextSearcher); assertEquals(0, context.queryResult().topDocs().totalHits); assertTrue(collected.get()); }
From source file:org.elasticsearch.search.query.QueryPhaseTests.java
License:Apache License
public void testMinScoreDisablesCountOptimization() throws Exception { TestSearchContext context = new TestSearchContext(); context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery())); context.setSize(0);/* w w w .j a v a2 s. c o m*/ final AtomicBoolean collected = new AtomicBoolean(); IndexSearcher contextSearcher = new IndexSearcher(new MultiReader()) { protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException { collected.set(true); super.search(leaves, weight, collector); } }; QueryPhase.execute(context, contextSearcher); assertEquals(0, context.queryResult().topDocs().totalHits); assertFalse(collected.get()); context.minimumScore(1); QueryPhase.execute(context, contextSearcher); assertEquals(0, context.queryResult().topDocs().totalHits); assertTrue(collected.get()); }