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.fuerve.villageelder.search.SearcherTest.java

License:Apache License

/**
 * Test method for {@link com.fuerve.villageelder.search.Searcher#initializeSearch()}.
 *///from ww  w.j  a v  a  2s. c o  m
@SuppressWarnings("unused")
@Test
public final void testInitializeSearch() throws Exception {
    // Gather declared fields.
    Field indexDirectoryField = Searcher.class.getDeclaredField("indexDirectory");
    Field taxonomyDirectoryField = Searcher.class.getDeclaredField("taxonomyDirectory");
    Field indexDirectoryNameField = Searcher.class.getDeclaredField("indexDirectoryName");
    Field taxonomyDirectoryNameField = Searcher.class.getDeclaredField("taxonomyDirectoryName");
    Field stringDirectoriesField = Searcher.class.getDeclaredField("stringDirectories");
    Field initializedField = Searcher.class.getDeclaredField("initialized");
    Field searchField = Searcher.class.getDeclaredField("search");
    Field indexReaderField = Searcher.class.getDeclaredField("indexReader");
    Field indexSearcherField = Searcher.class.getDeclaredField("indexSearcher");
    Field taxonomyReaderField = Searcher.class.getDeclaredField("taxonomyReader");

    indexDirectoryField.setAccessible(true);
    taxonomyDirectoryField.setAccessible(true);
    indexDirectoryNameField.setAccessible(true);
    taxonomyDirectoryNameField.setAccessible(true);
    stringDirectoriesField.setAccessible(true);
    initializedField.setAccessible(true);
    searchField.setAccessible(true);
    indexReaderField.setAccessible(true);
    indexSearcherField.setAccessible(true);
    taxonomyReaderField.setAccessible(true);

    // Setup
    Directory indexDirectoryExpected = new RAMDirectory();
    Directory taxonomyDirectoryExpected = new RAMDirectory();

    IndexWriterConfig iwc = new IndexWriterConfig(Lucene.LUCENE_VERSION, Lucene.getPerFieldAnalyzer());
    IndexWriter iw = new IndexWriter(indexDirectoryExpected, iwc);
    TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxonomyDirectoryExpected, OpenMode.CREATE);

    iw.commit();
    tw.commit();

    Searcher target = new Searcher(indexDirectoryExpected, taxonomyDirectoryExpected);
    target.initializeSearch();

    // Gather field values.
    Directory indexDirectoryActual = (Directory) indexDirectoryField.get(target);
    Directory taxonomyDirectoryActual = (Directory) taxonomyDirectoryField.get(target);
    String indexDirectoryNameActual = (String) indexDirectoryNameField.get(target);
    String taxonomyDirectoryNameActual = (String) taxonomyDirectoryNameField.get(target);
    boolean stringDirectoriesActual = stringDirectoriesField.getBoolean(target);
    boolean initializedActual = initializedField.getBoolean(target);
    Search searchFieldActual = (Search) searchField.get(target);
    IndexReader indexReaderActual = (IndexReader) indexReaderField.get(target);
    IndexSearcher indexSearcherActual = (IndexSearcher) indexSearcherField.get(target);
    TaxonomyReader taxonomyReaderActual = (TaxonomyReader) taxonomyReaderField.get(target);

    // Test
    assertEquals(true, initializedActual);
    assertNotNull(indexReaderActual);
    assertNotNull(indexSearcherActual);
    assertNotNull(taxonomyReaderActual);

    // Finish
    tw.close();
    iw.close();
}

From source file:com.fuerve.villageelder.search.SearchQueryParserTest.java

License:Apache License

private IndexReader buildDummyIndex() throws IOException {
    RAMDirectory indexDirectory = new RAMDirectory();

    IndexWriterConfig iwc = new IndexWriterConfig(Lucene.LUCENE_VERSION, Lucene.getPerFieldAnalyzer());
    iwc.setOpenMode(OpenMode.CREATE);//from  w w w.j  a  v a  2 s  . c o  m
    IndexWriter iw = new IndexWriter(indexDirectory, iwc);

    Document doc = new Document();
    doc.add(new StringField("Author", "foo", Field.Store.YES));
    doc.add(new LongField("RevisionNumber", 50L, Field.Store.YES));
    doc.add(new StringField("Revision", "50", Field.Store.YES));
    doc.add(new TextField("Message", "stuff", Field.Store.YES));
    iw.addDocument(doc);

    doc = new Document();
    doc.add(new StringField("Author", "bar", Field.Store.YES));
    doc.add(new LongField("RevisionNumber", 5000L, Field.Store.YES));
    doc.add(new StringField("Revision", "5000", Field.Store.YES));
    doc.add(new TextField("Message", "stuff", Field.Store.YES));
    iw.addDocument(doc);
    iw.commit();
    iw.close();

    DirectoryReader result = DirectoryReader.open(indexDirectory);
    return result;
}

