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:edu.cmu.geoparser.resource.gazindexing.CollaborativeIndex.GazInfoIndexerAllCountries.java

License:Apache License

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

    GazInfoIndexerAllCountries gi = new GazInfoIndexerAllCountries();

    argv[0] = "-write";
    String mode = argv[0];/* w w  w  . ja v  a  2  s. co  m*/

    if (mode.equals("-write")) {
        if (argv.length != 3)
            throw new Exception("Command line argument number wrong");
        argv[1] = "GeoNames/allCountries.txt";
        argv[2] = "GazIndex/InfoIndex";
        BufferedReader br = GetReader.getUTF8FileReader(argv[1]);
        IndexWriter iw = GetWriter.getIndexWriter(argv[2], 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.
        argv[1] = "/Users/Indri/Eclipse_workspace/GazIndex/";
        IndexSearcher is = GetReader.getIndexSearcher(argv[1], "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.geoparser.resource.gazindexing.CollaborativeIndex.GazStringIndexerAllCountries.java

License:Apache License

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

    GazStringIndexerAllCountries gi = new GazStringIndexerAllCountries();

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

    if (mode.equals("-write")) {
        if (argv.length != 3)
            throw new Exception("Command line argument number wrong");
        argv[1] = "GeoNames/allCountries.txt";
        argv[2] = "GazIndex/StringIndex/";

        BufferedReader br = GetReader.getUTF8FileReader(argv[1]);
        IndexWriter iw = GetWriter.getIndexWriter(argv[2], 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.
        argv[1] = "GazIndex/StringIndex";
        IndexSearcher is = GetReader.getIndexSearcher(argv[1], "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"));
        }
    }
}

From source file:edu.utah.bmi.ibiomes.catalog.cli.DictionaryBuilder.java

License:Open Source License

/**
 * //from  w  w w .ja va2  s. c  o  m
 * @param semTypes
 * @param terminologies
 * @param normalize
 * @param indexFile
 * @throws Exception
 */
private void buildIndexFromMysql(String indexFile) throws Exception {
    Connection con = null;
    int docCount = 0;

    try {
        Statement stmt;
        //Register the JDBC driver for MySQL.
        Class.forName("com.mysql.jdbc.Driver");

        //Define URL of database
        System.out.println("---------------------------------------------------------------------");
        String url = "jdbc:mysql://" + _dbServer + ":" + _dbPort + "/" + _dbSchema;
        System.out.println("Connecting to database: " + url);

        //Get a connection to the database
        con = DriverManager.getConnection(url, _dbUser, _dbPassword);
        System.out.println("Connected!");
        System.out.println("---------------------------------------------------------------------");

        Directory dir = FSDirectory.open(new File(indexFile));
        Analyzer analyzer = new KeywordAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_41, analyzer);
        IndexWriter writer = new IndexWriter(dir, config);
        writer.deleteAll();

        String query = "select * FROM METADATA_ATTRIBUTE";

        //Get a Statement object
        stmt = con.createStatement();

        System.out.println("Executing SQL query...");
        ResultSet rs = stmt.executeQuery(query);

        //read each SQL record
        while (rs.next()) {
            String attributeCode = rs.getString("CODE");
            attributeCode = attributeCode.toUpperCase();
            String attributeTerm = rs.getString("TERM");
            String attributeDef = rs.getString("DEFINITION");
            String valueType = rs.getString("TYPE");

            docCount++;
            Document document = new Document();

            //indexed fields
            document.add(new TextField(MetadataLookup.LOOKUP_FIELD_CODE, attributeCode, Field.Store.YES));

            if (attributeTerm != null) {
                document.add(new TextField(MetadataLookup.LOOKUP_FIELD_TERM, attributeTerm, Field.Store.YES));
            }
            if (attributeDef != null) {
                document.add(
                        new TextField(MetadataLookup.LOOKUP_FIELD_DEFINITION, attributeDef, Field.Store.YES));
            }
            if (valueType != null) {
                document.add(new TextField(MetadataLookup.LOOKUP_FIELD_TYPE, valueType, Field.Store.YES));
            }

            System.out.println("[" + docCount + "] " + attributeCode + " (" + attributeTerm + ")");
            writer.addDocument(document);

        }
        rs.close();
        writer.close();

        System.out.println("\n" + docCount + " added to index '" + indexFile + "'\n");
        System.out.println("---------------------------------------------------------------------");

        //close SQL connection
        System.out.println("Closing SQL connection...");
        con.close();
        System.out.println("Done!");
    } catch (Exception e) {
        e.printStackTrace();
        if (con != null)
            con.close();
        System.out.println("\n" + docCount + " added to index '" + indexFile + "'\n");
        throw new Exception(e);
    }
}

From source file:edu.utah.bmi.ibiomes.dictionary.lucene.LuceneDictionaryBuilder.java

License:Open Source License

/**
 * Delete dictionary/*  ww  w .  j a va 2 s  .  com*/
 * @param indexFilePath Path to index file
 * @throws IOException 
 */
public void deleteDictionary(String indexFilePath) throws IOException {
    File indexFile = new File(indexFilePath);
    Directory dir = FSDirectory.open(indexFile);
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_41,
            new StandardAnalyzer(Version.LUCENE_41));
    IndexWriter writer = new IndexWriter(dir, config);
    writer.deleteAll();
    writer.close();
}

From source file:it.unibz.instasearch.indexing.StorageIndexer.java

License:Open Source License

/**
 * Delethe the whole index//  w  ww.jav  a  2 s. co  m
 * @throws Exception
 */
