use of org.apache.lucene.index.MultiReader in project elasticsearch by elastic.
the class DateFieldTypeTests method testRangeQuery.
public void testRangeQuery() throws IOException {
Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).build();
QueryShardContext context = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("foo").settings(indexSettings).build(), indexSettings), null, null, null, null, null, xContentRegistry(), null, null, () -> nowInMillis);
MappedFieldType ft = createDefaultFieldType();
ft.setName("field");
String date1 = "2015-10-12T14:10:55";
String date2 = "2016-04-28T11:33:52";
long instant1 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime(date1).getMillis();
long instant2 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime(date2).getMillis() + 999;
ft.setIndexOptions(IndexOptions.DOCS);
Query expected = new IndexOrDocValuesQuery(LongPoint.newRangeQuery("field", instant1, instant2), SortedNumericDocValuesField.newRangeQuery("field", instant1, instant2));
assertEquals(expected, ft.rangeQuery(date1, date2, true, true, context).rewrite(new MultiReader()));
ft.setIndexOptions(IndexOptions.NONE);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ft.rangeQuery(date1, date2, true, true, context));
assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}
use of org.apache.lucene.index.MultiReader in project elasticsearch by elastic.
the class DateFieldTypeTests method testIsFieldWithinQueryEmptyReader.
public void testIsFieldWithinQueryEmptyReader() throws IOException {
QueryRewriteContext context = new QueryRewriteContext(null, null, null, xContentRegistry(), null, null, () -> nowInMillis);
IndexReader reader = new MultiReader();
DateFieldType ft = new DateFieldType();
ft.setName("my_date");
assertEquals(Relation.DISJOINT, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", randomBoolean(), randomBoolean(), null, null, context));
}
use of org.apache.lucene.index.MultiReader in project elasticsearch by elastic.
the class NestedHelperTests method testNested.
public void testNested() throws IOException {
QueryShardContext context = indexService.newQueryShardContext(0, new MultiReader(), () -> 0);
NestedQueryBuilder queryBuilder = new NestedQueryBuilder("nested1", new MatchAllQueryBuilder(), ScoreMode.Avg);
ESToParentBlockJoinQuery query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
Query expectedChildQuery = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), Occur.MUST).add(new TermQuery(new Term("_type", "__nested1")), Occur.FILTER).build();
assertEquals(expectedChildQuery, query.getChildQuery());
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
queryBuilder = new NestedQueryBuilder("nested1", new TermQueryBuilder("nested1.foo", "bar"), ScoreMode.Avg);
query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
// this time we do not add a filter since the inner query only matches inner docs
expectedChildQuery = new TermQuery(new Term("nested1.foo", "bar"));
assertEquals(expectedChildQuery, query.getChildQuery());
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
queryBuilder = new NestedQueryBuilder("nested2", new TermQueryBuilder("nested2.foo", "bar"), ScoreMode.Avg);
query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
// we need to add the filter again because of include_in_parent
expectedChildQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("nested2.foo", "bar")), Occur.MUST).add(new TermQuery(new Term("_type", "__nested2")), Occur.FILTER).build();
assertEquals(expectedChildQuery, query.getChildQuery());
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
queryBuilder = new NestedQueryBuilder("nested3", new TermQueryBuilder("nested3.foo", "bar"), ScoreMode.Avg);
query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
// we need to add the filter again because of include_in_root
expectedChildQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("nested3.foo", "bar")), Occur.MUST).add(new TermQuery(new Term("_type", "__nested3")), Occur.FILTER).build();
assertEquals(expectedChildQuery, query.getChildQuery());
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
}
use of org.apache.lucene.index.MultiReader in project elasticsearch by elastic.
the class RangeQueryRewriteTests method testRewriteMissingField.
public void testRewriteMissingField() throws Exception {
IndexService indexService = createIndex("test");
IndexReader reader = new MultiReader();
QueryRewriteContext context = new QueryShardContext(0, indexService.getIndexSettings(), null, null, indexService.mapperService(), null, null, xContentRegistry(), null, reader, null);
RangeQueryBuilder range = new RangeQueryBuilder("foo");
assertEquals(Relation.DISJOINT, range.getRelation(context));
}
use of org.apache.lucene.index.MultiReader in project elasticsearch by elastic.
the class HDRPercentileRanksAggregatorTests method testEmpty.
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);
}
}
Aggregations