From source file:com.fuerve.villageelder.search.SearchTest.java

License:Apache License

/**
 * Test method for {@link com.fuerve.villageelder.search.Search#getFacetsCollector(org.apache.lucene.index.DirectoryReader, org.apache.lucene.facet.taxonomy.TaxonomyReader)}.
 *//*from   w w  w  .ja  va  2s.  c o  m*/
@Test
@SuppressWarnings({ "unchecked", "unused" })
public final void testGetFacetsCollector() throws Exception {
    // Constants
    Field defaultSortField = Search.class.getDeclaredField("DEFAULT_SORT");
    Field defaultFacetsField = Search.class.getDeclaredField("DEFAULT_FACETS");
    Field defaultFacetStringsField = Search.class.getDeclaredField("DEFAULT_FACET_STRINGS");
    Field defaultAnalyzerField = Search.class.getDeclaredField("DEFAULT_ANALYZER");
    Field defaultHitsField = Search.class.getDeclaredField("DEFAULT_HITS");

    defaultSortField.setAccessible(true);
    defaultFacetsField.setAccessible(true);
    defaultFacetStringsField.setAccessible(true);
    defaultAnalyzerField.setAccessible(true);
    defaultHitsField.setAccessible(true);

    final Sort defaultSort = (Sort) defaultSortField.get(null);
    final List<FacetRequest> defaultFacets = (List<FacetRequest>) defaultFacetsField.get(null);
    final Map<String, Integer> defaultFacetStrings = (Map<String, Integer>) defaultFacetStringsField.get(null);
    final Analyzer defaultAnalyzer = (Analyzer) defaultAnalyzerField.get(null);
    final int defaultHits = defaultHitsField.getInt(null);

    // Private members
    Field queryField = Search.class.getDeclaredField("query");
    Field sortField = Search.class.getDeclaredField("sort");
    Field facetsField = Search.class.getDeclaredField("facets");

    queryField.setAccessible(true);
    sortField.setAccessible(true);
    facetsField.setAccessible(true);

    // Test setup
    QueryParser parser = getQueryParser();
    Query queryExpected = parser.parse("test:foo");
    List<FacetRequest> facetsExpected = new ArrayList<FacetRequest>();
    Sort sortExpected = Sort.RELEVANCE;

    Search target = new Search(queryExpected, facetsExpected, sortExpected);
    target.addFacet("test", 100);

    // Gather fields
    Query queryActual = (Query) queryField.get(target);
    Sort sortActual = (Sort) sortField.get(target);
    List<FacetRequest> facetsActual = (List<FacetRequest>) facetsField.get(target);

    // Set up some dummy indices.
    Directory indexDirectory = new RAMDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(Lucene.LUCENE_VERSION, Lucene.getPerFieldAnalyzer());
    IndexWriter iw = new IndexWriter(indexDirectory, iwc);
    Directory taxonomyDirectory = new RAMDirectory();
    TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxonomyDirectory, OpenMode.CREATE);

    iw.commit();
    tw.commit();

    // Test
    FacetsCollector actual = target.getFacetsCollector(DirectoryReader.open(indexDirectory),
            new DirectoryTaxonomyReader(taxonomyDirectory));

    assertEquals("DocsOnlyCollector", actual.getClass().getSimpleName());
    iw.close();
    tw.close();
    taxonomyDirectory.close();
}

From source file:com.fun.sb.demo.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 = "/Users/baidu/temp/index/";
    String docsPath = "/Users/baidu/temp/";
    boolean create = true;
    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            indexPath = args[i + 1];/*from  www . j  a va  2 s  .c om*/
            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 readable, 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:com.gauronit.tagmata.core.Indexer.java

License:Open Source License