public void deleteIndex() throws Exception {

    RetryingRunnable runnable = new RetryingRunnable() {
        public void run() throws Exception {
            IndexWriter w = createIndexWriter(true); // open for writing and close (make empty)
            w.deleteAll();
            w.commit();
            w.close(true);

            Directory dir = getIndexDir();
            for (String file : dir.listAll()) {
                if (dir.fileExists(file)) // still exits
                {
                    dir.sync(file);
                    dir.deleteFile(file);
                }
            }
            dir.close();
        }

        public boolean handleException(Throwable e) {
            return true;
        }
    };

    changeListener.onIndexReset(); // close searcher because index is deleted

    runRetryingRunnable(runnable); // delete index with retry
}

From source file:net.sf.logsaw.index.internal.LuceneIndexServiceImpl.java

License:Open Source License

private void truncate(ILogResource log, IndexWriter writer) throws CoreException {
    try {//from ww  w.  j ava 2  s. c o m
        writer.deleteAll();
        writer.commit();
    } catch (Exception e) {
        // Unexpected exception; wrap with CoreException
        throw new CoreException(new Status(IStatus.ERROR, IndexPlugin.PLUGIN_ID,
                NLS.bind(Messages.LuceneIndexService_error_failedToTruncateIndex,
                        new Object[] { log.getName(), e.getLocalizedMessage() }),
                e));
    }
}

From source file:net.simpleframework.ado.lucene.AbstractLuceneManager.java

License:Apache License

@Override
public void rebuildIndex() {
    final IDataQuery<?> dq = queryAll();
    if (dq == null) {
        return;//from  www  .  j a v  a 2 s .co  m
    }
    IndexWriter iWriter = null;
    try {
        iWriter = createIndexWriter();
        if (indexExists()) {
            iWriter.deleteAll();
        }
        dq.setFetchSize(0);
        for (Object obj; (obj = dq.next()) != null;) {
            final LuceneDocument document = new LuceneDocument();
            if (objectToDocument(obj, document)) {
                iWriter.addDocument(document.doc);
            }
        }
        iWriter.commit();
    } catch (final IOException e) {
        throw ADOException.of(e);
    } finally {
        closeWriter(iWriter);
    }
}

From source file:net.ymate.platform.module.search.Searchs.java

License:Apache License

private static void __doIndexRebuild(final String name, final int batchSize, final IRebuildHandler handler) {
    if (__indexHelper.__doCheckBuildWorking(name)) {
        return;/* w  w  w .ja  v a  2s .  co  m*/
    }
    __executor.execute(new Runnable() {

        public void run() {
            __indexHelper.__doSetBuildWorking(name, true);
            //
            IndexWriter _writer = getIndexWriter(name);
            try {
                _writer.deleteAll();
                //
                int _batchSize = batchSize > 0 ? batchSize : 100;
                long _count = handler.getAmount();
                long _batchCount = 0;
                if (_count % _batchSize > 0) {
                    _batchCount = _count / _batchSize + 1;
                } else {
                    _batchCount = _count / _batchSize;
                }
                //
                for (int _idx = 1; _idx <= _batchCount; _idx++) {
                    List<? extends ISearchable> _datas = handler.getBatchDatas(_idx, _batchSize);
                    for (ISearchable _data : _datas) {
                        Document _doc = __doIndexDocumentCreate(_data);
                        _writer.addDocument(_doc);
                        handler.onIndexCreated(_data);
                    }
                }
            } catch (Exception e) {
                _LOG.info("Index Rebuilding For " + name + " Error:", RuntimeUtils.unwrapThrow(e));
            } finally {
                __indexHelper.__doSetBuildWorking(name, false);
            }
        }

    });
}

From source file:nmsu.cs.TFIDFVector.java

License:Open Source License

/**
 * build Lucene index for the text//from   w  w w  . j av  a2 s .  c om
 * @param indexDir
 */
public void buildIndex(String indexDir) {

    //      System.out.println(Debugger.getCallerPosition()+" is RawData null "+rawdata==null);

    IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_36, new PorterAnalyzer());
    IndexWriter w;
    try {
        w = new IndexWriter(FSDirectory.open(new File(indexDir)), conf);
        w.deleteAll();
        w.commit();

        System.out.println(rawdata.id2Docs.size());

        for (Map.Entry<Integer, Doc> entry : rawdata.id2Docs.entrySet()) {
            Document document = BaseLineMethod.convertDoc2Document(entry.getValue());
            w.addDocument(document);
        }

        w.commit();
        w.close();

    } catch (CorruptIndexException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (LockObtainFailedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.apache.clerezza.rdf.cris.GraphIndexer.java

License:Apache License

@Override
public void reCreateIndex() {
    processDefinitions();/*from  w w  w  .ja va 2s  .c om*/

    List<NonLiteral> instances = new ArrayList<NonLiteral>();

    for (UriRef indexedType : type2IndexedProperties.keySet()) {
        //lock necessary?
        Lock lock = new GraphNode(indexedType, this.baseGraph).readLock();
        lock.lock();
        try {
            Iterator<Triple> iter = this.baseGraph.filter(null, RDF.type, indexedType);
            while (iter.hasNext()) {
                instances.add(iter.next().getSubject());
            }
        } finally {
            lock.unlock();
        }
    }
    logger.debug("instances " + instances.size());
    IndexWriter writer = luceneTools.getIndexWriter(true);
    try {
        writer.deleteAll();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    for (NonLiteral instance : instances) {
        indexResource(instance, writer);
    }
    luceneTools.commitChanges();
}