Search in sources :

Example 1 with SearcherManager

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

the class Engine method acquireSearcher.

/**
     * Returns a new searcher instance. The consumer of this
     * API is responsible for releasing the returned searcher in a
     * safe manner, preferably in a try/finally block.
     *
     * @see Searcher#close()
     */
public final Searcher acquireSearcher(String source) throws EngineException {
    boolean success = false;
    /* Acquire order here is store -> manager since we need
          * to make sure that the store is not closed before
          * the searcher is acquired. */
    store.incRef();
    try {
        // can never be null
        final SearcherManager manager = getSearcherManager();
        /* This might throw NPE but that's fine we will run ensureOpen()
            *  in the catch block and throw the right exception */
        final IndexSearcher searcher = manager.acquire();
        try {
            final Searcher retVal = newSearcher(source, searcher, manager);
            success = true;
            return retVal;
        } finally {
            if (!success) {
                manager.release(searcher);
            }
        }
    } catch (AlreadyClosedException ex) {
        throw ex;
    } catch (Exception ex) {
        // throw EngineCloseException here if we are already closed
        ensureOpen();
        logger.error((Supplier<?>) () -> new ParameterizedMessage("failed to acquire searcher, source {}", source), ex);
        throw new EngineException(shardId, "failed to acquire searcher, source " + source, ex);
    } finally {
        if (!success) {
            // release the ref in the case of an error...
            store.decRef();
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) SearcherManager(org.apache.lucene.search.SearcherManager) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) NoSuchFileException(java.nio.file.NoSuchFileException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 2 with SearcherManager

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

the class InternalEngine method createSearcherManager.

private SearcherManager createSearcherManager() throws EngineException {
    boolean success = false;
    SearcherManager searcherManager = null;
    try {
        try {
            final DirectoryReader directoryReader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(indexWriter), shardId);
            searcherManager = new SearcherManager(directoryReader, searcherFactory);
            lastCommittedSegmentInfos = readLastCommittedSegmentInfos(searcherManager, store);
            success = true;
            return searcherManager;
        } catch (IOException e) {
            maybeFailEngine("start", e);
            try {
                indexWriter.rollback();
            } catch (IOException inner) {
                // iw is closed below
                e.addSuppressed(inner);
            }
            throw new EngineCreationFailureException(shardId, "failed to open reader on writer", e);
        }
    } finally {
        if (success == false) {
            // release everything we created on a failure
            IOUtils.closeWhileHandlingException(searcherManager, indexWriter);
        }
    }
}
Also used : ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) SearcherManager(org.apache.lucene.search.SearcherManager) IOException(java.io.IOException)

Example 3 with SearcherManager

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

the class AnalyzingInfixSuggester method lookup.

/**
   * This is an advanced method providing the capability to send down to the suggester any 
   * arbitrary lucene query to be used to filter the result of the suggester
   * 
   * @param key the keyword being looked for
   * @param contextQuery an arbitrary Lucene query to be used to filter the result of the suggester. {@link #addContextToQuery} could be used to build this contextQuery.
   * @param num number of items to return
   * @param allTermsRequired all searched terms must match or not
   * @param doHighlight if true, the matching term will be highlighted in the search result
   * @return the result of the suggester
   * @throws IOException f the is IO exception while reading data from the index
   */
