Search in sources :

Example 11 with QueryBitSetProducer

use of org.apache.lucene.search.join.QueryBitSetProducer in project elasticsearch by elastic.

the class NestedChildrenFilterTests method testNestedChildrenFilter.

public void testNestedChildrenFilter() throws Exception {
    int numParentDocs = scaledRandomIntBetween(0, 32);
    int maxChildDocsPerParent = scaledRandomIntBetween(8, 16);
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    for (int i = 0; i < numParentDocs; i++) {
        int numChildDocs = scaledRandomIntBetween(0, maxChildDocsPerParent);
        List<Document> docs = new ArrayList<>(numChildDocs + 1);
        for (int j = 0; j < numChildDocs; j++) {
            Document childDoc = new Document();
            childDoc.add(new StringField("type", "child", Field.Store.NO));
            docs.add(childDoc);
        }
        Document parenDoc = new Document();
        parenDoc.add(new StringField("type", "parent", Field.Store.NO));
        parenDoc.add(new LegacyIntField("num_child_docs", numChildDocs, Field.Store.YES));
        docs.add(parenDoc);
        writer.addDocuments(docs);
    }
    IndexReader reader = writer.getReader();
    writer.close();
    IndexSearcher searcher = new IndexSearcher(reader);
    FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext();
    BitSetProducer parentFilter = new QueryBitSetProducer(new TermQuery(new Term("type", "parent")));
    Query childFilter = new TermQuery(new Term("type", "child"));
    int checkedParents = 0;
    final Weight parentsWeight = searcher.createNormalizedWeight(new TermQuery(new Term("type", "parent")), false);
    for (LeafReaderContext leaf : reader.leaves()) {
        DocIdSetIterator parents = parentsWeight.scorer(leaf).iterator();
        for (int parentDoc = parents.nextDoc(); parentDoc != DocIdSetIterator.NO_MORE_DOCS; parentDoc = parents.nextDoc()) {
            int expectedChildDocs = leaf.reader().document(parentDoc).getField("num_child_docs").numericValue().intValue();
            hitContext.reset(null, leaf, parentDoc, searcher);
            NestedChildrenQuery nestedChildrenFilter = new NestedChildrenQuery(parentFilter, childFilter, hitContext);
            TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
            searcher.search(new ConstantScoreQuery(nestedChildrenFilter), totalHitCountCollector);
            assertThat(totalHitCountCollector.getTotalHits(), equalTo(expectedChildDocs));
            checkedParents++;
        }
    }
    assertThat(checkedParents, equalTo(numParentDocs));
    reader.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) NestedChildrenQuery(org.elasticsearch.search.fetch.subphase.InnerHitsContext.NestedInnerHits.NestedChildrenQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) Weight(org.apache.lucene.search.Weight) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) FetchSubPhase(org.elasticsearch.search.fetch.FetchSubPhase) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) NestedChildrenQuery(org.elasticsearch.search.fetch.subphase.InnerHitsContext.NestedInnerHits.NestedChildrenQuery) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) LegacyIntField(org.apache.lucene.document.LegacyIntField)

Example 12 with QueryBitSetProducer

use of org.apache.lucene.search.join.QueryBitSetProducer in project lucene-solr by apache.

the class ChildDocTransformer method create.

