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

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

Introduction

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

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes all open resources and releases the write lock.

Usage

From source file:com.m3958.apps.pcms.lucene.facet.MultiCategoryListsFacetsExample.java

License:Apache License

/** Build the example index. */
private void index() throws IOException {
    IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER,
            new WhitespaceAnalyzer(FacetExamples.EXAMPLES_VER)));

    // Writes facet ords to a separate directory from the main index
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);

    // Reused across documents, to add the necessary facet fields
    FacetFields facetFields = new FacetFields(taxoWriter, indexingParams);

    add(indexWriter, facetFields, "Author/Bob", "Publish Date/2010/10/15");
    add(indexWriter, facetFields, "Author/Lisa", "Publish Date/2010/10/20");
    add(indexWriter, facetFields, "Author/Lisa", "Publish Date/2012/1/1");
    add(indexWriter, facetFields, "Author/Susan", "Publish Date/2012/1/7");
    add(indexWriter, facetFields, "Author/Frank", "Publish Date/1999/5/5");

    indexWriter.close();
    taxoWriter.close();/*from w w  w  . j  av  a2  s.co  m*/
}

From source file:com.m3958.apps.pcms.lucene.facet.SimpleFacetsExample.java

License:Apache License

/** Build the example index. */
private void index() throws IOException {
    IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER,
            new WhitespaceAnalyzer(FacetExamples.EXAMPLES_VER)));

    // Writes facet ords to a separate directory from the main index
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);

    // Reused across documents, to add the necessary facet fields
    FacetFields facetFields = new FacetFields(taxoWriter);

    add(indexWriter, facetFields, "Author/Bob", "Publish Date/2010/10/15");
    add(indexWriter, facetFields, "Author/Lisa", "Publish Date/2010/10/20");
    add(indexWriter, facetFields, "Author/Lisa", "Publish Date/2012/1/1");
    add(indexWriter, facetFields, "Author/Susan", "Publish Date/2012/1/7");
    add(indexWriter, facetFields, "Author/Frank", "Publish Date/1999/5/5");

    indexWriter.close();
    taxoWriter.close();/*  w w  w  .  j a va 2  s .  c om*/
}

From source file:com.m3958.apps.pcms.lucene.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 = "c:/lucene/index";
    String docsPath = "c:/lucene/docs";
    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;
    //      }/*from w w  w  .  j  av a 2  s .  co  m*/
    //    }

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

    final File docDir = new File(docsPath);
    if (!docDir.exists() || !docDir.canRead()) {
        System.out.println("Document directory '" + docDir.getAbsolutePath()
                + "' does not exist or is not readable, please check the path");
        System.exit(1);
    }

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

        Directory dir = FSDirectory.open(new File(indexPath));
        //      Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);

        Analyzer analyzer = new ComplexAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_45, analyzer);

        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);

        indexCusValues(writer);
        //      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:com.main.Indexer.java

public void indexing() throws TikaException, SAXException {
    //Input folder
    String docsPath = "C:\\Users\\piyush\\Documents\\NetBeansProjects\\luceneFinal\\indexing\\doc";
    //Output folder
    String indexPath = "C:\\Users\\piyush\\Documents\\NetBeansProjects\\luceneFinal\\indexing\\index";
    //Input Path Variable
    final Path docDir = Paths.get(docsPath);
    try {//from ww w.  j  a  va 2  s  .  com
        //org.apache.lucene.store.Directory instance
        Directory dir = FSDirectory.open(Paths.get(indexPath));
        //analyzer with the default stop words
        Analyzer analyzer = new StandardAnalyzer();
        //IndexWriter Configuration
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        //IndexWriter writes new index files to the directory
        IndexWriter writer = new IndexWriter(dir, iwc);
        //Its recursive method to iterate all files and directories
        indexDocs(writer, docDir);
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.mathworks.xzheng.advsearching.MultiPhraseQueryTest.java

License:Apache License

protected void setUp() throws Exception {
    Directory directory = new RAMDirectory();

    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46,
            new WhitespaceAnalyzer(Version.LUCENE_46));
    IndexWriter writer = new IndexWriter(directory, config);
    Document doc1 = new Document();
    doc1.add(new Field("field", "the quick brown fox jumped over the lazy dog", Field.Store.YES,
            Field.Index.ANALYZED));
    writer.addDocument(doc1);/*  www  .j a  va 2  s.  c  o m*/
    Document doc2 = new Document();
    doc2.add(new Field("field", "the fast fox hopped over the hound", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc2);
    writer.close();

    searcher = new IndexSearcher(DirectoryReader.open(directory));
}

From source file:com.mathworks.xzheng.advsearching.MultiSearcherTest.java

License:Apache License

public void setUp() throws Exception {
    String[] animals = { "aardvark", "beaver", "coati", "dog", "elephant", "frog", "gila monster", "horse",
            "iguana", "javelina", "kangaroo", "lemur", "moose", "nematode", "orca", "python", "quokka", "rat",
            "scorpion", "tarantula", "uromastyx", "vicuna", "walrus", "xiphias", "yak", "zebra" };

    Analyzer analyzer = new WhitespaceAnalyzer(Version.LUCENE_46);

    Directory aTOmDirectory = new RAMDirectory(); // #1
    Directory nTOzDirectory = new RAMDirectory(); // #1

    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer);

    IndexWriter aTOmWriter = new IndexWriter(aTOmDirectory, config);
    IndexWriter nTOzWriter = new IndexWriter(nTOzDirectory, config);

    for (int i = animals.length - 1; i >= 0; i--) {
        Document doc = new Document();
        String animal = animals[i];
        doc.add(new Field("animal", animal, Field.Store.YES, Field.Index.NOT_ANALYZED));
        if (animal.charAt(0) < 'n') {
            aTOmWriter.addDocument(doc); // #2
        } else {/*w ww.j a  va2s .c o m*/
            nTOzWriter.addDocument(doc); // #2
        }
    }

    aTOmWriter.close();
    nTOzWriter.close();

    searchers = new IndexSearcher[2];
    searchers[0] = new IndexSearcher(DirectoryReader.open(aTOmDirectory));
    searchers[1] = new IndexSearcher(DirectoryReader.open(nTOzDirectory));
}

From source file:com.mathworks.xzheng.advsearching.SecurityFilterTest.java

License:Apache License

protected void setUp() throws Exception {
    Directory directory = new RAMDirectory();

    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46,
            new WhitespaceAnalyzer(Version.LUCENE_46));
    IndexWriter writer = new IndexWriter(directory, config);

    Document document = new Document(); // 1
    document.add(new Field("owner", // 1
            "elwood", // 1
            Field.Store.YES, // 1
            Field.Index.NOT_ANALYZED)); // 1
    document.add(new Field("keywords", // 1
            "elwood's sensitive info", // 1
            Field.Store.YES, // 1
            Field.Index.ANALYZED)); // 1
    writer.addDocument(document);/*www .j a  v a 2s .  c o  m*/

    document = new Document(); // 2
    document.add(new Field("owner", // 2
            "jake", // 2
            Field.Store.YES, // 2
            Field.Index.NOT_ANALYZED)); // 2
    document.add(new Field("keywords", // 2
            "jake's sensitive info", // 2
            Field.Store.YES, // 2
            Field.Index.ANALYZED)); // 2
    writer.addDocument(document);

    writer.close();
    searcher = new IndexSearcher(DirectoryReader.open(directory));
}

