Search in sources :

Example 1 with BitSetProducer

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

the class BitSetFilterCacheTests method testRejectOtherIndex.

public void testRejectOtherIndex() throws IOException {
    BitsetFilterCache cache = new BitsetFilterCache(INDEX_SETTINGS, new BitsetFilterCache.Listener() {

        @Override
        public void onCache(ShardId shardId, Accountable accountable) {
        }

        @Override
        public void onRemoval(ShardId shardId, Accountable accountable) {
        }
    });
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig());
    writer.addDocument(new Document());
    DirectoryReader reader = DirectoryReader.open(writer);
    writer.close();
    reader = ElasticsearchDirectoryReader.wrap(reader, new ShardId("test2", "_na_", 0));
    BitSetProducer producer = cache.getBitSetProducer(new MatchAllDocsQuery());
    try {
        producer.getBitSet(reader.leaves().get(0));
        fail();
    } catch (IllegalStateException expected) {
        assertEquals("Trying to load bit set for index [test2] with cache of index [test]", expected.getMessage());
    } finally {
        IOUtils.close(reader, dir);
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexWriter(org.apache.lucene.index.IndexWriter) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) Accountable(org.apache.lucene.util.Accountable) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Example 2 with BitSetProducer

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

the class SortBuilder method resolveNested.

protected static Nested resolveNested(QueryShardContext context, String nestedPath, QueryBuilder nestedFilter) throws IOException {
    Nested nested = null;
    if (nestedPath != null) {
        BitSetProducer rootDocumentsFilter = context.bitsetFilter(Queries.newNonNestedFilter());
        ObjectMapper nestedObjectMapper = context.getObjectMapper(nestedPath);
        if (nestedObjectMapper == null) {
            throw new QueryShardException(context, "[nested] failed to find nested object under path [" + nestedPath + "]");
        }
        if (!nestedObjectMapper.nested().isNested()) {
            throw new QueryShardException(context, "[nested] nested object under path [" + nestedPath + "] is not of nested type");
        }
        Query innerDocumentsQuery;
        if (nestedFilter != null) {
            context.nestedScope().nextLevel(nestedObjectMapper);
            innerDocumentsQuery = QueryBuilder.rewriteQuery(nestedFilter, context).toFilter(context);
            context.nestedScope().previousLevel();
        } else {
            innerDocumentsQuery = nestedObjectMapper.nestedTypeFilter();
        }
        nested = new Nested(rootDocumentsFilter, innerDocumentsQuery);
    }
    return nested;
}
Also used : Query(org.apache.lucene.search.Query) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) Nested(org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) QueryShardException(org.elasticsearch.index.query.QueryShardException) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper)

Example 3 with BitSetProducer

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

the class NestedQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    ObjectMapper nestedObjectMapper = context.getObjectMapper(path);
    if (nestedObjectMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new IllegalStateException("[" + NAME + "] failed to find nested object under path [" + path + "]");
        }
    }
    if (!nestedObjectMapper.nested().isNested()) {
        throw new IllegalStateException("[" + NAME + "] nested object under path [" + path + "] is not of nested type");
    }
    final BitSetProducer parentFilter;
    Query innerQuery;
    ObjectMapper objectMapper = context.nestedScope().getObjectMapper();
    if (objectMapper == null) {
        parentFilter = context.bitsetFilter(Queries.newNonNestedFilter());
    } else {
        parentFilter = context.bitsetFilter(objectMapper.nestedTypeFilter());
    }
    try {
        context.nestedScope().nextLevel(nestedObjectMapper);
        innerQuery = this.query.toQuery(context);
    } finally {
        context.nestedScope().previousLevel();
    }
    // in its child space
    if (new NestedHelper(context.getMapperService()).mightMatchNonNestedDocs(innerQuery, path)) {
        innerQuery = Queries.filtered(innerQuery, nestedObjectMapper.nestedTypeFilter());
    }
    return new ESToParentBlockJoinQuery(innerQuery, parentFilter, scoreMode, objectMapper == null ? null : objectMapper.fullPath());
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) NestedHelper(org.elasticsearch.index.search.NestedHelper) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper)

Example 4 with BitSetProducer

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

the class SynonymTokenizer method testToChildBlockJoinQuery.