public List<LookupResult> lookup(CharSequence key, BooleanQuery contextQuery, int num, boolean allTermsRequired, boolean doHighlight) throws IOException {
    if (searcherMgr == null) {
        throw new IllegalStateException("suggester was not built");
    }
    final BooleanClause.Occur occur;
    if (allTermsRequired) {
        occur = BooleanClause.Occur.MUST;
    } else {
        occur = BooleanClause.Occur.SHOULD;
    }
    BooleanQuery.Builder query;
    Set<String> matchedTokens;
    String prefixToken = null;
    try (TokenStream ts = queryAnalyzer.tokenStream("", new StringReader(key.toString()))) {
        //long t0 = System.currentTimeMillis();
        ts.reset();
        final CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
        final OffsetAttribute offsetAtt = ts.addAttribute(OffsetAttribute.class);
        String lastToken = null;
        query = new BooleanQuery.Builder();
        int maxEndOffset = -1;
        matchedTokens = new HashSet<>();
        while (ts.incrementToken()) {
            if (lastToken != null) {
                matchedTokens.add(lastToken);
                query.add(new TermQuery(new Term(TEXT_FIELD_NAME, lastToken)), occur);
            }
            lastToken = termAtt.toString();
            if (lastToken != null) {
                maxEndOffset = Math.max(maxEndOffset, offsetAtt.endOffset());
            }
        }
        ts.end();
        if (lastToken != null) {
            Query lastQuery;
            if (maxEndOffset == offsetAtt.endOffset()) {
                // Use PrefixQuery (or the ngram equivalent) when
                // there was no trailing discarded chars in the
                // string (e.g. whitespace), so that if query does
                // not end with a space we show prefix matches for
                // that token:
                lastQuery = getLastTokenQuery(lastToken);
                prefixToken = lastToken;
            } else {
                // Use TermQuery for an exact match if there were
                // trailing discarded chars (e.g. whitespace), so
                // that if query ends with a space we only show
                // exact matches for that term:
                matchedTokens.add(lastToken);
                lastQuery = new TermQuery(new Term(TEXT_FIELD_NAME, lastToken));
            }
            if (lastQuery != null) {
                query.add(lastQuery, occur);
            }
        }
        if (contextQuery != null) {
            boolean allMustNot = true;
            for (BooleanClause clause : contextQuery.clauses()) {
                if (clause.getOccur() != BooleanClause.Occur.MUST_NOT) {
                    allMustNot = false;
                    break;
                }
            }
            if (allMustNot) {
                // All are MUST_NOT: add the contextQuery to the main query instead (not as sub-query)
                for (BooleanClause clause : contextQuery.clauses()) {
                    query.add(clause);
                }
            } else if (allTermsRequired == false) {
                // We must carefully upgrade the query clauses to MUST:
                BooleanQuery.Builder newQuery = new BooleanQuery.Builder();
                newQuery.add(query.build(), BooleanClause.Occur.MUST);
                newQuery.add(contextQuery, BooleanClause.Occur.MUST);
                query = newQuery;
            } else {
                // Add contextQuery as sub-query
                query.add(contextQuery, BooleanClause.Occur.MUST);
            }
        }
    }
    // TODO: we could allow blended sort here, combining
    // weight w/ score.  Now we ignore score and sort only
    // by weight:
    Query finalQuery = finishQuery(query, allTermsRequired);
    //System.out.println("finalQuery=" + finalQuery);
    // Sort by weight, descending:
    TopFieldCollector c = TopFieldCollector.create(SORT, num, true, false, false);
    // We sorted postings by weight during indexing, so we
    // only retrieve the first num hits now:
    Collector c2 = new EarlyTerminatingSortingCollector(c, SORT, num);
    List<LookupResult> results = null;
    SearcherManager mgr;
    IndexSearcher searcher;
    synchronized (searcherMgrLock) {
        // acquire & release on same SearcherManager, via local reference
        mgr = searcherMgr;
        searcher = mgr.acquire();
    }
    try {
        //System.out.println("got searcher=" + searcher);
        searcher.search(finalQuery, c2);
        TopFieldDocs hits = c.topDocs();
        // Slower way if postings are not pre-sorted by weight:
        // hits = searcher.search(query, null, num, SORT);
        results = createResults(searcher, hits, num, key, doHighlight, matchedTokens, prefixToken);
    } finally {
        mgr.release(searcher);
    }
    return results;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) TokenStream(org.apache.lucene.analysis.TokenStream) Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SearcherManager(org.apache.lucene.search.SearcherManager) StringReader(java.io.StringReader) EarlyTerminatingSortingCollector(org.apache.lucene.search.EarlyTerminatingSortingCollector) TopFieldCollector(org.apache.lucene.search.TopFieldCollector) Collector(org.apache.lucene.search.Collector) TopFieldCollector(org.apache.lucene.search.TopFieldCollector) TermQuery(org.apache.lucene.search.TermQuery) Occur(org.apache.lucene.search.BooleanClause.Occur) Term(org.apache.lucene.index.Term) BooleanClause(org.apache.lucene.search.BooleanClause) EarlyTerminatingSortingCollector(org.apache.lucene.search.EarlyTerminatingSortingCollector) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) OffsetAttribute(org.apache.lucene.analysis.tokenattributes.OffsetAttribute)

