Search in sources :

Example 61 with IndexReader

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

the class SpellCheckComponent method process.

@Override
@SuppressWarnings("unchecked")
public void process(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    if (!params.getBool(COMPONENT_NAME, false) || spellCheckers.isEmpty()) {
        return;
    }
    boolean shardRequest = "true".equals(params.get(ShardParams.IS_SHARD));
    String q = params.get(SPELLCHECK_Q);
    SolrSpellChecker spellChecker = getSpellChecker(params);
    Collection<Token> tokens = null;
    if (q != null) {
        //we have a spell check param, tokenize it with the query analyzer applicable for this spellchecker
        tokens = getTokens(q, spellChecker.getQueryAnalyzer());
    } else {
        q = rb.getQueryString();
        if (q == null) {
            q = params.get(CommonParams.Q);
        }
        tokens = queryConverter.convert(q);
    }
    if (tokens != null && tokens.isEmpty() == false) {
        if (spellChecker != null) {
            int count = params.getInt(SPELLCHECK_COUNT, 1);
            boolean onlyMorePopular = params.getBool(SPELLCHECK_ONLY_MORE_POPULAR, DEFAULT_ONLY_MORE_POPULAR);
            boolean extendedResults = params.getBool(SPELLCHECK_EXTENDED_RESULTS, false);
            boolean collate = params.getBool(SPELLCHECK_COLLATE, false);
            float accuracy = params.getFloat(SPELLCHECK_ACCURACY, Float.MIN_VALUE);
            int alternativeTermCount = params.getInt(SpellingParams.SPELLCHECK_ALTERNATIVE_TERM_COUNT, 0);
            //If specified, this can be a discrete # of results, or a percentage of fq results.
            Integer maxResultsForSuggest = maxResultsForSuggest(rb);
            ModifiableSolrParams customParams = new ModifiableSolrParams();
            for (String checkerName : getDictionaryNames(params)) {
                customParams.add(getCustomParams(checkerName, params));
            }
            Integer hitsInteger = (Integer) rb.rsp.getToLog().get("hits");
            long hits = 0;
            if (hitsInteger == null) {
                hits = rb.getNumberDocumentsFound();
            } else {
                hits = hitsInteger.longValue();
            }
            SpellingResult spellingResult = null;
            if (maxResultsForSuggest == null || hits <= maxResultsForSuggest) {
                SuggestMode suggestMode = SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX;
                if (onlyMorePopular) {
                    suggestMode = SuggestMode.SUGGEST_MORE_POPULAR;
                } else if (alternativeTermCount > 0) {
                    suggestMode = SuggestMode.SUGGEST_ALWAYS;
                }
                IndexReader reader = rb.req.getSearcher().getIndexReader();
                SpellingOptions options = new SpellingOptions(tokens, reader, count, alternativeTermCount, suggestMode, extendedResults, accuracy, customParams);
                spellingResult = spellChecker.getSuggestions(options);
            } else {
                spellingResult = new SpellingResult();
            }
            boolean isCorrectlySpelled = hits > (maxResultsForSuggest == null ? 0 : maxResultsForSuggest);
            NamedList response = new SimpleOrderedMap();
            NamedList suggestions = toNamedList(shardRequest, spellingResult, q, extendedResults);
            response.add("suggestions", suggestions);
            if (extendedResults) {
                response.add("correctlySpelled", isCorrectlySpelled);
            }
            if (collate) {
                addCollationsToResponse(params, spellingResult, rb, q, response, spellChecker.isSuggestionsMayOverlap());
            }
            if (shardRequest) {
                addOriginalTermsToResponse(response, tokens);
            }
            rb.rsp.add("spellcheck", response);
        } else {
            throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Specified dictionaries do not exist: " + getDictionaryNameAsSingleString(getDictionaryNames(params)));
        }
    }
}
Also used : ConjunctionSolrSpellChecker(org.apache.solr.spelling.ConjunctionSolrSpellChecker) SolrSpellChecker(org.apache.solr.spelling.SolrSpellChecker) NamedList(org.apache.solr.common.util.NamedList) Token(org.apache.lucene.analysis.Token) SpellingOptions(org.apache.solr.spelling.SpellingOptions) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SuggestMode(org.apache.lucene.search.spell.SuggestMode) SpellingResult(org.apache.solr.spelling.SpellingResult) IndexReader(org.apache.lucene.index.IndexReader) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrException(org.apache.solr.common.SolrException)

Example 62 with IndexReader

use of org.apache.lucene.index.IndexReader in project zm-mailbox by Zimbra.

the class RawIndexEditor method dumpAll.

void dumpAll() throws IOException {
    IndexReader reader = IndexReader.open(luceneDirectory);
    try {
        int maxDoc = reader.maxDoc();
        System.out.println("There are " + maxDoc + " documents in this index.");
        for (int i = 0; i < maxDoc; i++) {
            dumpDocument(reader.document(i), reader.isDeleted(i));
        }
    } finally {
        reader.close();
    }
}
Also used : IndexReader(org.apache.lucene.index.IndexReader)

Example 63 with IndexReader

use of org.apache.lucene.index.IndexReader in project zm-mailbox by Zimbra.

the class RemoteMailQueue method action.

