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));
}
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));
}
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();
}
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;
}
};
}
};
}
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);
}
}
Aggregations