Example usage for org.apache.lucene.index IndexWriter IndexWriter

List of usage examples for org.apache.lucene.index IndexWriter IndexWriter

Introduction

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

Prototype

public IndexWriter(Directory d, IndexWriterConfig conf) throws IOException 

Source Link

Document

Constructs a new IndexWriter per the settings given in conf.

Usage

From source file:com.github.alvanson.xltsearch.IndexTask.java

License:Apache License

@Override
protected Boolean call() {
    IndexWriter iwriter = null;/*from   www  .j  a  v  a  2  s .c  om*/
    boolean result = false;

    updateMessage("started");
    try {
        int count = 0;
        Docket docket;

        IndexWriterConfig iwconfig = new IndexWriterConfig(config.getVersion(), config.getAnalyzer());
        iwconfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        iwconfig.setSimilarity(config.getSimilarity());
        iwriter = new IndexWriter(config.getDirectory(), iwconfig);

        while ((docket = inQueue.take()) != Docket.DONE) {
            count++;
            updateMessage(docket.relPath);
            switch (docket.status) {
            case PARSED:
                // index parsed file
                Document doc = new Document();
                // store relative path  ** must be indexed for updateDocument
                doc.add(new StringField(config.pathField, docket.relPath, Field.Store.YES));
                // index content
                doc.add(new TextField(config.contentField, docket.content.toString(), Field.Store.NO));
                // index standard metadata
                for (Map.Entry<String, Property> e : config.metadataFields.entrySet()) {
                    for (String value : docket.metadata.getValues(e.getValue())) {
                        doc.add(new TextField(e.getKey(), value, Field.Store.YES));
                    }
                }
                // store hashsum
                doc.add(new StringField(config.hashSumField, docket.hashSum, Field.Store.YES));
                // add/update document
                iwriter.updateDocument(new Term(config.pathField, docket.relPath), doc);
                // fall through
            case PASS:
                break;
            case DELETE:
                iwriter.deleteDocuments(new Term(config.pathField, docket.relPath));
                break;
            default:
                logger.error("Unexpected docket state while processing {}: {}", docket.relPath,
                        docket.status.toString());
                cancel(true); // cancel task
            }
            updateProgress(count, count + docket.workLeft);
        }
        // end of queue
        updateMessage("complete");
        updateProgress(count, count + docket.workLeft);
        result = true;
    } catch (IOException ex) {
        updateMessage("I/O exception");
        logger.error("I/O exception while writing to index", ex);
    } catch (InterruptedException ex) {
        if (isCancelled()) {
            updateMessage("cancelled");
        } else {
            updateMessage("interrupted");
            logger.error("Interrupted", ex);
        }
    }
    // close iwriter
    if (iwriter != null) {
        try {
            iwriter.close();
        } catch (IOException ex) {
            logger.warn("I/O exception while closing index writer", ex);
        }
    }
    return result;
}

From source file:com.github.buzztaiki.lucene.lastuni.CJKSingleCharQueryTest.java

License:Apache License

private IndexWriter newWriter(Directory dir, Analyzer analyzer) throws IOException {
    return new IndexWriter(dir,
            new IndexWriterConfig(TEST_VERSION_CURRENT, analyzer).setOpenMode(OpenMode.CREATE));
}

From source file:com.github.cmisbox.persistence.Storage.java

License:Open Source License

private Storage() {
    this.log = LogFactory.getLog(this.getClass());
    this.config = Config.getInstance();
    File file = new File(this.config.getConfigHome(), Storage.indexFolderName);
    if (!file.exists()) {
        file.mkdirs();//w ww.  ja va  2  s.c om
        this.log.info(Messages.createdIndexFolder);
    }
    try {
        this.directory = FSDirectory.open(file);
        this.writer = new IndexWriter(this.directory,
                new IndexWriterConfig(Version.LUCENE_33, new StandardAnalyzer(Version.LUCENE_33)));

        this.reader = IndexReader.open(this.writer, false);

        List<String> roots = this.getRootPaths();

        for (String root : roots) {
            Watcher.getInstance().addWatch(root);
        }

    } catch (Exception e) {
        this.log.fatal(e);
        Main.exit(1);
    }

}

From source file:com.github.flaxsearch.testutil.Fixtures.java

License:Apache License

