Search in sources :

Example 21 with MultiReader

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);
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Term(org.apache.lucene.index.Term) HashSet(java.util.HashSet)

Example 22 with MultiReader

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());
}
Also used : MultiReader(org.apache.lucene.index.MultiReader)

Example 23 with MultiReader

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;
}
Also used : SuperIndexSearcher(org.opensolaris.opengrok.configuration.SuperIndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) MultiReader(org.apache.lucene.index.MultiReader) Query(org.apache.lucene.search.Query) ParseException(org.apache.lucene.queryparser.classic.ParseException) CompatibleAnalyser(org.opensolaris.opengrok.analysis.CompatibleAnalyser) SuggestMode(org.apache.lucene.search.spell.SuggestMode) SortedSet(java.util.SortedSet) ScoreDoc(org.apache.lucene.search.ScoreDoc) IOUtils(org.opensolaris.opengrok.util.IOUtils) Term(org.apache.lucene.index.Term) QueryBuilder(org.opensolaris.opengrok.search.QueryBuilder) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) LoggerFactory(org.opensolaris.opengrok.logger.LoggerFactory) Document(org.apache.lucene.document.Document) Definitions(org.opensolaris.opengrok.analysis.Definitions) Project(org.opensolaris.opengrok.configuration.Project) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) Map(java.util.Map) SortField(org.apache.lucene.search.SortField) FSDirectory(org.apache.lucene.store.FSDirectory) AnalyzerGuru(org.opensolaris.opengrok.analysis.AnalyzerGuru) SuggestWord(org.apache.lucene.search.spell.SuggestWord) SuperIndexSearcher(org.opensolaris.opengrok.configuration.SuperIndexSearcher) Sort(org.apache.lucene.search.Sort) DirectoryReader(org.apache.lucene.index.DirectoryReader) DirectSpellChecker(org.apache.lucene.search.spell.DirectSpellChecker) Set(java.util.Set) IOException(java.io.IOException) Summarizer(org.opensolaris.opengrok.search.Summarizer) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) IndexDatabase(org.opensolaris.opengrok.index.IndexDatabase) HistoryContext(org.opensolaris.opengrok.search.context.HistoryContext) Pattern(java.util.regex.Pattern) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) Context(org.opensolaris.opengrok.search.context.Context) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) MultiReader(org.apache.lucene.index.MultiReader) FileNotFoundException(java.io.FileNotFoundException) FSDirectory(org.apache.lucene.store.FSDirectory) SortField(org.apache.lucene.search.SortField) IOException(java.io.IOException) Project(org.opensolaris.opengrok.configuration.Project) Sort(org.apache.lucene.search.Sort) ParseException(org.apache.lucene.queryparser.classic.ParseException) File(java.io.File) DirectSpellChecker(org.apache.lucene.search.spell.DirectSpellChecker)

Example 24 with MultiReader

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;
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) IndexReader(org.apache.lucene.index.IndexReader) IOException(java.io.IOException)

Example 25 with 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);
}
Also used : LuceneIndexReader(org.apache.jackrabbit.oak.plugins.index.lucene.reader.LuceneIndexReader) MultiReader(org.apache.lucene.index.MultiReader) LuceneIndexReader(org.apache.jackrabbit.oak.plugins.index.lucene.reader.LuceneIndexReader) IndexReader(org.apache.lucene.index.IndexReader)

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