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