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.pongasoft.kiwidoc.index.impl.lucene.impl.RAMDirectoryFactory.java

License:Apache License

/**
 * Opens an empty ram directory//from ww w.j a va 2s .  c  o m
 * @return
 * @throws IOException
 */
private Directory openEmptyRAMDirectory() throws IOException {
    Directory directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory, null, true, IndexWriter.MaxFieldLength.UNLIMITED);
    writer.close();

    log.info("Opened empty RAM directory");
    return directory;
}

From source file:com.ponysdk.sample.client.page.addon.SelectizeAddon.java

License:Apache License

public SelectizeAddon() {
    super(Element.newInput());
    setTerminalHandler(this);

    ////from  www  .j  a  v a 2  s  . c  o  m
    final Analyzer analyzer = new StandardAnalyzer();
    final Directory directory = new RAMDirectory();

    final IndexWriterConfig config = new IndexWriterConfig(analyzer);
    IndexWriter writer;
    try {
        writer = new IndexWriter(directory, config);
        final Document doc = new Document();
        final String text = "Test de ouf";

        final FieldType fieldType = new FieldType();
        fieldType.setIndexOptions(IndexOptions.NONE);
        fieldType.setStored(true);
        fieldType.setTokenized(false);
        doc.add(new Field("id", "12", fieldType));
        doc.add(new Field("fieldname", text, TextField.TYPE_STORED));

        writer.addDocument(doc);

        addAssetsType(writer);
        addTenor(writer);
        addClients(writer);
        addSide(writer);

        writer.close();
    } catch (final IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        // Now search the index:
        final DirectoryReader ireader = DirectoryReader.open(directory);
        isearcher = new IndexSearcher(ireader);
        // Parse a simple query that searches for "text":
        // final QueryParser parser = new QueryParser("fieldname",
        // analyzer);
        // parser.setFuzzyMinSim(2f);

        final Term term = new Term("fieldname", "indesfed");
        final Query query = new FuzzyQuery(term);
        // final TopDocs hits = isearcher.search(query, 1000).scoreDocs;

        // final Query query = parser.parse("indeed");
        final ScoreDoc[] hits = isearcher.search(query, 1000).scoreDocs;
        // Iterate through the results:
        for (final ScoreDoc hit : hits) {
            System.err.println("Score : " + hit.score);
            final Document hitDoc = isearcher.doc(hit.doc);
            System.err.println("Found document" + hitDoc.getField("fieldname").stringValue());
        }
        // ireader.close();
        // directory.close();
    } catch (final Exception exception) {
        exception.printStackTrace();
    }

    // <input type="text" id="input-tags3" class="demo-default"
    // value="science,biology,chemistry,physics">
}

From source file:com.radialpoint.word2vec.lucene.IndexFiles.java

License:Open Source License

/** Index all text files under a directory. */
@SuppressWarnings("deprecation")
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;/*  w ww .  j  av a 2 s .  c om*/
    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 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);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40, 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.redhat.satellite.search.index.IndexManager.java

License:Open Source License

/**
 * Create an empty index if it exists//  ww w  .ja  v a2 s  .c  om
 *
 * @param indexName index to use
 * @param lang language.
 * @throws IndexingException something went wrong adding the document
 */
public void createIndex(String indexName, String lang) throws IndexingException {

    try {
        IndexWriter writer = getIndexWriter(indexName, lang);
        try {
            writer.flush();
        } finally {
            try {
                writer.close();
            } finally {
                // unlock it if it is locked.
                unlockIndex(indexName);
            }
        }
    } catch (CorruptIndexException e) {
        throw new IndexingException(e);
    } catch (LockObtainFailedException e) {
        throw new IndexingException(e);
    } catch (IOException e) {
        throw new IndexingException(e);
    }
}

From source file:com.redhat.satellite.search.index.IndexManager.java

License:Open Source License

/**
 * Adds a document to an index/* w w w . java 2s. c o m*/
 *
 * @param indexName index to use
 * @param doc Document to be indexed.
 * @param lang language.
 * @throws IndexingException something went wrong adding the document
 */
public void addToIndex(String indexName, Document doc, String lang) throws IndexingException {

    try {
        IndexWriter writer = getIndexWriter(indexName, lang);
        try {
            writer.addDocument(doc);
            writer.flush();
        } finally {
            try {
                writer.close();
            } finally {
                // unlock it if it is locked.
                unlockIndex(indexName);
            }
        }
    } catch (CorruptIndexException e) {
        throw new IndexingException(e);
    } catch (LockObtainFailedException e) {
        throw new IndexingException(e);
    } catch (IOException e) {
        throw new IndexingException(e);
    }
}

From source file:com.redhat.satellite.search.index.ngram.tests.NGramTestSetup.java

License:Open Source License

/**
 * Creates an index in RAM//from  w  w w. j  a v  a 2s .c o  m
 * */