public void testToChildBlockJoinQuery() throws Exception {
    BitSetProducer parentFilter = new QueryBitSetProducer(new TermQuery(new Term(FIELD_NAME, "parent")));
    BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
    booleanQuery.add(new ToChildBlockJoinQuery(new TermQuery(new Term(FIELD_NAME, "parent")), parentFilter), Occur.MUST);
    booleanQuery.add(new TermQuery(new Term(FIELD_NAME, "child")), Occur.MUST);
    query = booleanQuery.build();
    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 = "parent document";
        final int docId = hits.scoreDocs[i].doc;
        TokenStream tokenStream = getAnyTokenStream(FIELD_NAME, docId);
        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) BooleanQuery(org.apache.lucene.search.BooleanQuery) CannedTokenStream(org.apache.lucene.analysis.CannedTokenStream) TokenStream(org.apache.lucene.analysis.TokenStream) Builder(org.apache.lucene.search.PhraseQuery.Builder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Term(org.apache.lucene.index.Term) ToChildBlockJoinQuery(org.apache.lucene.search.join.ToChildBlockJoinQuery) IntPoint(org.apache.lucene.document.IntPoint) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer)

Example 5 with BitSetProducer

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

the class ChildFieldValueSourceParser method parse.

@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
    final String sortFieldName = fp.parseArg();
    final Query query;
    if (fp.hasMoreArguments()) {
        query = fp.parseNestedQuery();
    } else {
        query = fp.subQuery(fp.getParam(CommonParams.Q), BlockJoinParentQParserPlugin.NAME).getQuery();
    }
    BitSetProducer parentFilter;
    BitSetProducer childFilter;
    SchemaField sf;
    try {
        AllParentsAware bjQ;
        if (!(query instanceof AllParentsAware)) {
            throw new SyntaxError("expect a reference to block join query " + AllParentsAware.class.getSimpleName() + " in " + fp.getString());
        }
        bjQ = (AllParentsAware) query;
        parentFilter = BlockJoinParentQParser.getCachedFilter(fp.getReq(), bjQ.getParentQuery()).filter;
        childFilter = BlockJoinParentQParser.getCachedFilter(fp.getReq(), bjQ.getChildQuery()).filter;
        if (sortFieldName == null || sortFieldName.equals("")) {
            throw new SyntaxError("field is omitted in " + fp.getString());
        }
        sf = fp.getReq().getSchema().getFieldOrNull(sortFieldName);
        if (null == sf) {
            throw new SyntaxError(NAME + " sort param field \"" + sortFieldName + "\" can't be found in schema");
        }
    } catch (SyntaxError e) {
        log.error("can't parse " + fp.getString(), e);
        throw e;
    }
    return new BlockJoinSortFieldValueSource(childFilter, parentFilter, sf);
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) Query(org.apache.lucene.search.Query) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) SyntaxError(org.apache.solr.search.SyntaxError) AllParentsAware(org.apache.solr.search.join.BlockJoinParentQParser.AllParentsAware)

Aggregations

BitSetProducer (org.apache.lucene.search.join.BitSetProducer)10 Term (org.apache.lucene.index.Term)5 Query (org.apache.lucene.search.Query)5 TermQuery (org.apache.lucene.search.TermQuery)5 QueryBitSetProducer (org.apache.lucene.search.join.QueryBitSetProducer)5 Document (org.apache.lucene.document.Document)4 StringField (org.apache.lucene.document.StringField)3 DirectoryReader (org.apache.lucene.index.DirectoryReader)3 IndexWriter (org.apache.lucene.index.IndexWriter)3 RAMDirectory (org.apache.lucene.store.RAMDirectory)3 Accountable (org.apache.lucene.util.Accountable)3 ElasticsearchDirectoryReader (org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader)3 ShardId (org.elasticsearch.index.shard.ShardId)3 CannedTokenStream (org.apache.lucene.analysis.CannedTokenStream)2 TokenStream (org.apache.lucene.analysis.TokenStream)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 IntPoint (org.apache.lucene.document.IntPoint)2 IndexReader (org.apache.lucene.index.IndexReader)2 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)2 LogByteSizeMergePolicy (org.apache.lucene.index.LogByteSizeMergePolicy)2