congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
IndexReader
Code IndexAdd Tabnine to your IDE (free)

How to use
IndexReader
in
org.apache.lucene.index

Best Java code snippets using org.apache.lucene.index.IndexReader (Showing top 20 results out of 2,376)

Refine searchRefine arrow

  • IndexSearcher
  • Term
  • Document
  • LeafReaderContext
  • LeafReader
  • TermEnum
  • Directory
  • DirectoryReader
origin: oracle/opengrok

/**
 * Get number of documents in this index database.
 * @return number of documents
 * @throws IOException if I/O exception occurred
 */
public int getNumFiles() throws IOException {
  IndexReader ireader = null;
  int numDocs = 0;
  try {
    ireader = DirectoryReader.open(indexDirectory); // open existing index
    numDocs = ireader.numDocs();
  } finally {
    if (ireader != null) {
      try {
        ireader.close();
      } catch (IOException e) {
        LOGGER.log(Level.WARNING, "An error occurred while closing index reader", e);
      }
    }
  }
  return numDocs;
}
origin: tjake/Solandra

 @Override
 protected Object createValue(IndexReader reader, Entry entryKey)
 throws IOException {
  Entry entry = entryKey;
  String field = entry.field;
    if (reader.maxDoc() == reader.docFreq(new Term(field))) {
   return DocIdSet.EMPTY_DOCIDSET;
  }
    OpenBitSet res = new OpenBitSet(reader.maxDoc());
  TermDocs termDocs = reader.termDocs();
  TermEnum termEnum = reader.terms (new Term (field));
  try {
   do {
    Term term = termEnum.term();
    if (term==null || term.field() != field) break;
    termDocs.seek (termEnum);
    while (termDocs.next()) {
     res.fastSet(termDocs.doc());
    }
   } while (termEnum.next());
  } finally {
   termDocs.close();
   termEnum.close();
  }
  res.flip(0, reader.maxDoc());
  return res;
 }
}
origin: languagetool-org/languagetool

@Override
public long getTotalTokenCount() {
 LuceneSearcher luceneSearcher = getLuceneSearcher(1);
 try {
  RegexpQuery query = new RegexpQuery(new Term("totalTokenCount", ".*"));
  TopDocs docs = luceneSearcher.searcher.search(query, 1000);  // Integer.MAX_VALUE might cause OOE on wrong index
  if (docs.totalHits == 0) {
   throw new RuntimeException("Expected 'totalTokenCount' meta documents not found in 1grams index: " + luceneSearcher.directory);
  } else if (docs.totalHits > 1000) {
   throw new RuntimeException("Did not expect more than 1000 'totalTokenCount' meta documents: " + docs.totalHits + " in " + luceneSearcher.directory);
  } else {
   long result = 0;
   for (ScoreDoc scoreDoc : docs.scoreDocs) {
    long tmp = Long.parseLong(luceneSearcher.reader.document(scoreDoc.doc).get("totalTokenCount"));
    if (tmp > result) {
     // due to the way FrequencyIndexCreator adds these totalTokenCount fields, we must not sum them,
     // but take the largest one:
     result = tmp;
    }
   }
   return result;
  }
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}
origin: oracle/opengrok

private static double computeNormalizedDocumentFrequency(final IndexReader indexReader, final Term term)
    throws IOException {
  int documentFrequency = indexReader.docFreq(term);
  return ((double) documentFrequency) / indexReader.numDocs();
}
origin: org.apache.lucene/lucene-core

 @Override
 protected synchronized void doClose() throws IOException {
  IOException ioe = null;
  for (final IndexReader reader : completeReaderSet) {
   try {
    if (closeSubReaders) {
     reader.close();
    } else {
     reader.decRef();
    }
   } catch (IOException e) {
    if (ioe == null) ioe = e;
   }
  }
  // throw the first exception
  if (ioe != null) throw ioe;
 }
}
origin: magese/ik-analyzer-solr7

iwriter = new IndexWriter(directory, iwConfig);
Document doc = new Document();
doc.add(new StringField("ID", "10000", Field.Store.YES));
doc.add(new TextField(fieldName, text, Field.Store.YES));
iwriter.addDocument(doc);
iwriter.close();
ireader = DirectoryReader.open(directory);
isearcher = new IndexSearcher(ireader);
TopDocs topDocs = isearcher.search(query, 5);
System.out.println("命中:" + topDocs.totalHits);
  Document targetDoc = isearcher.doc(scoreDocs[i].doc);
  System.out.println("内容:" + targetDoc.toString());
