Example usage for org.apache.lucene.index MultiReader MultiReader

List of usage examples for org.apache.lucene.index MultiReader MultiReader

Introduction

In this page you can find the example usage for org.apache.lucene.index MultiReader MultiReader.

Prototype

MultiReader

Source Link

Usage

From source file:org.apache.solr.uninverting.TestFieldCacheSort.java

License:Apache License

/** test sorts when there's nothing in the index */
public void testEmptyIndex() throws Exception {
    IndexSearcher empty = newSearcher(new MultiReader());
    Query query = new TermQuery(new Term("contents", "foo"));

    Sort sort = new Sort();
    TopDocs td = empty.search(query, 10, sort, true, true);
    assertEquals(0, td.totalHits);//from  w w w  . j a  v a2 s  .  c om

    sort.setSort(SortField.FIELD_DOC);
    td = empty.search(query, 10, sort, true, true);
    assertEquals(0, td.totalHits);

    sort.setSort(new SortField("int", SortField.Type.INT), SortField.FIELD_DOC);
    td = empty.search(query, 10, sort, true, true);
    assertEquals(0, td.totalHits);

    sort.setSort(new SortField("string", SortField.Type.STRING, true), SortField.FIELD_DOC);
    td = empty.search(query, 10, sort, true, true);
    assertEquals(0, td.totalHits);

    sort.setSort(new SortField("string_val", SortField.Type.STRING_VAL, true), SortField.FIELD_DOC);
    td = empty.search(query, 10, sort, true, true);
    assertEquals(0, td.totalHits);

    sort.setSort(new SortField("float", SortField.Type.FLOAT), new SortField("string", SortField.Type.STRING));
    td = empty.search(query, 10, sort, true, true);
    assertEquals(0, td.totalHits);
}

From source file:org.elasticsearch.index.mapper.core.DateFieldTypeTests.java

License:Apache License

public void testIsFieldWithinQueryEmptyReader() throws IOException {
    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));
}

From source file:org.elasticsearch.index.mapper.core.DateFieldTypeTests.java

License:Apache License

public void testRangeQuery() throws IOException {
    MappedFieldType ft = createDefaultFieldType();
    ft.setName("field");
    String date1 = "2015-10-12T14:10:55";
    String date2 = "2016-04-28T11:33:52";
    long instant1 = LegacyDateFieldMapper.Defaults.DATE_TIME_FORMATTER.parser().parseDateTime(date1)
            .getMillis();/*from www .  jav  a  2s  .  c om*/
    long instant2 = LegacyDateFieldMapper.Defaults.DATE_TIME_FORMATTER.parser().parseDateTime(date2)
            .getMillis();
    ft.setIndexOptions(IndexOptions.DOCS);
    assertEquals(LongPoint.newRangeQuery("field", instant1, instant2),
            ft.rangeQuery(date1, date2, true, true).rewrite(new MultiReader()));

    ft.setIndexOptions(IndexOptions.NONE);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
            () -> ft.rangeQuery(date1, date2, true, true));
    assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}

From source file:org.elasticsearch.index.query.RangeQueryRewriteTests.java

License:Apache License

public void testRewriteMissingField() throws Exception {
    IndexService indexService = createIndex("test");
    IndexReader reader = new MultiReader();
    QueryRewriteContext context = new QueryRewriteContext(indexService.getIndexSettings(),
            indexService.mapperService(), null, null, null, reader, null);
    RangeQueryBuilder range = new RangeQueryBuilder("foo");
    assertEquals(Relation.DISJOINT, range.getRelation(context));
}

From source file:org.elasticsearch.index.query.RangeQueryRewriteTests.java

License:Apache License

public void testRewriteEmptyReader() throws Exception {
    IndexService indexService = createIndex("test");
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
            .startObject("foo").field("type", "date").endObject().endObject().endObject().endObject().string();
    indexService.mapperService().merge("type", new CompressedXContent(mapping), MergeReason.MAPPING_UPDATE,
            false);//from  w  w  w. j  a  v  a 2s .  c  om
    IndexReader reader = new MultiReader();
    QueryRewriteContext context = new QueryRewriteContext(indexService.getIndexSettings(),
            indexService.mapperService(), null, null, null, reader, null);
    RangeQueryBuilder range = new RangeQueryBuilder("foo");
    // no values -> DISJOINT
    assertEquals(Relation.DISJOINT, range.getRelation(context));
}

From source file:org.elasticsearch.index.search.ESToParentBlockJoinQueryTests.java

License:Apache License

public void testRewrite() throws IOException {
    Query q = new ESToParentBlockJoinQuery(new PhraseQuery("body", "term"), // rewrites to a TermQuery
            new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))), ScoreMode.Avg, "nested");
    Query expected = new ESToParentBlockJoinQuery(new TermQuery(new Term("body", "term")),
            new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))), ScoreMode.Avg, "nested");
    Query rewritten = q.rewrite(new MultiReader());
    assertEquals(expected, rewritten);//from  w  w w.jav  a  2  s  . c  o  m
}

From source file:org.elasticsearch.index.search.NestedHelperTests.java

License:Apache License

public void testNested() throws IOException {
    QueryShardContext context = indexService.newQueryShardContext(0, new MultiReader(), () -> 0);
    NestedQueryBuilder queryBuilder = new NestedQueryBuilder("nested1", new MatchAllQueryBuilder(),
            ScoreMode.Avg);/* w w w  .  j  a  v a  2s  .c o m*/
    ESToParentBlockJoinQuery query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);

    Query expectedChildQuery = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), Occur.MUST)
            // we automatically add a filter since the inner query might match non-nested docs
            .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"));
}

From source file:org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregatorTests.java

License:Apache License

public void testUnmappedField() throws Exception {
    TermsValuesSourceBuilder terms = new TermsValuesSourceBuilder(randomAlphaOfLengthBetween(5, 10))
            .field("unknown");
    CompositeAggregationBuilder builder = new CompositeAggregationBuilder("test",
            Collections.singletonList(terms));
    IndexSearcher searcher = new IndexSearcher(new MultiReader());
    QueryShardException exc = expectThrows(QueryShardException.class,
            () -> createAggregatorFactory(builder, searcher));
    assertThat(exc.getMessage(),//www  . j  a  v  a  2s. com
            containsString("failed to find field [unknown] and [missing] is not provided"));
    // should work when missing is provided
    terms.missing("missing");
    createAggregatorFactory(builder, searcher);
}

From source file:org.elasticsearch.search.aggregations.bucket.filter.FilterAggregatorTests.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"));
    FilterAggregationBuilder builder = new FilterAggregationBuilder("test", filter);
    AggregatorFactory<?> factory = createAggregatorFactory(builder, indexSearcher, fieldType);
    assertThat(factory, Matchers.instanceOf(FilterAggregatorFactory.class));
    FilterAggregatorFactory filterFactory = (FilterAggregatorFactory) factory;
    Query parsedQuery = filterFactory.getWeight().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//from  w w w  .ja  v  a  2s .  c om
    assertEquals(1, ((BooleanQuery) parsedQuery).getMinimumNumberShouldMatch());
}

From source file:org.elasticsearch.search.aggregations.bucket.filter.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.getWeights()[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 ww.j  av  a 2 s  .c o m
    assertEquals(1, ((BooleanQuery) parsedQuery).getMinimumNumberShouldMatch());
}