Search in sources :

Example 41 with MultiReader

use of org.apache.lucene.index.MultiReader in project lucene-solr by apache.

the class TestBooleanRewrites method testMatchAllMustNot.

// MatchAllQuery as MUST_NOT clause cannot return anything
public void testMatchAllMustNot() throws IOException {
    IndexSearcher searcher = newSearcher(new MultiReader());
    // Test Must with MatchAll MustNot
    BooleanQuery bq = new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.MUST).add(new TermQuery(new Term("foo", "baz")), Occur.FILTER).add(new TermQuery(new Term("foo", "bad")), Occur.SHOULD).add(new MatchAllDocsQuery(), Occur.MUST_NOT).build();
    assertEquals(new MatchNoDocsQuery(), searcher.rewrite(bq));
    // Test Must with MatchAll MustNot and other MustNot
    BooleanQuery bq2 = new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.MUST).add(new TermQuery(new Term("foo", "baz")), Occur.FILTER).add(new TermQuery(new Term("foo", "bad")), Occur.SHOULD).add(new TermQuery(new Term("foo", "bor")), Occur.MUST_NOT).add(new MatchAllDocsQuery(), Occur.MUST_NOT).build();
    assertEquals(new MatchNoDocsQuery(), searcher.rewrite(bq2));
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Term(org.apache.lucene.index.Term)

Example 42 with MultiReader

use of org.apache.lucene.index.MultiReader in project lucene-solr by apache.

the class TestBooleanRewrites method testConvertShouldAndFilterToMust.

// Duplicate Should and Filter query is converted to Must (with minShouldMatch -1)
public void testConvertShouldAndFilterToMust() throws IOException {
    IndexSearcher searcher = newSearcher(new MultiReader());
    // no minShouldMatch
    BooleanQuery bq = new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.SHOULD).add(new TermQuery(new Term("foo", "bar")), Occur.FILTER).build();
    assertEquals(new TermQuery(new Term("foo", "bar")), searcher.rewrite(bq));
    // minShouldMatch is set to -1
    bq = new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.SHOULD).add(new TermQuery(new Term("foo", "bar")), Occur.FILTER).add(new TermQuery(new Term("foo", "baz")), Occur.SHOULD).add(new TermQuery(new Term("foo", "quz")), Occur.SHOULD).setMinimumNumberShouldMatch(2).build();
    BooleanQuery expected = new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.MUST).add(new TermQuery(new Term("foo", "baz")), Occur.SHOULD).add(new TermQuery(new Term("foo", "quz")), Occur.SHOULD).setMinimumNumberShouldMatch(1).build();
    assertEquals(expected, searcher.rewrite(bq));
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Term(org.apache.lucene.index.Term)

Example 43 with MultiReader

use of org.apache.lucene.index.MultiReader in project lucene-solr by apache.

the class TestBooleanQuery method testDeMorgan.

public void testDeMorgan() throws Exception {
    Directory dir1 = newDirectory();
    RandomIndexWriter iw1 = new RandomIndexWriter(random(), dir1);
    Document doc1 = new Document();
    doc1.add(newTextField("field", "foo bar", Field.Store.NO));
    iw1.addDocument(doc1);
    IndexReader reader1 = iw1.getReader();
    iw1.close();
    Directory dir2 = newDirectory();
    RandomIndexWriter iw2 = new RandomIndexWriter(random(), dir2);
    Document doc2 = new Document();
    doc2.add(newTextField("field", "foo baz", Field.Store.NO));
    iw2.addDocument(doc2);
    IndexReader reader2 = iw2.getReader();
    iw2.close();
    // Query: +foo -ba*
    BooleanQuery.Builder query = new BooleanQuery.Builder();
    query.add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.MUST);
    WildcardQuery wildcardQuery = new WildcardQuery(new Term("field", "ba*"));
    wildcardQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
    query.add(wildcardQuery, BooleanClause.Occur.MUST_NOT);
    MultiReader multireader = new MultiReader(reader1, reader2);
    IndexSearcher searcher = newSearcher(multireader);
    assertEquals(0, searcher.search(query.build(), 10).totalHits);
    final ExecutorService es = Executors.newCachedThreadPool(new NamedThreadFactory("NRT search threads"));
    searcher = new IndexSearcher(multireader, es);
    if (VERBOSE)
        System.out.println("rewritten form: " + searcher.rewrite(query.build()));
    assertEquals(0, searcher.search(query.build(), 10).totalHits);
    es.shutdown();
    es.awaitTermination(1, TimeUnit.SECONDS);
    multireader.close();
    reader1.close();
    reader2.close();
    dir1.close();
    dir2.close();
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MultiReader(org.apache.lucene.index.MultiReader) NamedThreadFactory(org.apache.lucene.util.NamedThreadFactory) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) IndexReader(org.apache.lucene.index.IndexReader) ExecutorService(java.util.concurrent.ExecutorService) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 44 with MultiReader

use of org.apache.lucene.index.MultiReader in project lucene-solr by apache.

