use of org.apache.lucene.index.MultiReader in project lucene-solr by apache.
the class TestConstantScoreQuery method testExtractTerms.
public void testExtractTerms() throws Exception {
final IndexSearcher searcher = newSearcher(new MultiReader());
final TermQuery termQuery = new TermQuery(new Term("foo", "bar"));
final ConstantScoreQuery csq = new ConstantScoreQuery(termQuery);
final Set<Term> scoringTerms = new HashSet<>();
searcher.createNormalizedWeight(csq, true).extractTerms(scoringTerms);
assertEquals(Collections.emptySet(), scoringTerms);
final Set<Term> matchingTerms = new HashSet<>();
searcher.createNormalizedWeight(csq, false).extractTerms(matchingTerms);
assertEquals(Collections.singleton(new Term("foo", "bar")), matchingTerms);
}
use of org.apache.lucene.index.MultiReader in project lucene-solr by apache.
the class TestIndexSearcher method testGetQueryCache.
public void testGetQueryCache() throws IOException {
IndexSearcher searcher = new IndexSearcher(new MultiReader());
assertEquals(IndexSearcher.getDefaultQueryCache(), searcher.getQueryCache());
QueryCache dummyCache = new QueryCache() {
@Override
public Weight doCache(Weight weight, QueryCachingPolicy policy) {
return weight;
}
};
searcher.setQueryCache(dummyCache);
assertEquals(dummyCache, searcher.getQueryCache());
IndexSearcher.setDefaultQueryCache(dummyCache);
searcher = new IndexSearcher(new MultiReader());
assertEquals(dummyCache, searcher.getQueryCache());
searcher.setQueryCache(null);
assertNull(searcher.getQueryCache());
IndexSearcher.setDefaultQueryCache(null);
searcher = new IndexSearcher(new MultiReader());
assertNull(searcher.getQueryCache());
}
use of org.apache.lucene.index.MultiReader in project OpenGrok by OpenGrok.
the class SearchHelper method prepareExec.
/**
* Create the searcher to use w.r.t. currently set parameters and the given
* projects. Does not produce any {@link #redirect} link. It also does
* nothing if {@link #redirect} or {@link #errorMsg} have a
* none-{@code null} value.
* <p>
* Parameters which should be populated/set at this time:
* <ul>
* <li>{@link #builder}</li> <li>{@link #dataRoot}</li>
* <li>{@link #order} (falls back to relevance if unset)</li>
* <li>{@link #parallel} (default: false)</li> </ul> Populates/sets: <ul>
* <li>{@link #query}</li> <li>{@link #searcher}</li> <li>{@link #sort}</li>
* <li>{@link #projects}</li> <li>{@link #errorMsg} if an error occurs</li>
* </ul>
*
* @param projects project names. If empty, a no-project setup
* is assumed (i.e. DATA_ROOT/index will be used instead of possible
* multiple DATA_ROOT/$project/index). If the set contains projects
* not known in the configuration or projects not yet indexed,
* an error will be returned in {@link #errorMsg}.
* @return this instance
*/
public SearchHelper prepareExec(SortedSet<String> projects) {
if (redirect != null || errorMsg != null) {
return this;
}
// the Query created by the QueryBuilder
try {
indexDir = new File(dataRoot, IndexDatabase.INDEX_DIR);
query = builder.build();
if (projects == null) {
errorMsg = "No project selected!";
return this;
}
this.projects = projects;
if (projects.isEmpty()) {
// no project setup
FSDirectory dir = FSDirectory.open(indexDir.toPath());
searcher = new IndexSearcher(DirectoryReader.open(dir));
closeOnDestroy = true;
} else {
// Check list of project names first to make sure all of them
// are valid and indexed.
closeOnDestroy = false;
Set<String> invalidProjects = projects.stream().filter(proj -> (Project.getByName(proj) == null)).collect(Collectors.toSet());
if (invalidProjects.size() > 0) {
errorMsg = "Project list contains invalid projects: " + String.join(", ", invalidProjects);
return this;
}
Set<Project> notIndexedProjects = projects.stream().map(x -> Project.getByName(x)).filter(proj -> !proj.isIndexed()).collect(Collectors.toSet());
if (notIndexedProjects.size() > 0) {
errorMsg = "Some of the projects to be searched are not indexed yet: " + String.join(", ", notIndexedProjects.stream().map(proj -> proj.getName()).collect(Collectors.toSet()));
return this;
}
// 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 multireader = RuntimeEnvironment.getInstance().getMultiReader(projects, searcherList);
if (multireader != null) {
searcher = new IndexSearcher(multireader);
} else {
errorMsg = "Failed to initialize search. Check the index.";
return this;
}
}
// Most probably they are not reused. SearcherLifetimeManager might help here.
switch(order) {
case LASTMODIFIED:
sort = new Sort(new SortField(QueryBuilder.DATE, SortField.Type.STRING, true));
break;
case BY_PATH:
sort = new Sort(new SortField(QueryBuilder.FULLPATH, SortField.Type.STRING));
break;
default:
sort = Sort.RELEVANCE;
break;
}
checker = new DirectSpellChecker();
} catch (ParseException e) {
errorMsg = PARSE_ERROR_MSG + e.getMessage();
} catch (FileNotFoundException e) {
// errorMsg = "Index database(s) not found: " + e.getMessage();
errorMsg = "Index database(s) not found.";
} catch (IOException e) {
errorMsg = e.getMessage();
}
return this;
}
use of org.apache.lucene.index.MultiReader in project OpenGrok by OpenGrok.
the class RuntimeEnvironment method getMultiReader.
/**
* Return collection of IndexReader objects as MultiReader object
* for given list of projects.
* The caller is responsible for releasing the IndexSearcher objects
* so we add them to the map.
*
* @param projects list of projects
* @param searcherList each SuperIndexSearcher produced will be put into this list
* @return MultiReader for the projects
*/
public MultiReader getMultiReader(SortedSet<String> projects, ArrayList<SuperIndexSearcher> searcherList) {
IndexReader[] subreaders = new IndexReader[projects.size()];
int ii = 0;
// String , need changes in projects.jspf too
for (String proj : projects) {
try {
SuperIndexSearcher searcher = RuntimeEnvironment.getInstance().getIndexSearcher(proj);
subreaders[ii++] = searcher.getIndexReader();
searcherList.add(searcher);
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "cannot get IndexReader for project " + proj, ex);
return null;
} catch (NullPointerException ex) {
LOGGER.log(Level.SEVERE, "cannot get IndexReader for project " + proj, ex);
return null;
}
}
MultiReader multiReader = null;
try {
multiReader = new MultiReader(subreaders, true);
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "cannot construct MultiReader for set of projects", ex);
}
return multiReader;
}
use of org.apache.lucene.index.MultiReader in project jackrabbit-oak by apache.
the class IndexNodeManager method createReader.
private IndexReader createReader(List<LuceneIndexReader> nrtReaders) {
if (readers.size() == 1 && nrtReaders.isEmpty()) {
IndexReader reader = readers.get(0).getReader();
reader.incRef();
return reader;
}
if (nrtReaders.size() == 1 && readers.isEmpty()) {
IndexReader reader = nrtReaders.get(0).getReader();
reader.incRef();
return reader;
}
IndexReader[] readerArr = new IndexReader[readers.size() + nrtReaders.size()];
int i = 0;
for (LuceneIndexReader r : Iterables.concat(readers, nrtReaders)) {
readerArr[i++] = r.getReader();
}
return new MultiReader(readerArr, false);
}
Aggregations