@Override
public DocTransformer create(String field, SolrParams params, SolrQueryRequest req) {
    SchemaField uniqueKeyField = req.getSchema().getUniqueKeyField();
    if (uniqueKeyField == null) {
        throw new SolrException(ErrorCode.BAD_REQUEST, " ChildDocTransformer requires the schema to have a uniqueKeyField.");
    }
    String parentFilter = params.get("parentFilter");
    if (parentFilter == null) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "Parent filter should be sent as parentFilter=filterCondition");
    }
    String childFilter = params.get("childFilter");
    int limit = params.getInt("limit", 10);
    BitSetProducer parentsFilter = null;
    try {
        Query parentFilterQuery = QParser.getParser(parentFilter, req).getQuery();
        parentsFilter = new QueryBitSetProducer(new QueryWrapperFilter(parentFilterQuery));
    } catch (SyntaxError syntaxError) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "Failed to create correct parent filter query");
    }
    Query childFilterQuery = null;
    if (childFilter != null) {
        try {
            childFilterQuery = QParser.getParser(childFilter, req).getQuery();
        } catch (SyntaxError syntaxError) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "Failed to create correct child filter query");
        }
    }
    return new ChildDocTransformer(field, parentsFilter, uniqueKeyField, req.getSchema(), childFilterQuery, limit);
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) Query(org.apache.lucene.search.Query) ToChildBlockJoinQuery(org.apache.lucene.search.join.ToChildBlockJoinQuery) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) SyntaxError(org.apache.solr.search.SyntaxError) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) QueryWrapperFilter(org.apache.solr.search.QueryWrapperFilter) SolrException(org.apache.solr.common.SolrException)

Example 13 with QueryBitSetProducer

use of org.apache.lucene.search.join.QueryBitSetProducer in project lucene-solr by apache.

the class TestHierarchicalDocBuilder method createParentFilter.

private BitSetProducer createParentFilter(String type) {
    BooleanQuery.Builder parentQuery = new BooleanQuery.Builder();
    parentQuery.add(new TermQuery(new Term("type_s", type)), Occur.MUST);
    return new QueryBitSetProducer(parentQuery.build());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) Term(org.apache.lucene.index.Term)

Example 14 with QueryBitSetProducer

use of org.apache.lucene.search.join.QueryBitSetProducer in project lucene-solr by apache.

the class SynonymTokenizer method testToParentBlockJoinQuery.

public void testToParentBlockJoinQuery() throws Exception {
    BitSetProducer parentFilter = new QueryBitSetProducer(new TermQuery(new Term(FIELD_NAME, "parent")));
    query = new ToParentBlockJoinQuery(new TermQuery(new Term(FIELD_NAME, "child")), parentFilter, ScoreMode.None);
    searcher = newSearcher(reader);
    hits = searcher.search(query, 100);
    int maxNumFragmentsRequired = 2;
    QueryScorer scorer = new QueryScorer(query, FIELD_NAME);
    Highlighter highlighter = new Highlighter(this, scorer);
    for (int i = 0; i < hits.totalHits; i++) {
        String text = "child document";
        TokenStream tokenStream = analyzer.tokenStream(FIELD_NAME, text);
        highlighter.setTextFragmenter(new SimpleFragmenter(40));
        highlighter.getBestFragments(tokenStream, text, maxNumFragmentsRequired, "...");
    }
    assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 1);
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) CannedTokenStream(org.apache.lucene.analysis.CannedTokenStream) TokenStream(org.apache.lucene.analysis.TokenStream) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) Term(org.apache.lucene.index.Term) IntPoint(org.apache.lucene.document.IntPoint)

Aggregations

QueryBitSetProducer (org.apache.lucene.search.join.QueryBitSetProducer)14 Query (org.apache.lucene.search.Query)11 TermQuery (org.apache.lucene.search.TermQuery)11 Term (org.apache.lucene.index.Term)9 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)8 ToParentBlockJoinQuery (org.apache.lucene.search.join.ToParentBlockJoinQuery)8 Sort (org.apache.lucene.search.Sort)6 SortField (org.apache.lucene.search.SortField)6 MultiValueMode (org.elasticsearch.search.MultiValueMode)5 ArrayList (java.util.ArrayList)4 Document (org.apache.lucene.document.Document)4 StringField (org.apache.lucene.document.StringField)4 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 BitSetProducer (org.apache.lucene.search.join.BitSetProducer)4 XFieldComparatorSource (org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource)4 DirectoryReader (org.apache.lucene.index.DirectoryReader)3 TopDocs (org.apache.lucene.search.TopDocs)3 TopFieldDocs (org.apache.lucene.search.TopFieldDocs)3 ElasticsearchDirectoryReader (org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader)3