the class OrdFieldSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final int off = readerContext.docBase;
    final LeafReader r;
    Object o = context.get("searcher");
    if (o instanceof SolrIndexSearcher) {
        SolrIndexSearcher is = (SolrIndexSearcher) o;
        SchemaField sf = is.getSchema().getFieldOrNull(field);
        if (sf != null && sf.hasDocValues() == false && sf.multiValued() == false && sf.getType().getNumberType() != null) {
            // it's a single-valued numeric field: we must currently create insanity :(
            List<LeafReaderContext> leaves = is.getIndexReader().leaves();
            LeafReader[] insaneLeaves = new LeafReader[leaves.size()];
            int upto = 0;
            for (LeafReaderContext raw : leaves) {
                insaneLeaves[upto++] = Insanity.wrapInsanity(raw.reader(), field);
            }
            r = SlowCompositeReaderWrapper.wrap(new MultiReader(insaneLeaves));
        } else {
            // reuse ordinalmap
            r = ((SolrIndexSearcher) o).getSlowAtomicReader();
        }
    } else {
        IndexReader topReader = ReaderUtil.getTopLevelContext(readerContext).reader();
        r = SlowCompositeReaderWrapper.wrap(topReader);
    }
    // if it's e.g. tokenized/multivalued, emulate old behavior of single-valued fc
    final SortedDocValues sindex = SortedSetSelector.wrap(DocValues.getSortedSet(r, field), SortedSetSelector.Type.MIN);
    return new IntDocValues(this) {

        private int lastDocID;

        private int getOrdForDoc(int docID) throws IOException {
            if (docID < lastDocID) {
                throw new IllegalArgumentException("docs out of order: lastDocID=" + lastDocID + " docID=" + docID);
            }
            if (docID > sindex.docID()) {
                sindex.advance(docID);
            }
            if (docID == sindex.docID()) {
                return sindex.ordValue();
            } else {
                return -1;
            }
        }

        protected String toTerm(String readableValue) {
            return readableValue;
        }

        @Override
        public int intVal(int doc) throws IOException {
            return getOrdForDoc(doc + off);
        }

        @Override
        public int ordVal(int doc) throws IOException {
            return getOrdForDoc(doc + off);
        }

        @Override
        public int numOrd() {
            return sindex.getValueCount();
        }

        @Override
        public boolean exists(int doc) throws IOException {
            return getOrdForDoc(doc + off) != 0;
        }

        @Override
        public ValueFiller getValueFiller() {
            return new ValueFiller() {

                private final MutableValueInt mval = new MutableValueInt();

                @Override
                public MutableValue getValue() {
                    return mval;
                }

                @Override
                public void fillValue(int doc) throws IOException {
                    mval.value = getOrdForDoc(doc);
                    mval.exists = mval.value != 0;
                }
            };
        }
    };
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) MultiReader(org.apache.lucene.index.MultiReader) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SortedDocValues(org.apache.lucene.index.SortedDocValues) SchemaField(org.apache.solr.schema.SchemaField) IndexReader(org.apache.lucene.index.IndexReader) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) MutableValueInt(org.apache.lucene.util.mutable.MutableValueInt) IntDocValues(org.apache.lucene.queries.function.docvalues.IntDocValues)

Example 45 with MultiReader

use of org.apache.lucene.index.MultiReader in project OpenGrok by OpenGrok.

the class SearchEngine method searchMultiDatabase.

/**
 * Perform search on multiple indexes in parallel.
 * @param paging whether to use paging (if yes, first X pages will load
 * faster)
 * @param root list of projects to search
 * @throws IOException
 */
private void searchMultiDatabase(List<Project> root, boolean paging) throws IOException {
    SortedSet<String> projects = new TreeSet<>();
    for (Project p : root) {
        projects.add(p.getName());
    }
    // We use MultiReader even for single project. This should
    // not matter given that MultiReader is just a cheap wrapper
    // around set of IndexReader objects.
    MultiReader searchables = RuntimeEnvironment.getInstance().getMultiReader(projects, searcherList);
    searcher = new IndexSearcher(searchables);
    collector = TopScoreDocCollector.create(hitsPerPage * cachePages);
    searcher.search(query, collector);
    totalHits = collector.getTotalHits();
    if (!paging && totalHits > 0) {
        collector = TopScoreDocCollector.create(totalHits);
        searcher.search(query, collector);
    }
    hits = collector.topDocs().scoreDocs;
    for (ScoreDoc hit : hits) {
        int docId = hit.doc;
        Document d = searcher.doc(docId);
        docs.add(d);
    }
}
Also used : SuperIndexSearcher(org.opensolaris.opengrok.configuration.SuperIndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) Project(org.opensolaris.opengrok.configuration.Project) TreeSet(java.util.TreeSet) MultiReader(org.apache.lucene.index.MultiReader) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Aggregations

MultiReader (org.apache.lucene.index.MultiReader)47 IndexReader (org.apache.lucene.index.IndexReader)20 Term (org.apache.lucene.index.Term)20 IndexSearcher (org.apache.lucene.search.IndexSearcher)14 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)7 Document (org.apache.lucene.document.Document)6 Query (org.apache.lucene.search.Query)6 IOException (java.io.IOException)5 TermQuery (org.apache.lucene.search.TermQuery)5 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 HashSet (java.util.HashSet)3 List (java.util.List)3 TreeSet (java.util.TreeSet)3 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)3 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)3 Sort (org.apache.lucene.search.Sort)3 ArrayList (java.util.ArrayList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 LuceneIndexReader (org.apache.jackrabbit.oak.plugins.index.lucene.reader.LuceneIndexReader)2 LeafReader (org.apache.lucene.index.LeafReader)2