private static void populateIndex(Directory directory) {
    try (IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig())) {

        {//  w w w  . j a v a  2s  .c o m
            Document doc = new Document();
            doc.add(new TextField("field2", "here is some text", Field.Store.YES));
            doc.add(new StringField("field1", "value1", Field.Store.YES));
            doc.add(new IntPoint("point", 2, 4));
            doc.add(new IntPoint("point", 0, 1));
            doc.add(new IntPoint("point", 2, 1));
            doc.add(new IntPoint("point", 14, 4));
            writer.addDocument(doc);
            // more than one segment
            writer.commit();
        }

        {
            Document doc = new Document();
            doc.add(new StringField("field1", "value2", Field.Store.YES));
            doc.add(new BinaryDocValuesField("field1", new BytesRef("some bytes")));
            doc.add(new TextField("field3",
                    "this is some more text in a different field value1 value11 value12 value21",
                    Field.Store.YES));
            writer.addDocument(doc);
        }

    } catch (IOException e) {
        throw new RuntimeException("We're a RAMDirectory, this should never happen!");
    }
}

From source file:com.github.flaxsearch.testutil.GutenbergIndex.java

License:Apache License

public static void main(String... args) throws IOException {

    Path source = Paths.get("src/test/resources/gutenberg");
    Path index = Paths.get("src/test/resources/index");

    clearDirectory(index);//  w w  w.j  a va 2  s .c  o  m
    try (Directory directory = FSDirectory.open(index);
            IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig())) {
        writeDocuments(writer, source);
    }

}

From source file:com.github.hotware.lucene.extension.manager.LuceneManagerImpl.java

License:BEER-WARE LICENSE

@Override
public final IndexWriter getIndexWriter(IndexWriterConfig config) throws IOException {
    return new IndexWriter(this.directory, config);
}

From source file:com.github.jiloc.USTweetsAnalyzer.Store.java

public Store() throws IOException {
    states = new HashSet<String>();
    //first we empty our directory 
    resetDir(new File("tweet_idex"));
    // The process of writing indexing
    dir = new SimpleFSDirectory(new File("tweet_index"));
    analyzer = new StandardAnalyzer(LUCENE_41);
    cfg = new IndexWriterConfig(LUCENE_41, analyzer);
    writer = new IndexWriter(dir, cfg);
    // a document contains a list of field(s)
    doc = new Document();

    s = new StringField("STATE", "", Field.Store.YES);
    l = new TextField("LOC", "", Field.Store.YES);

    doc.add(s);//from  w ww.  ja va2s . com
    doc.add(l);
}

From source file:com.github.le11.nls.lucene.UIMABaseAnalyzerTest.java

License:Apache License

@Before
public void setUp() throws Exception {
    dir = new RAMDirectory();
    analyzer = new UIMABaseAnalyzer("/WhitespaceTokenizer.xml", "org.apache.uima.TokenAnnotation");
    writer = new IndexWriter(dir, new IndexWriterConfig(Version.LUCENE_32, analyzer));
}

From source file:com.github.le11.nls.lucene.UIMAPayloadsAnalyzerTest.java

License:Apache License

@Before
public void setUp() throws Exception {
    dir = new RAMDirectory();
    analyzer = new UIMAPayloadsAnalyzer("/HmmTaggerAggregate.xml");
    writer = new IndexWriter(dir, new IndexWriterConfig(Version.LUCENE_33, analyzer));
}

From source file:com.github.le11.nls.lucene.UIMATypeBasedSimilarityTest.java

License:Apache License

@Before
public void setUp() throws Exception {
    dir = new RAMDirectory();
    analyzer = new UIMAPayloadsAnalyzer("/HmmTaggerAggregate.xml");
    writer = new IndexWriter(dir, new IndexWriterConfig(Version.LUCENE_33, analyzer));

    Document doc = new Document();
    doc.add(new Field("title", "this is a dummy title containing an entity for London", Field.Store.YES,
            Field.Index.ANALYZED));
    doc.add(new Field("contents", "there is some content written here about the british city", Field.Store.YES,
            Field.Index.ANALYZED));
    writer.addDocument(doc, analyzer);/*from   w  ww.ja v a2s. c om*/
    writer.commit();

    // try the search over the first doc
    IndexSearcher indexSearcher = new IndexSearcher(writer.getReader());
    TopDocs result = indexSearcher.search(new MatchAllDocsQuery("contents"), 10);
    assertTrue(result.totalHits > 0);
    Document d = indexSearcher.doc(result.scoreDocs[0].doc);
    assertNotNull(d);
    assertNotNull(d.getFieldable("title"));
    assertNotNull(d.getFieldable("contents"));

    // add a second doc
    doc = new Document();
    doc.add(new Field("title", "some title regarding some article written in English", Field.Store.YES,
            Field.Index.ANALYZED));
    doc.add(new Field("contents", "this is the content of the article about", Field.Store.YES,
            Field.Index.ANALYZED));
    writer.addDocument(doc, analyzer);
    writer.commit();
}