if (ireader != null) {
  try {
    ireader.close();
  } catch (IOException e) {
    e.printStackTrace();
    directory.close();
  } catch (IOException e) {
    e.printStackTrace();
origin: oracle/opengrok

IndexSearcher searcher = new IndexSearcher(ireader);
TopDocs top = searcher.search(q, 1);
if (top.totalHits == 0) {
Document doc = searcher.doc(top.scoreDocs[0].doc);
String foundPath = doc.get(QueryBuilder.PATH);
  IndexableField tags = doc.getField(QueryBuilder.TAGS);
  if (tags != null) {
    return Definitions.deserialize(tags.binaryValue().bytes);
ireader.close();
origin: dermotte/LIRE

private ImageSearchHits search(String[] hashes, GlobalFeature queryFeature, IndexReader reader) throws IOException {
  IndexSearcher searcher = new IndexSearcher(reader);
  searcher.setSimilarity(new ClassicSimilarity(){
    @Override
    public float tf(float freq) {
  for (int i = 0; i < hashes.length; i++) {
    queryBuilder.add(new BooleanClause(new TermQuery(new Term(hashesFieldName, hashes[i] + "")), BooleanClause.Occur.SHOULD));
  TopDocs docs = searcher.search(queryBuilder.build(), maxResultsHashBased);
  double tmpScore = 0d;
  for (int i = 0; i < docs.scoreDocs.length; i++) {
    feature.setByteArrayRepresentation(reader.document(docs.scoreDocs[i].doc).getBinaryValue(featureFieldName).bytes,
        reader.document(docs.scoreDocs[i].doc).getBinaryValue(featureFieldName).offset,
        reader.document(docs.scoreDocs[i].doc).getBinaryValue(featureFieldName).length);
    tmpScore = queryFeature.getDistance(feature);
    if (resultScoreDocs.size() < maximumHits) {
origin: tamingtext/book

public static void dumpTags(File file, String field, long maxDocs) throws IOException {
 Directory dir = FSDirectory.open(file);
 IndexReader reader = IndexReader.open(dir, true);
 TermEnum te = reader.terms(new Term(field, ""));
 do {
  Term term = te.term();
  if (term == null || term.field().equals(field) == false) {
   break;
  }
  System.err.printf("%s %d\n", term.text(), te.docFreq());
 } while (te.next());
 te.close();
}

origin: stackoverflow.com

Query query = new TermQuery(new Term(GRAMMED_WORDS_FIELD, term));
Sort sort = new Sort(COUNT_FIELD, true);
TopDocs docs = autoCompleteSearcher.search(query, null, 5, sort);
List<String> suggestions = new ArrayList<String>();
for (ScoreDoc doc : docs.scoreDocs) {
  suggestions.add(autoCompleteReader.document(doc.doc).get(
      SOURCE_WORD_FIELD));
    wordsMap.put(word, sourceReader.docFreq(new Term(
        fieldToAutocomplete, word)));
sourceReader.close();
  autoCompleteReader = IndexReader.open(autoCompleteDirectory);
} else {
  autoCompleteReader.reopen();
origin: rnewson/couchdb-lucene

if (getBooleanParameter(req, "rewrite")) {
  final Query rewritten_q = q.rewrite(searcher
      .getIndexReader());
  queryRow.put("rewritten_q", rewritten_q.toString());
    final int freq = searcher.getIndexReader().docFreq((Term) term);
    freqs.put(term.toString(), freq);
    td = searcher.search(q, skip + limit);
  } else {
    td = searcher.search(q, skip + limit, sort);
    for (final IndexableField f : doc.getFields()) {
      if (!f.fieldType().stored()) {
        continue;
      fetch_ids[i - skip] = doc.get("_id");
origin: dermotte/LIRE

private void testSearchSpeed(Class<? extends GlobalFeature> featureClass) throws IOException {
  ParallelIndexer parallelIndexer = new ParallelIndexer(DocumentBuilder.NUM_OF_THREADS, indexPath, testExtensive, true);
  parallelIndexer.addExtractor(featureClass);
  parallelIndexer.run();
  IndexReader reader = DirectoryReader.open(new RAMDirectory(FSDirectory.open(Paths.get(indexPath)), IOContext.READONCE));
  Bits liveDocs = MultiFields.getLiveDocs(reader);
  double queryCount = 0d;
  ImageSearcher searcher = new GenericFastImageSearcher(100, featureClass);
  long ms = System.currentTimeMillis();
  String fileName;
  Document queryDoc;
  ImageSearchHits hits;
  for (int i = 0; i < reader.maxDoc(); i++) {
    if (reader.hasDeletions() && !liveDocs.get(i)) continue; // if it is deleted, just ignore it.
    fileName = getIDfromFileName(reader.document(i).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
    if (queries.keySet().contains(fileName)) {
      queryCount += 1d;
      // ok, we've got a query here for a document ...
      queryDoc = reader.document(i);
      hits = searcher.search(queryDoc, reader);
    }
  }
  ms = System.currentTimeMillis() - ms;
  System.out.printf("%s \t %3.1f \n", featureClass.getName().substring(featureClass.getName().lastIndexOf('.') + 1), (double) ms / queryCount);
}
origin: org.apache.lucene/lucene-demos

private static void indexDocs(File file, File index, boolean create)
   throws Exception {
 if (!create) {				  // incrementally update
  reader = IndexReader.open(FSDirectory.open(index), false);		  // open existing index
  uidIter = reader.terms(new Term("uid", "")); // init uid iterator
  indexDocs(file);
  if (deleting) {				  // delete rest of stale docs
   while (uidIter.term() != null && uidIter.term().field() == "uid") {
    System.out.println("deleting " +
      HTMLDocument.uid2url(uidIter.term().text()));
    reader.deleteDocuments(uidIter.term());
    uidIter.next();
   }
   deleting = false;
  }
  uidIter.close();				  // close uid iterator
  reader.close();				  // close existing index
 } else					  // don't have exisiting
  indexDocs(file);
}
origin: neo4j/neo4j

for ( LeafReaderContext leafReaderContext : searcher.getIndexReader().leaves() )
  Fields fields = leafReaderContext.reader().fields();
  for ( String field : fields )
          searcher.search( new TermQuery( new Term( field, termsRef ) ), collector );
origin: org.seasar.tuigwaa/tuigwaa-cms

IndexReader reader = IndexReader.open(directory);
int docNum = reader.numDocs();
    if(reader.isDeleted(i)){
      continue;
    Document doc = reader.document(i);
    String curDocId = doc.get(DOC_ID);
    if (doc != null && docId.equals(curDocId)) {
      reader.delete(i);
      break;
reader.close();
directory.close();
origin: oracle/opengrok

} else {
  dir = FSDirectory.open(new File(indexDir, proj).toPath());
  ir = DirectoryReader.open(dir);
  t = new Term(QueryBuilder.FULL, builder.getFreetext());
  getSuggestion(t, ir, dummy);
  s.freetext = dummy.toArray(new String[dummy.size()]);
  t = new Term(QueryBuilder.REFS, builder.getRefs());
  getSuggestion(t, ir, dummy);
  s.refs = dummy.toArray(new String[dummy.size()]);
  t = new Term(QueryBuilder.DEFS, builder.getDefs());
  getSuggestion(t, ir, dummy);
  s.defs = dummy.toArray(new String[dummy.size()]);
if (ir != null && closeOnDestroy) {
  try {
    ir.close();
  } catch (IOException ex) {
    LOGGER.log(Level.WARNING, "Got exception while "
origin: languagetool-org/languagetool

private long getCount(Term term, LuceneSearcher luceneSearcher) {
 long result = 0;
 try {
  TopDocs docs = luceneSearcher.searcher.search(new TermQuery(term), 2000);
  if (docs.totalHits > 2000) {
   throw new RuntimeException("More than 2000 matches for '" + term + "' not supported for performance reasons: " +
                 docs.totalHits + " matches in " + luceneSearcher.directory);
  }
  for (ScoreDoc scoreDoc : docs.scoreDocs) {
   String countStr = luceneSearcher.reader.document(scoreDoc.doc).get("count");
   result += Long.parseLong(countStr);
  }
  //System.out.println(term + " -> " + result);
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
 return result;
}
origin: querydsl/querydsl

private long innerCount() {
  try {
    final int maxDoc = searcher.getIndexReader().maxDoc();
    if (maxDoc == 0) {
      return 0;
    }
    TotalHitCountCollector collector = new TotalHitCountCollector();
    searcher.search(createQuery(), getFilter(), collector);
    return collector.getTotalHits();
  } catch (IOException e) {
    throw new QueryException(e);
  } catch (IllegalArgumentException e) {
    throw new QueryException(e);
  }
}
origin: org.apache.lucene/lucene-queries

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
 IndexSearcher searcher = (IndexSearcher)context.get("searcher");
 TFIDFSimilarity sim = asTFIDF(searcher.getSimilarity(true), field);
 if (sim == null) {
  throw new UnsupportedOperationException("requires a TFIDFSimilarity (such as ClassicSimilarity)");
 }
 int docfreq = searcher.getIndexReader().docFreq(new Term(indexedField, indexedBytes));
 float idf = sim.idf(docfreq, searcher.getIndexReader().maxDoc());
 return new ConstDoubleDocValues(idf, this);
}

origin: org.apache.lucene/lucene-spellchecker

HighFrequencyIterator() throws IOException {
 termsEnum = reader.terms(new Term(field, ""));
 minNumDocs = (int)(thresh * (float)reader.numDocs());
 Term term = termsEnum.term();
 if (term == null || term.field() != field) {
  comp = null;
 } else {
  comp = BytesRef.getUTF8SortedAsUnicodeComparator();
 }
}
org.apache.lucene.indexIndexReader

Javadoc

IndexReader is an abstract class, providing an interface for accessing a point-in-time view of an index. Any changes made to the index via IndexWriter will not be visible until a new IndexReader is opened. It's best to use DirectoryReader#open(IndexWriter) to obtain an IndexReader, if your IndexWriter is in-process. When you need to re-open to see changes to the index, it's best to use DirectoryReader#openIfChanged(DirectoryReader)since the new reader will share resources with the previous one when possible. Search of an index is done entirely through this abstract interface, so that any subclass which implements it is searchable.

There are two different types of IndexReaders:

  • LeafReader: These indexes do not consist of several sub-readers, they are atomic. They support retrieval of stored fields, doc values, terms, and postings.
  • CompositeReader: Instances (like DirectoryReader) of this reader can only be used to get stored fields from the underlying LeafReaders, but it is not possible to directly retrieve postings. To do that, get the sub-readers via CompositeReader#getSequentialSubReaders. Alternatively, you can mimic an LeafReader (with a serious slowdown), by wrapping composite readers with SlowCompositeReaderWrapper.

IndexReader instances for indexes on disk are usually constructed with a call to one of the static DirectoryReader.open() methods, e.g. DirectoryReader#open(org.apache.lucene.store.Directory). DirectoryReader implements the CompositeReader interface, it is not possible to directly get postings.

For efficiency, in this API documents are often referred to via document numbers, non-negative integers which each name a unique document in the index. These document numbers are ephemeral -- they may change as documents are added to and deleted from an index. Clients should thus not rely on a given document having the same number between sessions.

NOTE: IndexReader instances are completely thread safe, meaning multiple threads can call any of its methods, concurrently. If your application requires external synchronization, you should not synchronize on the IndexReader instance; use your own (non-Lucene) objects instead.

Most used methods

  • close
    Closes files associated with this index. Also saves any new deletions to disk. No other methods shou
  • numDocs
    Returns the number of documents in this index.
  • maxDoc
    Returns one greater than the largest possible document number. This may be used to, e.g., determine
  • document
  • open
  • docFreq
    Returns the number of documents containing theterm. This method returns 0 if the term or field does
  • leaves
    Returns the reader's leaves, or itself if this reader is atomic. This is a convenience method callin
  • terms
    Returns an enumeration of all terms starting at a given term. If the given term does not exist, the
  • termDocs
    Returns an enumeration of all the documents which containterm. For each document, the document numbe
  • indexExists
    Returns true if an index exists at the specified directory. If the directory does not exist or if th
  • hasDeletions
    Returns true if any documents have been deleted. Implementers should consider overriding this method
  • isDeleted
    Returns true if document n has been deleted
  • hasDeletions,
  • isDeleted,
  • decRef,
  • getTermFreqVector,
  • numDeletedDocs,
  • getContext,
  • getRefCount,
  • getTermVector,
  • getTermVectors,
  • incRef

Popular in Java

  • Creating JSON documents from java classes using gson
  • getResourceAsStream (ClassLoader)
  • putExtra (Intent)
  • setContentView (Activity)
  • Pointer (com.sun.jna)
    An abstraction for a native pointer data type. A Pointer instance represents, on the Java side, a na
  • VirtualMachine (com.sun.tools.attach)
    A Java virtual machine. A VirtualMachine represents a Java virtual machine to which this Java vir
  • Window (java.awt)
    A Window object is a top-level window with no borders and no menubar. The default layout for a windo
  • Queue (java.util)
    A collection designed for holding elements prior to processing. Besides basic java.util.Collection o
  • ServletException (javax.servlet)
    Defines a general exception a servlet can throw when it encounters difficulty.
  • HttpServletRequest (javax.servlet.http)
    Extends the javax.servlet.ServletRequest interface to provide request information for HTTP servlets.
  • Top 12 Jupyter Notebook extensions
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now