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

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

Introduction

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

Prototype

@SuppressWarnings("try")
public long deleteAll() throws IOException 

Source Link

Document

Delete all documents in the index.

Usage

From source file:com.jaeksoft.searchlib.index.WriterLucene.java

License:Open Source License

private void deleteAllNoLock() throws SearchLibException {
    IndexWriter indexWriter = null;
    try {//from  w  w  w .  j  av  a  2s . com
        indexWriter = open();
        indexWriter.deleteAll();
        close(indexWriter);
        indexWriter = null;
        indexLucene.reloadNoLock();
    } catch (CorruptIndexException e) {
        throw new SearchLibException(e);
    } catch (LockObtainFailedException e) {
        throw new SearchLibException(e);
    } catch (IOException e) {
        throw new SearchLibException(e);
    } finally {
        close(indexWriter);
    }
}

From source file:com.meltmedia.cadmium.search.SearchContentPreprocessor.java

License:Apache License

@Override
public synchronized void processFromDirectory(String metaDir) throws Exception {
    SearchHolder newStagedSearcher = new SearchHolder();
    indexDir = new File(metaDir, "lucene-index");
    dataDir = new File(metaDir).getParentFile();
    newStagedSearcher.directory = new NIOFSDirectory(indexDir);
    IndexWriter iwriter = null;
    try {/*from  ww  w.ja  va2 s  .com*/
        iwriter = new IndexWriter(newStagedSearcher.directory,
                new IndexWriterConfig(Version.LUCENE_43, analyzer).setRAMBufferSizeMB(5));
        iwriter.deleteAll();
        writeIndex(iwriter, dataDir);
    } finally {
        IOUtils.closeQuietly(iwriter);
        iwriter = null;
    }
    newStagedSearcher.indexReader = DirectoryReader.open(newStagedSearcher.directory);
    SearchHolder oldStage = stagedSearch;
    stagedSearch = newStagedSearcher;
    if (oldStage != null) {
        oldStage.close();
    }
    log.info("About to call processSearchPreprocessors()");
    processSearchPreprocessors(newStagedSearcher.indexReader, analyzer, "content");
}

From source file:com.nuvolect.deepdive.lucene.Index.java

