List of usage examples for org.apache.lucene.search IndexSearcher getIndexReader
public IndexReader getIndexReader()
From source file:org.apache.zeppelin.search.LuceneSearch.java
License:Apache License
private List<Map<String, String>> doSearch(IndexSearcher searcher, Query query, Analyzer analyzer, Highlighter highlighter) { List<Map<String, String>> matchingParagraphs = Lists.newArrayList(); ScoreDoc[] hits;// w ww . j ava 2s.c o m try { hits = searcher.search(query, 20).scoreDocs; for (int i = 0; i < hits.length; i++) { logger.debug("doc={} score={}", hits[i].doc, hits[i].score); int id = hits[i].doc; Document doc = searcher.doc(id); String path = doc.get(ID_FIELD); if (path != null) { logger.debug((i + 1) + ". " + path); String title = doc.get("title"); if (title != null) { logger.debug(" Title: {}", doc.get("title")); } String text = doc.get(SEARCH_FIELD_TEXT); String header = doc.get(SEARCH_FIELD_TITLE); String fragment = ""; if (text != null) { TokenStream tokenStream = TokenSources.getTokenStream(searcher.getIndexReader(), id, SEARCH_FIELD_TEXT, analyzer); TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, text, true, 3); logger.debug(" {} fragments found for query '{}'", frag.length, query); for (int j = 0; j < frag.length; j++) { if ((frag[j] != null) && (frag[j].getScore() > 0)) { logger.debug(" Fragment: {}", frag[j].toString()); } } fragment = (frag != null && frag.length > 0) ? frag[0].toString() : ""; } if (header != null) { TokenStream tokenTitle = TokenSources.getTokenStream(searcher.getIndexReader(), id, SEARCH_FIELD_TITLE, analyzer); TextFragment[] frgTitle = highlighter.getBestTextFragments(tokenTitle, header, true, 3); header = (frgTitle != null && frgTitle.length > 0) ? frgTitle[0].toString() : ""; } else { header = ""; } matchingParagraphs.add(ImmutableMap.of("id", path, // <noteId>/paragraph/<paragraphId> "name", title, "snippet", fragment, "text", text, "header", header)); } else { logger.info("{}. No {} for this document", i + 1, ID_FIELD); } } } catch (IOException | InvalidTokenOffsetsException e) { logger.error("Exception on searching for {}", query, e); } return matchingParagraphs; }
From source file:org.arastreju.sge.index.ArastrejuIndex.java
License:Apache License
public void dump() { ContextIndex index = provider.forContext(conversationContext.getPrimaryContext()); org.apache.lucene.search.IndexSearcher searcher = index.getSearcher(); IndexReader reader = searcher.getIndexReader(); try {//from ww w. j ava2 s. co m TopDocs top = searcher.search(new MatchAllDocsQuery(), 100); for (int i = 0; i < top.totalHits; i++) { Document doc = reader.document(top.scoreDocs[i].doc); LOGGER.info("---Document--- id: " + top.scoreDocs[i].doc); List<Fieldable> fields = doc.getFields(); for (Fieldable f : fields) { LOGGER.info("\tField: name='" + f.name() + "', val='" + f.stringValue() + "'"); } } } catch (IOException e) { String msg = "caught IOException while dumping index"; LOGGER.error(msg, e); throw new RuntimeException(msg, e); } }
From source file:org.carrot2.source.lucene.LuceneDocumentSource.java
License:Open Source License
/** * Close all opened indexes in the shared context. *//* w w w. j ava 2 s. c o m*/ private void closeAllIndexes() { synchronized (context) { for (IndexSearcher searcher : openIndexes.values()) { try { searcher.getIndexReader().close(); } catch (IOException e) { logger.warn("Could not close search index: " + searcher, e); } } } }
From source file:org.cee.store.lucene.LuceneArticleStore.java
License:Apache License
private Query createRelatedArticlesQuery(List<EntityKey> sites, ArticleKey reference, IndexSearcher searcher, String language) throws IOException { Query articleQuery = createArticleQuery(reference); TopDocs topDocs = searcher.search(articleQuery, 1); if (topDocs.totalHits == 0) { return new BooleanQuery(true); }//from ww w . j av a 2s . c o m MoreLikeThis mlt = new MoreLikeThis(searcher.getIndexReader()); mlt.setFieldNames(LuceneConstants.ARTICLE_RELATED_SEARCH_FIELDS); mlt.setMaxQueryTerms(20); mlt.setBoost(true); mlt.setMinTermFreq(0); mlt.setMinDocFreq(0); Query relatedQuery = boostRelatedQuery(mlt.like(topDocs.scoreDocs[0].doc)); BooleanQuery query = new BooleanQuery(); query.add(new BooleanClause(relatedQuery, Occur.MUST)); query.add(new BooleanClause(createQueryArticlesOfSites(sites), Occur.MUST)); return query; }
From source file:org.codelibs.elasticsearch.common.lucene.Lucene.java
License:Apache License
/** * Check whether there is one or more documents matching the provided query. *///from www .j a va2 s . co m public static boolean exists(IndexSearcher searcher, Query query) throws IOException { final Weight weight = searcher.createNormalizedWeight(query, false); // the scorer API should be more efficient at stopping after the first // match than the bulk scorer API for (LeafReaderContext context : searcher.getIndexReader().leaves()) { final Scorer scorer = weight.scorer(context); if (scorer == null) { continue; } final Bits liveDocs = context.reader().getLiveDocs(); final DocIdSetIterator iterator = scorer.iterator(); for (int doc = iterator.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = iterator.nextDoc()) { if (liveDocs == null || liveDocs.get(doc)) { return true; } } } return false; }
From source file:org.codelibs.elasticsearch.search.suggest.Suggester.java
License:Apache License
public Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> execute( String name, T suggestion, IndexSearcher searcher, CharsRefBuilder spare) throws IOException { // #3469 We want to ignore empty shards if (searcher.getIndexReader().numDocs() == 0) { return null; }//from ww w .j a v a2s . c o m return innerExecute(name, suggestion, searcher, spare); }
From source file:org.codelibs.elasticsearch.search.suggest.term.TermSuggester.java
License:Apache License
@Override public TermSuggestion innerExecute(String name, TermSuggestionContext suggestion, IndexSearcher searcher, CharsRefBuilder spare) throws IOException { DirectSpellChecker directSpellChecker = suggestion.getDirectSpellCheckerSettings() .createDirectSpellChecker(); final IndexReader indexReader = searcher.getIndexReader(); TermSuggestion response = new TermSuggestion(name, suggestion.getSize(), suggestion.getDirectSpellCheckerSettings().sort()); List<Token> tokens = queryTerms(suggestion, spare); for (Token token : tokens) { // TODO: Extend DirectSpellChecker in 4.1, to get the raw suggested words as BytesRef SuggestWord[] suggestedWords = directSpellChecker.suggestSimilar(token.term, suggestion.getShardSize(), indexReader, suggestion.getDirectSpellCheckerSettings().suggestMode()); Text key = new Text(new BytesArray(token.term.bytes())); TermSuggestion.Entry resultEntry = new TermSuggestion.Entry(key, token.startOffset, token.endOffset - token.startOffset); for (SuggestWord suggestWord : suggestedWords) { Text word = new Text(suggestWord.string); resultEntry.addOption(new TermSuggestion.Entry.Option(word, suggestWord.freq, suggestWord.score)); }// w w w . j a va 2 s . c om response.addTerm(resultEntry); } return response; }
From source file:org.codesearch.indexer.server.tasks.ClearTask.java
License:Open Source License
/** * executes the task deletes all index files and resets the * lastIndexingRevision of all repositories * * @throws TaskExecutionException//from ww w. j av a2s .c o m */ @Override public void execute() throws TaskExecutionException { if (repositories == null || repositories.isEmpty()) { // Clear the whole index LOG.info("Clearing the whole index"); boolean deleteSuccess = true; if (indexLocation.listFiles() != null) { for (File f : indexLocation.listFiles()) { if (!f.delete()) { LOG.error("Could not delete file: " + f.getName()); deleteSuccess = false; } } } if (deleteSuccess) { LOG.debug("Successfully cleared index"); } else { LOG.error("Could not delete all index files"); } try { dba.purgeDatabaseEntries(); LOG.debug("Cleared code analysis index"); } catch (DatabaseAccessException ex) { LOG.warn("Could not clear code analysis index: \n" + ex); } } else { // Clear specific repository from the index StringBuilder repos = new StringBuilder(); for (RepositoryDto repositoryDto : repositories) { repos.append(repositoryDto.getName()).append(" "); } LOG.info("Clearing index for repositories: " + repos.toString().trim()); if (job != null) { job.getJobDataMap().put(IndexingJob.FIELD_CURRENT_STEPS, repositories.size()); } IndexSearcher searcher = null; try { FSDirectory fsd = FSDirectory.open(indexLocation); searcher = new IndexSearcher(fsd, false); int index = 0; for (RepositoryDto repositoryDto : repositories) { if (job != null) { // set the status job.setCurrentRepository(index); job.getJobDataMap().put(IndexingJob.FIELD_FINISHED_STEPS, index); } Term term = new Term(IndexConstants.INDEX_FIELD_REPOSITORY, repositoryDto.getName().toLowerCase()); LOG.debug("Deleting documents where field '" + term.field() + "' is '" + term.text() + "'"); int deleteCount = 0; try { deleteCount = searcher.getIndexReader().deleteDocuments(term); } catch (IOException ex) { LOG.error("Error encountered while clearing index: " + ex); } indexStatusManager.setStatus(repositoryDto.getName(), VersionControlPlugin.UNDEFINED_VERSION); LOG.debug("Cleared " + deleteCount + " documents for repository " + repositoryDto.getName()); index++; } } catch (CorruptIndexException ex) { LOG.error("Could not clear index because it is corrupt: \n" + ex); } catch (LockObtainFailedException ex) { LOG.error("Could not clear index because it is locked."); } catch (StaleReaderException ex) { LOG.error("The index was modified by another program/instance while clearing."); } catch (IOException ex) { // if there is no index nothing has to be cleared } finally { if (searcher != null) { try { searcher.close(); } catch (IOException ex) { } } } for (RepositoryDto repositoryDto : repositories) { try { dba.deleteRepository(repositoryDto.getName()); LOG.debug("Cleared code analysis index for repository " + repositoryDto.getName()); } catch (DatabaseAccessException ex) { LOG.warn("Could not clear code analysis index for repository: " + repositoryDto.getName() + ", ignore if code analysis is not enabled \n" + ex); } } } LOG.info("Finished clearing index"); }
From source file:org.compass.core.lucene.engine.manager.LuceneIndexHolder.java
License:Apache License
public LuceneIndexHolder(IndexHoldersCache indexHoldersCache, String subIndex, IndexSearcher indexSearcher) { this.indexHoldersCache = indexHoldersCache; this.subIndex = subIndex; this.indexSearcher = indexSearcher; this.indexReader = indexSearcher.getIndexReader(); if (indexHoldersCache.isDebug()) { AtomicInteger count = indexHoldersCache.getDebugHoldersCount().get(subIndex); if (count == null) { AtomicInteger newCount = new AtomicInteger(); count = indexHoldersCache.getDebugHoldersCount().putIfAbsent(subIndex, newCount); if (count == null) { count = newCount;/*from ww w .j ava2 s . com*/ } } count.incrementAndGet(); } }
From source file:org.compass.core.lucene.engine.spellcheck.DefaultLuceneSpellCheckManager.java
License:Apache License
public CompassSpellChecker createSpellChecker(final String[] subIndexes, final String[] aliases) { String[] calcSubIndexes = indexStore.calcSubIndexes(subIndexes, aliases); ArrayList<Searchable> searchers = new ArrayList<Searchable>(calcSubIndexes.length); ArrayList<IndexReader> readers = new ArrayList<IndexReader>(calcSubIndexes.length); for (String subIndex : calcSubIndexes) { synchronized (indexLocks.get(subIndex)) { IndexSearcher searcher = searcherMap.get(subIndex); if (searcher != null) { searchers.add(searcher); readers.add(searcher.getIndexReader()); }/*from w w w . ja v a 2 s . c om*/ } } if (searchers.isEmpty()) { return null; } MultiSearcher searcher; try { searcher = searchEngineFactory.getLuceneIndexManager() .openMultiSearcher(searchers.toArray(new Searchable[searchers.size()])); } catch (IOException e) { throw new SearchEngineException("Failed to open searcher for spell check", e); } MultiReader reader = new CacheableMultiReader(readers.toArray(new IndexReader[readers.size()]), false); return new CompassSpellChecker(searcher, reader); }