public void action(Server server, QueueAction action, String[] ids) throws ServiceException {
    if (ZimbraLog.rmgmt.isDebugEnabled())
        ZimbraLog.rmgmt.debug("action=" + action + " ids=" + Arrays.deepToString(ids) + " " + this);
    RemoteManager rm = RemoteManager.getRemoteManager(server);
    IndexReader indexReader = null;
    try {
        boolean all = false;
        if (ids.length == 1 && ids[0].equals("ALL")) {
            // Special case ALL that postsuper supports
            clearIndex();
            all = true;
        } else {
            indexReader = IndexReader.open(LuceneDirectory.open(mIndexPath), false);
        }
        int done = 0;
        int total = ids.length;
        while (done < total) {
            int last = Math.min(total, done + MAX_REMOTE_EXECUTION_QUEUEIDS);
            StringBuilder sb = new StringBuilder(128 + (last * MAX_LENGTH_OF_QUEUEIDS));
            sb.append("zmqaction " + action.toString() + " " + mQueueName + " ");
            int i;
            boolean first = true;
            for (i = done; i < last; i++) {
                if (first) {
                    first = false;
                } else {
                    sb.append(",");
                }
                if (!all) {
                    Term toDelete = new Term(QueueAttr.id.toString(), ids[i].toLowerCase());
                    int numDeleted = indexReader.deleteDocuments(toDelete);
                    mNumMessages.getAndAdd(-numDeleted);
                    if (ZimbraLog.rmgmt.isDebugEnabled())
                        ZimbraLog.rmgmt.debug("deleting term:" + toDelete + ", docs deleted=" + numDeleted);
                }
                sb.append(ids[i].toUpperCase());
            }
            done = last;
            rm.execute(sb.toString());
        }
    } catch (IOException ioe) {
        throw ServiceException.FAILURE("exception occurred performing queue action", ioe);
    } finally {
        if (indexReader != null) {
            try {
                indexReader.close();
            } catch (IOException ioe) {
                ZimbraLog.rmgmt.warn("exception occured closing index reader during action", ioe);
            }
        }
    }
}
Also used : IndexReader(org.apache.lucene.index.IndexReader) Term(org.apache.lucene.index.Term) IOException(java.io.IOException)

Example 64 with IndexReader

use of org.apache.lucene.index.IndexReader in project zm-mailbox by Zimbra.

the class RemoteMailQueue method search.

public SearchResult search(Query query, int offset, int limit) throws ServiceException {
    SearchResult result = new SearchResult();
    IndexReader indexReader = null;
    try {
        if (!mIndexPath.exists()) {
            return result;
        }
        indexReader = IndexReader.open(LuceneDirectory.open(mIndexPath));
        summarize(result, indexReader);
        if (query == null) {
            list0(result, indexReader, offset, limit);
        } else {
            search0(result, indexReader, query, offset, limit);
        }
    } catch (Exception e) {
        throw ServiceException.FAILURE("exception occurred searching mail queue", e);
    } finally {
        if (indexReader != null) {
            try {
                indexReader.close();
            } catch (IOException ioe) {
                ZimbraLog.rmgmt.warn("exception occured closing index reader from search", ioe);
            }
        }
    }
    return result;
}
Also used : IndexReader(org.apache.lucene.index.IndexReader) IOException(java.io.IOException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException)

Example 65 with IndexReader

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

the class TestBufferedIndexInput method testSetBufferSize.

public void testSetBufferSize() throws IOException {
    Path indexDir = createTempDir("testSetBufferSize");
    MockFSDirectory dir = new MockFSDirectory(indexDir, random());
    IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE).setMergePolicy(newLogMergePolicy(false)));
    for (int i = 0; i < 37; i++) {
        Document doc = new Document();
        doc.add(newTextField("content", "aaa bbb ccc ddd" + i, Field.Store.YES));
        doc.add(newTextField("id", "" + i, Field.Store.YES));
        writer.addDocument(doc);
    }
    dir.allIndexInputs.clear();
    IndexReader reader = DirectoryReader.open(writer);
    Term aaa = new Term("content", "aaa");
    Term bbb = new Term("content", "bbb");
    reader.close();
    dir.tweakBufferSizes();
    writer.deleteDocuments(new Term("id", "0"));
    reader = DirectoryReader.open(writer);
    IndexSearcher searcher = newSearcher(reader);
    ScoreDoc[] hits = searcher.search(new TermQuery(bbb), 1000).scoreDocs;
    dir.tweakBufferSizes();
    assertEquals(36, hits.length);
    reader.close();
    dir.tweakBufferSizes();
    writer.deleteDocuments(new Term("id", "4"));
    reader = DirectoryReader.open(writer);
    searcher = newSearcher(reader);
    hits = searcher.search(new TermQuery(bbb), 1000).scoreDocs;
    dir.tweakBufferSizes();
    assertEquals(35, hits.length);
    dir.tweakBufferSizes();
    hits = searcher.search(new TermQuery(new Term("id", "33")), 1000).scoreDocs;
    dir.tweakBufferSizes();
    assertEquals(1, hits.length);
    hits = searcher.search(new TermQuery(aaa), 1000).scoreDocs;
    dir.tweakBufferSizes();
    assertEquals(35, hits.length);
    writer.close();
    reader.close();
}
Also used : Path(java.nio.file.Path) IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

IndexReader (org.apache.lucene.index.IndexReader)962 Document (org.apache.lucene.document.Document)610 Directory (org.apache.lucene.store.Directory)603 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)549 IndexSearcher (org.apache.lucene.search.IndexSearcher)410 Term (org.apache.lucene.index.Term)332 TopDocs (org.apache.lucene.search.TopDocs)204 TermQuery (org.apache.lucene.search.TermQuery)160 Query (org.apache.lucene.search.Query)158 IndexWriter (org.apache.lucene.index.IndexWriter)150 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)144 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)143 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)142 Field (org.apache.lucene.document.Field)135 BytesRef (org.apache.lucene.util.BytesRef)134 IOException (java.io.IOException)133 BooleanQuery (org.apache.lucene.search.BooleanQuery)122 ArrayList (java.util.ArrayList)108 TextField (org.apache.lucene.document.TextField)81 Test (org.junit.Test)81