public static JSONObject index(final String volumeId, final String searchPath, final boolean forceIndex) {

    if (m_interrupt[0]) {

        LogUtil.log(LogUtil.LogType.INDEX, "Index canceled post interrupt");

        m_interrupt[0] = false;/*from w w  w.j  a  va2s.c o m*/
        return responseInterruptIndexing();
    }

    OmniFile cacheDir = IndexUtil.getCacheDir(volumeId, searchPath);
    boolean cacheDirCreated = false;
    try {
        cacheDirCreated = OmniUtil.forceMkdir(cacheDir);
    } catch (IOException e) {
        return responseFolderCreateError(searchPath);
    }

    final String luceneDirPath = cacheDir.getAbsolutePath();

    boolean cacheDirExists = !cacheDirCreated;
    boolean indexingOngoing = m_indexThread != null && m_indexThread.isAlive();
    boolean indexingRequired = !cacheDirExists || forceIndex;

    synchronized (m_lock) {

        if (indexingOngoing) {

            if (m_fileTreeActive)
                m_index_state = INDEX_STATE.filetree;
            else
                m_index_state = INDEX_STATE.indexing;
        } else {
            if (indexingRequired)
                m_index_state = INDEX_STATE.indexing;
            else
                m_index_state = INDEX_STATE.complete;
        }
    }

    if (indexingRequired || indexingOngoing) {

        if (indexingOngoing) {

            // Nothing to do, let the background process run. Monitor m_indexedDocs for progress.
        } else {

            synchronized (m_lock) {
                m_index_state = INDEX_STATE.filetree;
                m_totalDocs[0] = 0;
                m_indexedDocs[0] = 0;
                m_error[0] = "";
            }
            m_threadGroup = new ThreadGroup(INDEX_THREAD_GROUP);
            m_indexThread = new Thread(m_threadGroup, new Runnable() {
                @Override
                public void run() {

                    //                        Analyzer analyzer = new org.apache.lucene.analysis.core.WhitespaceAnalyzer();
                    //                        Analyzer analyzer = new org.apache.lucene.analysis.core.KeywordAnalyzer();
                    //                        Analyzer analyzer = new org.apache.lucene.analysis.standard.StandardAnalyzer();
                    Analyzer analyzer = new org.apache.lucene.analysis.core.SimpleAnalyzer();
                    IndexWriterConfig config = new IndexWriterConfig(analyzer);
                    IndexWriter iwriter = null;

                    try {
                        Directory m_directory = FSDirectory.open(Paths.get(luceneDirPath));
                        iwriter = new IndexWriter(m_directory, config);
                        iwriter.deleteAll();
                        iwriter.commit();
                    } catch (IOException e) {
                        LogUtil.logException(LogUtil.LogType.INDEX, e);
                        m_error[0] = "IndexWriter constructor exception";
                    }

                    synchronized (m_lock) {
                        m_fileTreeActive = true;
                        m_index_state = INDEX_STATE.filetree;
                    }
                    Collection<OmniFile> files = IndexUtil.getFilePaths(volumeId, searchPath);

                    synchronized (m_lock) {
                        m_index_state = INDEX_STATE.indexing;
                        m_fileTreeActive = false;
                        m_totalDocs[0] = files.size();
                        m_indexedDocs[0] = 0;
                    }

                    try {

                        for (OmniFile file : files) {

                            if (m_interrupt[0]) {
                                LogUtil.log(LogUtil.LogType.INDEX, "Iterator loop canceled");
                                break;
                            }

                            String path = file.getPath();

                            //                                LogUtil.log(LogUtil.LogType.INDEX, "indexing: " + path);// this is a bit excessive
                            iwriter.addDocument(makeDoc(volumeId, path));
                            synchronized (m_lock) {
                                ++m_indexedDocs[0];
                            }
                        }

                        iwriter.commit();
                        iwriter.close();
                        synchronized (m_lock) {
                            m_index_state = m_interrupt[0] ? INDEX_STATE.interrupted : INDEX_STATE.complete;
                            m_totalDocs[0] = m_indexedDocs[0];
                        }

                    } catch (Exception e) {
                        LogUtil.logException(LogUtil.LogType.INDEX, e);
                        m_error[0] = "IndexWriter addDocument exception";
                    }
                }
            }, INDEX_THREAD, STACK_SIZE);

            m_indexThread.setPriority(Thread.MAX_PRIORITY);
            m_indexThread.start();
        }
    } else {

        // Indexing is complete
        // Get number of documents indexed
        try {
            Directory directory = FSDirectory.open(Paths.get(luceneDirPath));
            DirectoryReader ireader = DirectoryReader.open(directory);
            synchronized (m_lock) {
                m_indexedDocs[0] = ireader.numDocs();
                m_totalDocs[0] = m_indexedDocs[0];
                m_index_state = INDEX_STATE.complete;
            }
            ireader.close();
            directory.close();
        } catch (IOException e) {
            LogUtil.logException(LogUtil.LogType.INDEX, e);
        }
    }

    JSONObject result = new JSONObject();
    try {
        synchronized (m_lock) {
            result.put("index_state", m_index_state.toString());
            result.put("error", m_error[0]);
            result.put("indexed_docs", m_indexedDocs[0]);
            result.put("total_docs", m_totalDocs[0]);
            //                result.put("full_path", cacheDir.getAbsolutePath());
            result.put("search_path", searchPath);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:com.senseidb.search.node.inmemory.InMemorySenseiService.java

License:Apache License

private void addDocuments(Directory directory, IndexWriter writer, List<JSONObject> documents) {
    try {/*from   w  ww  . j a  v a  2s .com*/
        writer.deleteAll();
        for (JSONObject doc : documents) {
            if (doc == null)
                continue;
            writer.addDocument(buildDoc(doc));
            pluggableSearchEngineManager.update(doc, "");
        }
        writer.commit();

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

From source file:com.sxc.lucene.index.IndexingTest.java

License:Apache License

protected void setUp() throws Exception { // 1
    directory = FSDirectory.open(new File("D:/programming/lucene/indexingTest"));

    IndexWriter writer = getWriter(); // 2
    writer.deleteAll();

    for (int i = 0; i < ids.length; i++) { // 3
        Document doc = new Document();
        doc.add(new StringField("id", ids[i], Field.Store.YES));
        doc.add(new StringField("country", unindexed[i], Field.Store.YES));
        doc.add(new TextField("contents", unstored[i], Field.Store.NO));
        doc.add(new TextField("city", text[i], Field.Store.YES));

        writer.addDocument(doc);//from  w  ww .j ava  2  s . c o m
    }
    writer.close();
}

From source file:de.uni_koeln.spinfo.maalr.lucene.core.DictionaryCreator.java

License:Apache License

void dropIndex() throws IndexException {
    IndexWriter writer = null;
    try {/*from   w w  w .j a va 2  s.  c  o m*/
        writer = initIndexWriter();
        writer.deleteAll();
    } catch (IOException e) {
        throw new IndexException(e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                // Ignore
            }
        }
    }
}

From source file:demo.jaxrs.search.server.Catalog.java

License:Apache License

@DELETE
public Response delete() throws IOException {
    final IndexWriter writer = getIndexWriter();

    try {/*from   w  ww.ja  v a 2s  .co  m*/
        storage.deleteAll();
        writer.deleteAll();
        writer.commit();
    } finally {
        writer.close();
    }

    return Response.ok().build();
}

From source file:dk.dma.msinm.lucene.AbstractLuceneIndex.java

License:Open Source License

/**
 * Deletes the current index/*from  w w  w .ja v  a  2s  .  c om*/
 * @throws IOException
 */
public void deleteIndex() throws IOException {
    // Delete the index
    IndexWriter writer = null;
    try {
        writer = getNewWriter();
        writer.deleteAll();
        writer.setCommitData(new HashMap<>());
        writer.commit();
    } finally {
        closeWriter(writer);
    }
}

From source file:edu.cmu.geolocator.resource.gazindexing.CollaborativeIndex.GazInfoIndexerAllCountries.java

License:Apache License

public static void main(String argv[]) throws Exception {

    if (argv.length < 1)
        throw new Exception("Command line argument number wrong");

    GazInfoIndexerAllCountries gi = new GazInfoIndexerAllCountries();

    //      argv[0]="-write";
    String mode = argv[0];/*w  w  w. j  a va 2  s. c  om*/

    if (mode.equals("-write")) {

        String argv1 = GlobalParam.getGeoNames() + "/allCountries.txt";
        String argv2 = GlobalParam.getGazIndex() + "/InfoIndex";
        BufferedReader br = GetReader.getUTF8FileReader(argv1);
        IndexWriter iw = GetWriter.getIndexWriter(argv2, 1400);
        iw.deleteAll();
        gi.indexGazatteer(br, iw);
        iw.close();
        br.close();
    }
    if (mode.equals("-read")) {
        System.out.println("input id. Output basic information. For debugging.");
        // query first two fields.
        String argv1 = GlobalParam.getGazIndex() + "/GazIndex/";
        IndexSearcher is = GetReader.getIndexSearcher(argv1, "disk");
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in, "utf-8"));
        String line;
        while ((line = r.readLine()) != null) {

            long id;
            try {
                id = Long.parseLong(line);
            } catch (Exception e) {
                System.err.println("number wrong.");
                continue;
            }

            Query q = NumericRangeQuery.newLongRange("ID", id, id, true, true);

            long start = System.currentTimeMillis();
            TopDocs docs = is.search(q, 1);
            if (docs == null) {
                System.err.println("Not found.");
                continue;
            }
            if (docs.scoreDocs.length == 0) {
                System.err.println("Not found.");
                continue;
            }
            ScoreDoc sd = docs.scoreDocs[0];
            Document d = is.doc(sd.doc);
            long end = System.currentTimeMillis();
            System.out.println(d);
            System.out.println(d.get("ID"));
            System.out.println(d.get("ORIGIN"));
            System.out.println(d.get("LONGTITUDE") + " " + d.get("LATITUDE"));
            System.out.println("lookup time: " + (end - start));
        }
    }
}

From source file:edu.cmu.geolocator.resource.gazindexing.CollaborativeIndex.GazStringIndexerAllCountries.java

License:Apache License

public static void main(String argv[]) throws Exception {

    if (argv.length < 1)
        throw new Exception("Command line argument number wrong");

    GazStringIndexerAllCountries gi = new GazStringIndexerAllCountries();

    //    argv[0] = "-read";
    String mode = argv[0];/* w ww.  ja va 2  s . co  m*/

    if (mode.equals("-write")) {
        String argv1 = GlobalParam.getGeoNames() + "/allCountries.txt";
        String argv2 = GlobalParam.getGazIndex() + "/StringIndex/";

        BufferedReader br = GetReader.getUTF8FileReader(argv1);
        IndexWriter iw = GetWriter.getIndexWriter(argv2, 1600);
        iw.deleteAll();
        gi.indexGazatteer(br, iw);
        // iw.optimize();
        iw.close();
        br.close();
    }
    if (mode.equals("-read")) {
        System.out.println("input id. Output basic information. For debugging.");
        // query first two fields.
        String argv1 = GlobalParam.getGazIndex() + "/StringIndex";
        IndexSearcher is = GetReader.getIndexSearcher(argv1, "disk");
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in, "utf-8"));
        String line;
        while ((line = r.readLine()) != null) {

            long id;
            try {
                id = Long.parseLong(line);
            } catch (Exception e) {
                System.err.println("number wrong.");
                continue;
            }

            Query q = NumericRangeQuery.newLongRange("ID", id, id, true, true);

            long start = System.currentTimeMillis();
            TopDocs docs = is.search(q, 1);
            if (docs == null) {
                System.err.println("Not found.");
                continue;
            }
            if (docs.scoreDocs.length == 0) {
                System.err.println("Not found.");
                continue;
            }
            ScoreDoc sd = docs.scoreDocs[0];
            Document d = is.doc(sd.doc);
            long end = System.currentTimeMillis();
            System.out.println(d);
            System.out.println(d.get("ID"));
            System.out.println(d.get("LOWERED_ORIGIN"));
        }
    }
}