Example 4 with SearcherManager

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

the class AnalyzingInfixSuggester method ramBytesUsed.

@Override
public long ramBytesUsed() {
    long mem = RamUsageEstimator.shallowSizeOf(this);
    try {
        if (searcherMgr != null) {
            SearcherManager mgr;
            IndexSearcher searcher;
            synchronized (searcherMgrLock) {
                // acquire & release on same SearcherManager, via local reference
                mgr = searcherMgr;
                searcher = mgr.acquire();
            }
            try {
                for (LeafReaderContext context : searcher.getIndexReader().leaves()) {
                    LeafReader reader = FilterLeafReader.unwrap(context.reader());
                    if (reader instanceof SegmentReader) {
                        mem += ((SegmentReader) context.reader()).ramBytesUsed();
                    }
                }
            } finally {
                mgr.release(searcher);
            }
        }
        return mem;
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) LeafReader(org.apache.lucene.index.LeafReader) FilterLeafReader(org.apache.lucene.index.FilterLeafReader) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) SearcherManager(org.apache.lucene.search.SearcherManager) IOException(java.io.IOException) SegmentReader(org.apache.lucene.index.SegmentReader)

Example 5 with SearcherManager

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

the class AnalyzingInfixSuggester method getChildResources.

@Override
public Collection<Accountable> getChildResources() {
    List<Accountable> resources = new ArrayList<>();
    try {
        if (searcherMgr != null) {
            SearcherManager mgr;
            IndexSearcher searcher;
            synchronized (searcherMgrLock) {
                // acquire & release on same SearcherManager, via local reference
                mgr = searcherMgr;
                searcher = mgr.acquire();
            }
            try {
                for (LeafReaderContext context : searcher.getIndexReader().leaves()) {
                    LeafReader reader = FilterLeafReader.unwrap(context.reader());
                    if (reader instanceof SegmentReader) {
                        resources.add(Accountables.namedAccountable("segment", (SegmentReader) reader));
                    }
                }
            } finally {
                mgr.release(searcher);
            }
        }
        return Collections.unmodifiableList(resources);
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) LeafReader(org.apache.lucene.index.LeafReader) FilterLeafReader(org.apache.lucene.index.FilterLeafReader) ArrayList(java.util.ArrayList) Accountable(org.apache.lucene.util.Accountable) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) SearcherManager(org.apache.lucene.search.SearcherManager) IOException(java.io.IOException) SegmentReader(org.apache.lucene.index.SegmentReader)

Aggregations

SearcherManager (org.apache.lucene.search.SearcherManager)21 IndexSearcher (org.apache.lucene.search.IndexSearcher)11 IOException (java.io.IOException)8 Directory (org.apache.lucene.store.Directory)8 SearcherFactory (org.apache.lucene.search.SearcherFactory)7 IndexWriter (org.apache.lucene.index.IndexWriter)6 TermQuery (org.apache.lucene.search.TermQuery)4 RAMDirectory (org.apache.lucene.store.RAMDirectory)4 File (java.io.File)3 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)3 Query (org.apache.lucene.search.Query)3 TopDocs (org.apache.lucene.search.TopDocs)3 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 FilterLeafReader (org.apache.lucene.index.FilterLeafReader)2 LeafReader (org.apache.lucene.index.LeafReader)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)2 SegmentReader (org.apache.lucene.index.SegmentReader)2 Term (org.apache.lucene.index.Term)2 FSDirectory (org.apache.lucene.store.FSDirectory)2