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

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

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

Refine searchRefine arrow

  • IndexWriterConfig
  • Document
  • Term
  • Field
  • DirectoryReader
  • IndexReader
  • FSDirectory
  • StringField
  • StandardAnalyzer
origin: FudanNLP/fnlp

System.out.println("Indexing to directory '" + indexPath  + "'...");
Date start = new Date();
Directory dir = FSDirectory.open(new File(indexPath));//Dirctory dir-->FSDirectory
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_47, analyzer);
iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
IndexWriter writer = new IndexWriter(dir, iwc);
  Document doc = new Document();
  doc.add(field);
  if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {
    writer.addDocument(doc);
  } else {
    writer.updateDocument(new Term("content",strs[i]), doc);
writer.close();
origin: stanfordnlp/CoreNLP

private void addPatterns(String id, Map<Integer, Set<E>> p, boolean commit) {
 try{
  setIndexWriter();
  Document doc = new Document();
  doc.add(new StringField("sentid", id, Field.Store.YES));
  doc.add(new Field("patterns", getBytes(p), LuceneFieldType.NOT_INDEXED));
  indexWriter.addDocument(doc);
  if(commit){
   indexWriter.commit();
   //closeIndexWriter();
  }
 }catch(IOException e){
  throw new RuntimeException(e);
 }
}
origin: oracle/opengrok

/**
 * Writes a document to contain the serialized version of {@code settings},
 * with a {@link QueryBuilder#OBJUID} value set to
 * {@link #INDEX_ANALYSIS_SETTINGS_OBJUID}. An existing version of the
 * document is first deleted.
 * @param writer a defined, target instance
 * @param settings a defined instance
 * @throws IOException if I/O error occurs while writing Lucene
 */
public void write(IndexWriter writer, IndexAnalysisSettings settings)
    throws IOException {
  byte[] objser = settings.serialize();
  writer.deleteDocuments(new Term(QueryBuilder.OBJUID,
    INDEX_ANALYSIS_SETTINGS_OBJUID));
  Document doc = new Document();
  StringField uidfield = new StringField(QueryBuilder.OBJUID,
    INDEX_ANALYSIS_SETTINGS_OBJUID, Field.Store.NO);
  doc.add(uidfield);
  doc.add(new StoredField(QueryBuilder.OBJSER, objser));
  doc.add(new StoredField(QueryBuilder.OBJVER,
    INDEX_ANALYSIS_SETTINGS_OBJVER));
  writer.addDocument(doc);
}
origin: neo4j/neo4j

private void applyDocuments( IndexWriter writer, IndexType type, LongObjectMap<DocumentContext> documents ) throws IOException
{
  for ( DocumentContext context : documents )
  {
    if ( context.exists )
    {
      if ( LuceneDataSource.documentIsEmpty( context.document ) )
      {
        writer.deleteDocuments( type.idTerm( context.entityId ) );
      }
      else
      {
        writer.updateDocument( type.idTerm( context.entityId ), context.document );
      }
    }
    else
    {
      writer.addDocument( context.document );
    }
  }
}
origin: neo4j/neo4j

private static void writeSomething( IndexReference indexReference ) throws IOException
{
  IndexWriter writer = indexReference.getWriter();
  writer.addDocument( new Document() );
  writer.commit();
}
origin: org.apache.lucene/lucene-demo

/** Build the example index. */
public void index() throws IOException {
 IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(
   new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
 // Add documents with a fake timestamp, 1000 sec before
 // "now", 2000 sec before "now", ...:
 for(int i=0;i<100;i++) {
  Document doc = new Document();
  long then = nowSec - i * 1000;
  // Add as doc values field, so we can compute range facets:
  doc.add(new NumericDocValuesField("timestamp", then));
  // Add as numeric field so we can drill-down:
  doc.add(new LongPoint("timestamp", then));
  indexWriter.addDocument(doc);
 }
 // Open near-real-time searcher
 searcher = new IndexSearcher(DirectoryReader.open(indexWriter));
 indexWriter.close();
}
origin: hibernate/hibernate-search

private void indexTestDocuments(Directory directory) throws IOException {
  IndexWriterConfig indexWriterConfig = new IndexWriterConfig( new StandardAnalyzer() );
  indexWriterConfig.setOpenMode( IndexWriterConfig.OpenMode.CREATE );
  IndexWriter indexWriter = new IndexWriter( directory, indexWriterConfig );
  Document document = new Document();
  document.add( new StringField( "stringField", "test", Field.Store.NO ) );
  document.add( new IntField( "intField", 0, Field.Store.NO ) );
  indexWriter.addDocument( document );
  indexWriter.commit();
  indexWriter.close();
}
origin: us.ihmc/IHMCJavaToolkit

private void initLucene() throws IOException
{
 StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
 Directory index = new RAMDirectory();
 IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_43, analyzer);
 IndexWriter writer = new IndexWriter(index, config);
 for (String s : classList.keySet())
 {
   Document doc = new Document();
   doc.add(new org.apache.lucene.document.TextField("classname", s, Field.Store.YES));
   writer.addDocument(doc);
 }
 writer.close();
 queryParser = new QueryParser(Version.LUCENE_43, "classname", analyzer);
 searcher = new IndexSearcher(DirectoryReader.open(index));
}
origin: apache/jackrabbit-oak

private static Directory createSampleDirectory(long numOfDocs, Iterable<Document> docs) throws IOException {
  Directory dir = new RAMDirectory();
  IndexWriterConfig config = new IndexWriterConfig(VERSION, LuceneIndexConstants.ANALYZER);
  IndexWriter writer = new  IndexWriter(dir, config);
  for (int i = 0; i < numOfDocs; i++) {
    Document doc = new Document();
    doc.add(new StringField("foo", "bar" + i, Field.Store.NO));
    writer.addDocument(doc);
  }
  for (Document doc : docs) {
    writer.addDocument(doc);
  }
  writer.close();
  return dir;
}
origin: com.h2database/h2

/**
 * Get the index writer/searcher wrapper for the given connection.
 *
 * @param conn the connection
 * @return the index access wrapper
 */
protected static IndexAccess getIndexAccess(Connection conn)
    throws SQLException {
  String path = getIndexPath(conn);
  synchronized (INDEX_ACCESS) {
    IndexAccess access = INDEX_ACCESS.get(path);
    if (access == null) {
      try {
        Directory indexDir = path.startsWith(IN_MEMORY_PREFIX) ?
            new RAMDirectory() : FSDirectory.open(new File(path));
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_30, analyzer);
        conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        IndexWriter writer = new IndexWriter(indexDir, conf);
        //see http://wiki.apache.org/lucene-java/NearRealtimeSearch
        access = new IndexAccess(writer);
      } catch (IOException e) {
        throw convertException(e);
      }
      INDEX_ACCESS.put(path, access);
    }
    return access;
  }
}
origin: jeremylong/DependencyCheck

    IndexWriter indexWriter = new IndexWriter(index, new IndexWriterConfig(analyzer))) {
  final Document doc = new Document();
  final Field v = new TextField(Fields.VENDOR, Fields.VENDOR, Field.Store.YES);
  final Field p = new TextField(Fields.PRODUCT, Fields.PRODUCT, Field.Store.YES);
  doc.add(v);
  doc.add(p);
      v.setStringValue(pair.getLeft());
      p.setStringValue(pair.getRight());
      indexWriter.addDocument(doc);
      productFieldAnalyzer.reset();
      vendorFieldAnalyzer.reset();
  indexWriter.commit();
} catch (DatabaseException ex) {
  LOGGER.debug("", ex);
origin: oracle/opengrok

try {
  Analyzer analyzer = AnalyzerGuru.getAnalyzer();
  IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
  iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
  iwc.setRAMBufferSizeMB(env.getRamBufferSize());
  iwc.setCodec(new Lucene70Codec(
    Lucene50StoredFieldsFormat.Mode.BEST_COMPRESSION));
  writer = new IndexWriter(indexDirectory, iwc);
  writer.commit(); // to make sure index exists on the disk
  completer = new PendingFileCompleter();
    reader = DirectoryReader.open(indexDirectory); // open existing index
    settings = readAnalysisSettings();
    if (settings == null) {
    int numDocs = reader.numDocs();
    if (numDocs > 0) {
      reader.close();
  try {
    if (writer != null) {
      writer.close();
origin: apache/tika

IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
int maxLen = 1000000;
int len = 0;
try (IndexWriter writer = new IndexWriter(directory, indexWriterConfig)) {
  List<Document> docs = new ArrayList<>();
  try (BufferedReader reader = getReader(inputFile)) {
    while (line != null) {
      len += line.length();
      Document document = new Document();
      document.add(new TextField(FIELD, line, Field.Store.NO));
      docs.add(document);
      if (len > maxLen) {
        writer.addDocuments(docs);
        docs.clear();
        len = 0;
    writer.addDocuments(docs);
  writer.commit();
  writer.flush();
try (IndexReader reader = DirectoryReader.open(directory)) {
  LeafReader wrappedReader = SlowCompositeReaderWrapper.wrap(reader);
  Terms terms = wrappedReader.terms(FIELD);
origin: opentripplanner/OpenTripPlanner

  long startTime = System.currentTimeMillis();
  directory = FSDirectory.open(new File(basePath, "lucene"));
  IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_47, analyzer).setOpenMode(OpenMode.CREATE);
  final IndexWriter writer = new IndexWriter(directory, config);
  for (Stop stop : graphIndex.stopForId.values()) {
    addStop(writer, stop);
    addCorner(writer, sv);
  writer.close();
  long elapsedTime = System.currentTimeMillis() - startTime;
  LOG.info("Built Lucene index in {} msec", elapsedTime);
  searcher = new IndexSearcher(DirectoryReader.open(directory));
} catch (Exception ex) {
  throw new RuntimeException("Lucene indexing failed.", ex);
origin: stackoverflow.com

Query query = new TermQuery(new Term(GRAMMED_WORDS_FIELD, term));
Sort sort = new Sort(COUNT_FIELD, true);
List<String> suggestions = new ArrayList<String>();
for (ScoreDoc doc : docs.scoreDocs) {
  suggestions.add(autoCompleteReader.document(doc.doc).get(
      SOURCE_WORD_FIELD));
writer.setMergeFactor(300);
writer.setMaxBufferedDocs(150);
    wordsMap.put(word, sourceReader.docFreq(new Term(
        fieldToAutocomplete, word)));
  writer.addDocument(doc);
sourceReader.close();
writer.optimize();
writer.close();
origin: apache/ignite

Object val = v.isPlatformType() ? v.value(coctx, false) : v;
Document doc = new Document();
  doc.add(new TextField(VAL_STR_FIELD_NAME, val.toString(), Field.Store.YES));
    doc.add(new TextField(idxdFields[i], fieldVal.toString(), Field.Store.YES));
  final Term term = new Term(KEY_FIELD_NAME, keyByteRef);
    writer.deleteDocuments(term);
  doc.add(new StringField(KEY_FIELD_NAME, keyByteRef, Field.Store.YES));
  writer.updateDocument(term, doc);
origin: oracle/opengrok

String projectDetail = this.project != null ? " for project " + project.getName() : "";
LOGGER.log(Level.INFO, "Optimizing the index{0}", projectDetail);
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
conf.setOpenMode(OpenMode.CREATE_OR_APPEND);
wrt = new IndexWriter(indexDirectory, conf);
wrt.forceMerge(1); // this is deprecated and not needed anymore
elapsed.report(LOGGER, String.format("Done optimizing index%s", projectDetail));
synchronized (lock) {
if (wrt != null) {
  try {
    wrt.close();
  } catch (IOException e) {
    if (writerException == null) {
origin: Impetus/Kundera

QueryParser qp = new QueryParser(DEFAULT_SEARCHABLE_FIELD, new StandardAnalyzer());
Query q = qp.parse(luceneQuery);
w.deleteDocuments(q);
w.commit();
w.close();
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
LogDocMergePolicy logDocMergePolicy = new LogDocMergePolicy();
logDocMergePolicy.setMergeFactor(1000);
indexWriterConfig.setMergePolicy(logDocMergePolicy);
w = new IndexWriter(index, indexWriterConfig);
w.getConfig().setRAMBufferSizeMB(32);
origin: org.infinispan/infinispan-lucene-directory

public static void initializeDirectory(Directory directory) throws IOException {
 IndexWriterConfig indexWriterConfig = new IndexWriterConfig(LuceneSettings.analyzer);
 IndexWriter iwriter = new IndexWriter(directory, indexWriterConfig);
 iwriter.commit();
 iwriter.close();
 //reopen to check for index
 IndexReader reader = DirectoryReader.open(directory);
 reader.close();
}
origin: dermotte/LIRE

  tmpAnalyzer = new KeywordAnalyzer(); // entire string as one token.
else if (analyzer == AnalyzerType.StandardAnalyzer)
  tmpAnalyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(tmpAnalyzer);
config.setRAMBufferSizeMB(512);
config.setCommitOnClose(true);
if (create)
  config.setOpenMode(IndexWriterConfig.OpenMode.CREATE); // overwrite if it exists.
config.setCodec(new LireCustomCodec());
return new IndexWriter(directory, config);
org.apache.lucene.indexIndexWriter

Javadoc

An IndexWriter creates and maintains an index.

The OpenMode option on IndexWriterConfig#setOpenMode(OpenMode) determines whether a new index is created, or whether an existing index is opened. Note that you can open an index with OpenMode#CREATEeven while readers are using the index. The old readers will continue to search the "point in time" snapshot they had opened, and won't see the newly created index until they re-open. If OpenMode#CREATE_OR_APPEND is used IndexWriter will create a new index if there is not already an index at the provided path and otherwise open the existing index.

In either case, documents are added with #addDocument(Iterable) and removed with #deleteDocuments(Term...) or #deleteDocuments(Query...). A document can be updated with #updateDocument(Term,Iterable) (which just deletes and then adds the entire document). When finished adding, deleting and updating documents, #close() should be called.

These changes are buffered in memory and periodically flushed to the Directory (during the above method calls). A flush is triggered when there are enough added documents since the last flush. Flushing is triggered either by RAM usage of the documents (see IndexWriterConfig#setRAMBufferSizeMB) or the number of added documents (see IndexWriterConfig#setMaxBufferedDocs(int)). The default is to flush when RAM usage hits IndexWriterConfig#DEFAULT_RAM_BUFFER_SIZE_MB MB. For best indexing speed you should flush by RAM usage with a large RAM buffer. Additionally, if IndexWriter reaches the configured number of buffered deletes (see IndexWriterConfig#setMaxBufferedDeleteTerms) the deleted terms and queries are flushed and applied to existing segments. In contrast to the other flush options IndexWriterConfig#setRAMBufferSizeMB and IndexWriterConfig#setMaxBufferedDocs(int), deleted terms won't trigger a segment flush. Note that flushing just moves the internal buffered state in IndexWriter into the index, but these changes are not visible to IndexReader until either #commit() or #close is called. A flush may also trigger one or more segment merges which by default run with a background thread so as not to block the addDocument calls (see below for changing the MergeScheduler).

Opening an IndexWriter creates a lock file for the directory in use. Trying to open another IndexWriter on the same directory will lead to a LockObtainFailedException.

Expert: IndexWriter allows an optional IndexDeletionPolicy implementation to be specified. You can use this to control when prior commits are deleted from the index. The default policy is KeepOnlyLastCommitDeletionPolicy which removes all prior commits as soon as a new commit is done (this matches behavior before 2.2). Creating your own policy can allow you to explicitly keep previous "point in time" commits alive in the index for some time, to allow readers to refresh to the new commit without having the old commit deleted out from under them. This is necessary on filesystems like NFS that do not support "delete on last close" semantics, which Lucene's "point in time" search normally relies on.

Expert: IndexWriter allows you to separately change the MergePolicy and the MergeScheduler. The MergePolicy is invoked whenever there are changes to the segments in the index. Its role is to select which merges to do, if any, and return a MergePolicy.MergeSpecification describing the merges. The default is LogByteSizeMergePolicy. Then, the MergeScheduler is invoked with the requested merges and it decides when and how to run the merges. The default is ConcurrentMergeScheduler.

NOTE: if you hit a VirtualMachineError, or disaster strikes during a checkpoint then IndexWriter will close itself. This is a defensive measure in case any internal state (buffered documents, deletions, reference counts) were corrupted. Any subsequent calls will throw an AlreadyClosedException.

NOTE: IndexWriter 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 IndexWriter instance as this may cause deadlock; use your own (non-Lucene) objects instead.

NOTE: If you call Thread.interrupt() on a thread that's within IndexWriter, IndexWriter will try to catch this (eg, if it's in a wait() or Thread.sleep()), and will then throw the unchecked exception ThreadInterruptedExceptionand clear the interrupt status on the thread.

Most used methods

  • <init>
    Expert: constructs an IndexWriter with a custom IndexDeletionPolicy, for the index in d. Text will b
  • close
    Closes the index with or without waiting for currently running merges to finish. This is only meanin
  • addDocument
    Adds a document to this index, using the provided analyzer instead of the value of #getAnalyzer(). I
  • commit
  • deleteDocuments
    Deletes the document(s) matching any of the provided queries. All deletes are flushed at the same ti
  • updateDocument
    Updates a document by first deleting the document(s) containing term and then adding the new documen
  • forceMerge
    Just like #forceMerge(int), except you can specify whether the call should block until all merging c
  • deleteAll
    Delete all documents in the index. This method will drop all buffered documents and will remove all
  • optimize
    Just like #optimize(), except you can specify whether the call should block until the optimize compl
  • rollback
    Close the IndexWriter without committing any changes that have occurred since the last commit (or si
  • addIndexes
    Merges all segments from an array of indexes into this index.
  • getConfig
    Returns a LiveIndexWriterConfig, which can be used to query the IndexWriter current settings, as wel
  • addIndexes,
  • getConfig,
  • getDirectory,
  • addDocuments,
  • isOpen,
  • isLocked,
  • numDocs,
  • unlock,
  • flush,
  • forceMergeDeletes

Popular in Java

  • Finding current android device location
  • getSharedPreferences (Context)
  • findViewById (Activity)
  • scheduleAtFixedRate (ScheduledExecutorService)
  • GridLayout (java.awt)
    The GridLayout class is a layout manager that lays out a container's components in a rectangular gri
  • OutputStream (java.io)
    A writable sink for bytes.Most clients will use output streams that write data to the file system (
  • ConnectException (java.net)
    A ConnectException is thrown if a connection cannot be established to a remote host on a specific po
  • PriorityQueue (java.util)
    A PriorityQueue holds elements on a priority heap, which orders the elements according to their natu
  • Project (org.apache.tools.ant)
    Central representation of an Ant project. This class defines an Ant project with all of its targets,
  • IsNull (org.hamcrest.core)
    Is the value null?
  • Top plugins for WebStorm
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