private synchronized void loadIndexes() throws Exception {
    try {//from  www  . j  a  v a 2 s.  com
        // Properties props = new Properties();
        // props.load(new FileInputStream(new File("indexer.properties")));
        indexDir = new File(".").getCanonicalPath() + File.separator + "indexes"; // props.getProperty("indexDir");

        // Open or create main index --
        try {
            IndexWriter iw = new IndexWriter(FSDirectory.open(new File(indexDir + File.separator + MAIN_INDEX)),
                    new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
            iw.close();
            iw = null;

        } catch (Exception ex) {
            try {
                IndexWriter iw = new IndexWriter(
                        FSDirectory.open(new File(indexDir + File.separator + MAIN_INDEX)),
                        new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
                iw.close();
                iw = null;
            } catch (Exception e) {
                logger.log(Level.SEVERE, "Failed creating main index", e);
                System.exit(0);
            }
        }
        // --
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Failed to load indexes", ex);
        throw ex;
    }
    loaded = true;
}

From source file:com.gauronit.tagmata.core.Indexer.java

License:Open Source License

public String createIndex(String indexDisplayName) {
    try {//from   ww w. jav a2s  . c om
        UUID uuid = UUID.randomUUID();
        String indexName = uuid.toString().substring(uuid.toString().length() - 8, uuid.toString().length());
        IndexWriter iw = new IndexWriter(FSDirectory.open(new File(indexDir + File.separator + indexName)),
                new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
        iw.prepareCommit();
        iw.commit();
        iw.close();
        iw = null;

        Document doc = new Document();
        doc.add(new Field("displayName", indexDisplayName, Store.YES, Index.NOT_ANALYZED));
        doc.add(new Field("indexName", indexName, Store.YES, Index.NOT_ANALYZED));

        IndexWriter mainIndexWriter = new IndexWriter(
                FSDirectory.open(new File(indexDir + File.separator + MAIN_INDEX)),
                new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
        mainIndexWriter.addDocument(doc);
        mainIndexWriter.commit();
        mainIndexWriter.close();
        mainIndexWriter = null;
        return indexName;
    } catch (IOException ex) {
        Logger.getLogger(Indexer.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:com.gauronit.tagmata.core.Indexer.java

License:Open Source License

public void deleteIndex(String indexName) {
    try {/*from  www . ja v  a  2  s  . c om*/
        IndexWriter mainIndexWriter = new IndexWriter(
                FSDirectory.open(new File(indexDir + File.separator + MAIN_INDEX)),
                new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
        Query q = new QueryParser(Version.LUCENE_35, "indexName", new StandardAnalyzer(Version.LUCENE_35))
                .parse(indexName);
        mainIndexWriter.deleteDocuments(q);
        mainIndexWriter.commit();
        mainIndexWriter.close();
        mainIndexWriter = null;

        IndexWriter writer = new IndexWriter(FSDirectory.open(new File(indexDir + File.separator + indexName)),
                new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
        writer.deleteAll();
        writer.prepareCommit();
        writer.commit();
        writer.close();
        writer = null;

        IOUtil.deleteDir(new File(indexDir + File.separator + indexName));
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
    }
}

From source file:com.gauronit.tagmata.core.Indexer.java

License:Open Source License

public void renameIndex(String indexName, String indexDisplayName) {
    try {/*from ww w .j  a v  a2 s . c o  m*/
        IndexWriter mainIndexWriter = new IndexWriter(
                FSDirectory.open(new File(indexDir + File.separator + MAIN_INDEX)),
                new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
        Document doc = new Document();
        doc.add(new Field("displayName", indexDisplayName, Store.YES, Index.NOT_ANALYZED));
        doc.add(new Field("indexName", indexName, Store.YES, Index.NOT_ANALYZED));

        mainIndexWriter.updateDocument(new Term("indexName", indexName), doc,
                new StandardAnalyzer(Version.LUCENE_35));
        mainIndexWriter.prepareCommit();
        mainIndexWriter.commit();

        mainIndexWriter.close();
        mainIndexWriter = null;
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:com.gauronit.tagmata.core.Indexer.java

License:Open Source License

public void deleteCards(ArrayList<CardSnapshot> cardSnaps) {
    try {/* w  w w  .  j  a  v a  2s.  c  o  m*/
        for (CardSnapshot cardSnap : cardSnaps) {
            IndexWriter writer = new IndexWriter(
                    FSDirectory.open(new File(indexDir + File.separator + cardSnap.getIndexName())),
                    new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
            writer.deleteDocuments(new Term("id", cardSnap.getId()));
            writer.prepareCommit();
            writer.commit();
            writer.close();
            writer = null;
        }
    } catch (Exception ex) {
    }
}

From source file:com.gauronit.tagmata.core.Indexer.java

License:Open Source License

public void saveCard(String title, String tags, String text, String indexName) {
    try {/*from   w  w w . j a  v a 2  s .c om*/
        IndexWriter writer = new IndexWriter(FSDirectory.open(new File(indexDir + File.separator + indexName)),
                new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));

        Document doc = new Document();
        doc.add(new Field("title", title, Store.YES, Index.ANALYZED));
        doc.add(new Field("tags", tags, Store.YES, Index.ANALYZED));
        doc.add(new Field("text", text, Store.YES, Index.ANALYZED));
        doc.add(new Field("analyzedText", text, Store.YES, Index.ANALYZED));
        doc.add(new Field("indexName", indexName, Store.YES, Index.ANALYZED));
        doc.add(new Field("id", UUID.randomUUID().toString(), Store.YES, Index.NOT_ANALYZED));
        writer.addDocument(doc, new StandardAnalyzer(Version.LUCENE_35));
        writer.prepareCommit();
        writer.commit();
        writer.close();
        writer = null;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}