public void setUp() throws Exception {
    super.setUp();
    initItems();
    this.stanDir = new RAMDirectory();
    IndexWriter stanWriter = new IndexWriter(this.stanDir, new StandardAnalyzer(), true);

    this.ngramDir = new RAMDirectory();
    IndexWriter ngramWriter = new IndexWriter(this.ngramDir, new NGramAnalyzer(min_ngram, max_ngram), true);

    for (Map<String, String> item : items) {
        String name = item.get("name");
        String descp = item.get("description");
        Document doc = new Document();
        doc.add(new Field("name", name, Field.Store.YES, Field.Index.TOKENIZED));
        doc.add(new Field("description", descp, Field.Store.YES, Field.Index.TOKENIZED));
        stanWriter.addDocument(doc);
        ngramWriter.addDocument(doc);
    }
    stanWriter.close();
    ngramWriter.close();
}

From source file:com.redsqirl.SimpleFileIndexer.java

License:Open Source License

public int index(File indexDir, File dataDir, String suffix) throws Exception {

    IndexWriter indexWriter = new IndexWriter(FSDirectory.open(indexDir), new SimpleAnalyzer(), true,
            IndexWriter.MaxFieldLength.LIMITED);
    indexWriter.setUseCompoundFile(false);

    indexDirectory(indexWriter, dataDir, suffix);

    int numIndexed = indexWriter.maxDoc();
    indexWriter.optimize();//from ww w  .j  a v a  2 s. c om
    indexWriter.close();

    return numIndexed;

}

From source file:com.redsqirl.SimpleFileIndexer.java

License:Open Source License

public void merge(String indexPath, String pathA, String pathB)
        throws CorruptIndexException, LockObtainFailedException, IOException {
    File dir = new File(indexPath);
    SimpleFSDirectory d = new SimpleFSDirectory(dir);
    IndexWriter writer = new IndexWriter(d, new StandardAnalyzer(Version.LUCENE_CURRENT),
            IndexWriter.MaxFieldLength.LIMITED);

    //File INDEXES_DIR = new File(pathA);

    Directory indexes[] = new Directory[2];
    indexes[0] = FSDirectory.open(new File(pathA));
    indexes[1] = FSDirectory.open(new File(pathB));

    /*/*from   w w w .ja  va  2 s  . co  m*/
    for (int i = 0; i < INDEXES_DIR.list().length; i++) {
       System.out.println("Adding: " + INDEXES_DIR.list()[i]);
       File fil = new File(pathB+"/"+INDEXES_DIR.list()[i]);
    }
    indexes[i] = FSDirectory.open(pathB);
     */

    logger.info(" Merging added indexes ");
    writer.addIndexesNoOptimize(indexes);
    logger.info(" Optimizing index ");

    indexes[0].close();
    indexes[1].close();

    writer.optimize();
    writer.commit();
    writer.close();
}

From source file:com.revorg.goat.IndexManager.java

License:Open Source License

/**
 * Create a Lucene Index.//from   w  ww.j ava  2  s. com
 *
 * @param indexPath Directory that contains the Lucene Collection
 * @throws Exception
 * @return ActionResult 
 */
public static String createIndex(String indexPath) {
    File theDir = new File(indexPath);
    // if the directory does not exist, create it

    try {

        //if the directory exist, do not create it
        if (theDir.exists()) {
            System.out.println("Failure to create index: " + indexPath);
            System.out.println("The index/directory already exists:");
            ActionResult = "Failure";
            return ActionResult + ActionResultError;
        }

        //StandardAnalyzer new StandardAnalyzer() = new StandardAnalyzer();    //Initialize Class
        IndexWriter writer = new IndexWriter(indexPath, new StandardAnalyzer(), true,
                IndexWriter.MaxFieldLength.LIMITED);
        writer.close();
        ActionResult = "Success";
        return ActionResult;
    } catch (Exception e) {
        ActionResultError = " caught a " + e.getClass() + " with message: " + e.getMessage();
        System.out.println("Failure to create index: " + indexPath);
    }
    ActionResult = "Failure";
    return ActionResult + ActionResultError;
}

From source file:com.revorg.goat.IndexManager.java

License:Open Source License

/**
 * Optimizes the structure and contents of the collection for searching; recovers space. Causes collection to be taken offline, preventing searches and indexing.
 *
 * @param indexPath Directory that contains the Lucene Collection
 * @throws Exception// w w  w .j av  a  2s  .  c  o m
 * @return ActionResult
 */
public static String optimizeIndex(String indexPath) {
    try {
        //StandardAnalyzer new StandardAnalyzer() = new StandardAnalyzer();    //Initialize Class
        IndexWriter writer = new IndexWriter(indexPath, new StandardAnalyzer(), false,
                IndexWriter.MaxFieldLength.LIMITED);
        writer.optimize();
        writer.close();
        ActionResult = "Success";
        return ActionResult;
    } catch (Exception e) {
        ActionResultError = " caught a " + e.getClass() + " with message: " + e.getMessage();
        System.out.println("Failure to optimize index: " + indexPath);
    }
    ActionResult = "Failure";
    return ActionResult + ActionResultError;
}