From source file:com.mathworks.xzheng.advsearching.SpanQueryTest.java

License:Apache License

protected void setUp() throws Exception {
    directory = new RAMDirectory();

    analyzer = new WhitespaceAnalyzer(Version.LUCENE_46);

    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer);

    IndexWriter writer = new IndexWriter(directory, config);

    Document doc = new Document();
    doc.add(new Field("f", "the quick brown fox jumps over the lazy dog", Field.Store.YES,
            Field.Index.ANALYZED));
    writer.addDocument(doc);//ww  w. ja v  a  2s. c  o m

    doc = new Document();
    doc.add(new Field("f", "the quick red fox jumps over the sleepy cat", Field.Store.YES,
            Field.Index.ANALYZED));
    writer.addDocument(doc);

    writer.close();

    searcher = new IndexSearcher(DirectoryReader.open(directory));
    reader = searcher.getIndexReader();

    quick = new SpanTermQuery(new Term("f", "quick"));
    brown = new SpanTermQuery(new Term("f", "brown"));
    red = new SpanTermQuery(new Term("f", "red"));
    fox = new SpanTermQuery(new Term("f", "fox"));
    lazy = new SpanTermQuery(new Term("f", "lazy"));
    sleepy = new SpanTermQuery(new Term("f", "sleepy"));
    dog = new SpanTermQuery(new Term("f", "dog"));
    cat = new SpanTermQuery(new Term("f", "cat"));
}

From source file:com.mathworks.xzheng.analysis.codec.MetaphoneAnalyzerTest.java

License:Apache License

public void testKoolKat() throws Exception {
    RAMDirectory directory = new RAMDirectory();
    Analyzer analyzer = new MetaphoneReplacementAnalyzer();

    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer);
    config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);

    IndexWriter writer = new IndexWriter(directory, config);

    Document doc = new Document();
    doc.add(new Field("contents", //#A
            "cool cat", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc);/*from   ww  w.  j  a  v a 2  s.  co m*/
    writer.close();

    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));

    Query query = new QueryParser(Version.LUCENE_46, //#B
            "contents", analyzer) //#B
                    .parse("kool kat"); //#B

    TopDocs hits = searcher.search(query, 1);
    assertEquals(1, hits.totalHits); //#C
    int docID = hits.scoreDocs[0].doc;
    doc = searcher.doc(docID);
    assertEquals("cool cat", doc.get("contents")); //#D

}

From source file:com.mathworks.xzheng.analysis.keyword.KeywordAnalyzerTest.java

License:Apache License

public void setUp() throws Exception {
    Directory directory = new RAMDirectory();

    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, new SimpleAnalyzer(Version.LUCENE_46));

    IndexWriter writer = new IndexWriter(directory, config);

    Document doc = new Document();
    doc.add(new Field("partnum", "Q36", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS)); //A
    doc.add(new Field("description", "Illidium Space Modulator", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc);/*  w ww  .j a  v  a2s .com*/

    writer.close();

    searcher = new IndexSearcher(DirectoryReader.open(directory));
}