Example usage for org.apache.lucene.index IndexWriterConfig setOpenMode

List of usage examples for org.apache.lucene.index IndexWriterConfig setOpenMode

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexWriterConfig setOpenMode.

Prototype

public IndexWriterConfig setOpenMode(OpenMode openMode) 

Source Link

Document

Specifies OpenMode of the index.

Usage

From source file:org.bidtime.lucene.index.LuceneIndexAndSearchDemo.java

License:Apache License

/**
 * // w w w . j av  a2 s.c  o  m
 * ???
 * @param args
 */
public static void main(String[] args) {
    //Lucene Document??
    String fieldName = "hanzi";
    String quanpin = "pinyin";
    String shouzimu = "shouzimu";
    //
    String text = "IK Analyzer???????";

    //IKAnalyzer?
    //PerFieldAnalyzerWrapper???field???
    Map<String, Analyzer> analyzerMap = new HashMap<String, Analyzer>();
    analyzerMap.put(quanpin, new IKAnalyzer4PinYin(false, IKAnalyzer4PinYin.PINYIN));
    analyzerMap.put(shouzimu, new IKAnalyzer4PinYin(false, IKAnalyzer4PinYin.PINYIN_SHOUZIMU));
    PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(new IKAnalyzer4PinYin(false), analyzerMap);

    Directory directory = null;
    IndexWriter iwriter = null;
    IndexReader ireader = null;
    IndexSearcher isearcher = null;
    try {
        //
        directory = new RAMDirectory();

        //?IndexWriterConfig
        IndexWriterConfig iwConfig = new IndexWriterConfig(wrapper);
        iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
        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));
        doc.add(new TextField(quanpin, text, Field.Store.YES));
        doc.add(new TextField(shouzimu, text, Field.Store.YES));

        iwriter.addDocument(doc);
        iwriter.close();

        //?**********************************
        //?   
        ireader = DirectoryReader.open(directory);
        isearcher = new IndexSearcher(ireader);

        String keyword = "";
        //QueryParser?Query
        Analyzer analyzer = new IKAnalyzer4PinYin(true);
        QueryParser qp = new QueryParser(fieldName, analyzer);
        QueryParser qpQuanpin = new QueryParser(quanpin, analyzer);
        QueryParser qpShouzimu = new QueryParser(shouzimu, analyzer);

        Query query = qp.parse(keyword);
        Query queryQuanpin = qpQuanpin.parse(keyword);
        Query queryShouzimu = qpShouzimu.parse(keyword);
        //            
        BooleanQuery bq = new BooleanQuery();
        BooleanQuery innerbq = new BooleanQuery();
        //            
        bq.add(query, BooleanClause.Occur.SHOULD);
        bq.add(queryQuanpin, BooleanClause.Occur.SHOULD);
        bq.add(queryShouzimu, BooleanClause.Occur.SHOULD);
        innerbq.add(bq, BooleanClause.Occur.MUST);

        //         System.out.println("innerbq = " + innerbq);

        //?5?
        TopDocs topDocs = isearcher.search(innerbq, 5);
        System.out.println("" + topDocs.totalHits);
        //
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (int i = 0; i < topDocs.totalHits; i++) {
            Document targetDoc = isearcher.doc(scoreDocs[i].doc);
            System.out.println("" + targetDoc.toString());
        }

    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (LockObtainFailedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    } finally {
        if (ireader != null) {
            try {
                ireader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (directory != null) {
            try {
                directory.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.bireme.interop.fromJson.Json2Lucene.java

License:Open Source License

public Json2Lucene(final String luceneDir, final boolean store, final boolean append) throws IOException {
    if (luceneDir == null) {
        throw new NullPointerException("luceneDir");
    }/*w ww .j av  a 2  s  .c  om*/
    final Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_9);
    final IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_9, analyzer);

    config.setOpenMode(append ? IndexWriterConfig.OpenMode.APPEND : IndexWriterConfig.OpenMode.CREATE);
    directory = new SimpleFSDirectory(new File(luceneDir));
    iwriter = new IndexWriter(directory, config);
    this.store = store ? Field.Store.YES : Field.Store.NO;
}

From source file:org.brutusin.fleadb.impl.GenericFleaDB.java

License:Apache License

private IndexWriter getIndexWriter() throws IOException {
    if (indexWriter == null) {
        synchronized (this) {
            if (indexWriter == null) {
                IndexWriterConfig config = new IndexWriterConfig(LUCENE_VERSION, null);
                config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
                this.indexWriter = new IndexWriter(indexDir, config);
            }//from   w  w  w .j  ava 2 s  . c  o  m
        }
    }
    return indexWriter;
}

From source file:org.codice.ddf.spatial.geocoding.index.GeoNamesLuceneIndexer.java

License:Open Source License

IndexWriter createIndexWriter(final boolean create, final Directory directory) throws IOException {

    final IndexWriterConfig indexWriterConfig = new IndexWriterConfig(ANALYZER);

    // Set to CREATE mode if the index does not exist.
    if (!DirectoryReader.indexExists(directory)) {
        indexWriterConfig.setOpenMode(OpenMode.CREATE);
    } else {/*w  w w .  j ava2s . c o  m*/
        indexWriterConfig.setOpenMode(create ? OpenMode.CREATE : OpenMode.APPEND);
    }
    indexWriterConfig.setSimilarity(SIMILARITY);
    return new IndexWriter(directory, indexWriterConfig);
}

From source file:org.codice.ddf.spatial.geocoding.query.GeoNamesQueryLuceneIndexTest.java

License:Open Source License

private void initializeIndex() throws IOException {
    directory = new RAMDirectory();

    final IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
    indexWriterConfig.setOpenMode(OpenMode.CREATE);

    final IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);

    indexWriter.addDocument(createDocumentFromGeoEntry(GEO_ENTRY_1));
    indexWriter.addDocument(createDocumentFromGeoEntry(GEO_ENTRY_2));
    indexWriter.addDocument(createDocumentFromGeoEntry(GEO_ENTRY_3));

    indexWriter.close();/*from  ww  w  .java2s  .  c om*/
}

From source file:org.dbpedia.spotlight.lucene.index.BaseIndexer.java

License:Apache License

/**
 * Base class with the indexing functionality used by the subclasses {@link SeparateOccurrencesIndexer} and {@link MergedOccurrencesContextIndexer}}.
 * @param lucene//from  ww  w.ja v  a  2s. co  m
 * @param create - what to do if lucene.mContextIndexDir already exists (so we do not unvoluntary add to an existing index). true to create the index or overwrite the existing one; false to append to the existing index
 * @throws IOException
 */
public BaseIndexer(LuceneManager lucene, boolean create) throws IOException {
    this.mLucene = lucene;

    //TODO this config is new in 3.6, does basically what LuceneManager does for us.
    IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_36, lucene.defaultAnalyzer());
    iwConfig.setOpenMode(create ? IndexWriterConfig.OpenMode.CREATE : IndexWriterConfig.OpenMode.APPEND);

    /* Determines ... buffering added documents and deletions before they are flushed to the Directory.
    NOTE: because IndexWriter uses ints when managing its internal storage,
    (...) it's best to set this value comfortably under 2048.
    http://lucene.apache.org/java/3_0_2/api/all/org/apache/lucene/index/IndexWriter.html#setRAMBufferSizeMB%28double%29
     */
    iwConfig.setRAMBufferSizeMB(lucene.RAMBufferSizeMB());

    /*
      Generally for faster indexing performance it's best to flush by RAM usage instead of document count and use as large a RAM buffer as you can.
      http://lucene.apache.org/java/3_0_2/api/all/org/apache/lucene/index/IndexWriter.html#setRAMBufferSizeMB%28double%29
            
      But if setting by doc count, the sweet spot suggested is 48 http://issues.apache.org/jira/browse/LUCENE-843
     */
    //this.mWriter.setMaxBufferedDocs(lucene.RAMBufferSizeMB());

    this.mWriter = new IndexWriter(lucene.directory(), iwConfig);

}

From source file:org.deeplearning4j.text.invertedindex.LuceneInvertedIndex.java

License:Apache License

private synchronized TrackingIndexWriter getWriterWithRetry() {
    if (this.indexWriter != null)
        return this.indexWriter;

    IndexWriterConfig iwc;
    IndexWriter writer = null;/*from  w  w  w. j av  a  2s  . co m*/

    try {
        if (analyzer == null)
            analyzer = new StandardAnalyzer(new InputStreamReader(new ByteArrayInputStream("".getBytes())));

        ensureDirExists();

        if (this.indexWriter == null) {
            indexBeingCreated.set(true);

            iwc = new IndexWriterConfig(Version.LATEST, analyzer);
            iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
            iwc.setWriteLockTimeout(1000);

            log.info("Creating new index writer");
            while ((writer = tryCreateWriter(iwc)) == null) {
                log.warn("Failed to create writer...trying again");
                iwc = new IndexWriterConfig(Version.LATEST, analyzer);
                iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
                iwc.setWriteLockTimeout(1000);

                Thread.sleep(10000);
            }
            this.indexWriter = new TrackingIndexWriter(writer);

        }

    }

    catch (Exception e) {
        throw new IllegalStateException(e);
    }

    return this.indexWriter;
}

From source file:org.Demo.IndexFiles.java

License:Apache License

/** Index all text files under a directory. */
public static void main(String[] args) {
    String usage = "java org.apache.lucene.demo.IndexFiles"
            + " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n"
            + "This indexes the documents in DOCS_PATH, creating a Lucene index"
            + "in INDEX_PATH that can be searched with SearchFiles";
    String indexPath = "index";
    String docsPath = null;/*from  www  .j  ava  2s  . c  o m*/
    boolean create = true;
    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            indexPath = args[i + 1];
            i++;
        } else if ("-docs".equals(args[i])) {
            docsPath = args[i + 1];
            i++;
        } else if ("-update".equals(args[i])) {
            create = false;
        }
    }

    if (docsPath == null) {
        System.err.println("Usage: " + usage);
        System.exit(1);
    }

    final Path docDir = Paths.get(docsPath);
    if (!Files.isReadable(docDir)) {
        System.out.println("Document directory '" + docDir.toAbsolutePath()
                + "' does not exist or is not dable, please check the path");
        System.exit(1);
    }

    Date start = new Date();
    try {
        System.out.println("Indexing to directory '" + indexPath + "'...");

        Directory dir = FSDirectory.open(Paths.get(indexPath));
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

        if (create) {
            // Create a new index in the directory, removing any
            // previously indexed documents:
            iwc.setOpenMode(OpenMode.CREATE);
        } else {
            // Add new documents to an existing index:
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }

        // Optional: for better indexing performance, if you
        // are indexing many documents, increase the RAM
        // buffer.  But if you do this, increase the max heap
        // size to the JVM (eg add -Xmx512m or -Xmx1g):
        //
        // iwc.setRAMBufferSizeMB(256.0);

        IndexWriter writer = new IndexWriter(dir, iwc);
        indexDocs(writer, docDir);

        // NOTE: if you want to maximize search performance,
        // you can optionally call forceMerge here.  This can be
        // a terribly costly operation, so generally it's only
        // worth it when your index is relatively static (ie
        // you're done adding documents to it):
        //
        // writer.forceMerge(1);

        writer.close();

        Date end = new Date();
        System.out.println(end.getTime() - start.getTime() + " total milliseconds");

    } catch (IOException e) {
        System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }
}

From source file:org.dizitart.no2.services.LuceneService.java

License:Apache License

public LuceneService() {
    try {/*from  w w w  . ja v a2 s  .c o  m*/
        this.keySerializer = new ObjectMapper();
        keySerializer.setVisibility(keySerializer.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE));

        indexDirectory = new RAMDirectory();
        analyzer = new StandardAnalyzer();

        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
        iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        indexWriter = new IndexWriter(indexDirectory, iwc);
        commit();
    } catch (IOException e) {
        throw new IndexingException(errorMessage("could not create full-text index", 0), e);
    } catch (VirtualMachineError vme) {
        handleVirtualMachineError(vme);
    }
}

From source file:org.dizitart.no2.services.LuceneService.java

License:Apache License

@Override
public void drop() {
    try {// w  w  w  .j ava 2 s  .  c  o  m
        indexDirectory = new RAMDirectory();
        analyzer = new StandardAnalyzer();

        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
        iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        indexWriter = new IndexWriter(indexDirectory, iwc);
        commit();
    } catch (IOException e) {
        throw new IndexingException(errorMessage("could not drop full-text index", 0), e);
    }
}