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

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

Introduction

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

Prototype

@Override
public final synchronized void close() throws IOException 

Source Link

Document

Closes files associated with this index.

Usage

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

public List<SEntity> search(String kind, Query query, Filter filter, Sort sort, int pagesize, int pageno) {
    List<SEntity> tag = new ArrayList<SEntity>();
    try {/*from w  w w. j av a  2  s  . c  o m*/
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex)));
        IndexSearcher searcher = new IndexSearcher(reader);
        BooleanQuery boolQuery = new BooleanQuery();
        boolQuery.add(new BooleanClause(new TermQuery(new Term(SEntity.KIND, kind)), Occur.MUST));
        if (query != null) {
            boolQuery.add(new BooleanClause(query, Occur.MUST));
        }
        if (pagesize <= 0)
            pagesize = 10;
        if (pageno <= 0)
            pageno = 1;
        int max = pageno * pagesize;
        TopDocs td = null;
        if (filter != null && sort != null) {
            td = searcher.search(boolQuery, filter, max, sort);
        } else if (filter != null) {
            td = searcher.search(boolQuery, filter, max);
        } else if (sort != null) {
            td = searcher.search(boolQuery, max, sort);
        } else {
            td = searcher.search(boolQuery, max);
        }
        for (int i = (pageno - 1) * pagesize; i < td.totalHits && i < max; i++) {
            SEntity item = new SEntity(this);
            Document doc = searcher.doc(td.scoreDocs[i].doc);
            item.setSchema(doc.get(SEntity.SCHEMA));
            read(item, doc);
            tag.add(item);
        }
        searcher.close();
        reader.close();
    } catch (Exception e) {
    }
    return tag;
}

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

protected void deleteEntity(String id) {
    if (id.length() == 0)
        return;//from w  w w.  j  a va2  s. c  om
    String kind = "";

    try {
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex)));
        IndexSearcher searcher = new IndexSearcher(reader);
        TopDocs td = searcher.search(new TermQuery(new Term(SEntity.ID, id)), 1);
        if (td.totalHits > 0) {
            Document doc = searcher.doc(td.scoreDocs[0].doc);
            kind = doc.get(SEntity.KIND);
        }
        searcher.close();
        reader.close();
    } catch (Exception e) {
    }
    if (kind.length() == 0)
        return;
    if (!allowDelete(id, kind))
        return;

    try {
        if (!kind.equals(KIND_QUOTA)) {
            if (!quotaDelete(id, kind))
                return;
        }
        removeBackup(id, kind);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer);
        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        IndexWriter writer = new IndexWriter(FSDirectory.open(new File(dirIndex)), iwc);
        writer.deleteDocuments(new Term(SEntity.ID, id));
        writer.close();
    } catch (Exception e) {
    }
}

From source file:com.bluecubs.xinco.index.XincoIndexer.java

License:Apache License

public static synchronized boolean removeXincoCoreData(XincoCoreData d, XincoDBManager dbm) {

    IndexReader reader = null;

    //check if document exists in index and delete
    try {/*from ww w.j a v  a 2s. co  m*/
        reader = IndexReader.open(dbm.config.FileIndexPath);
        reader.delete(new Term("id", "" + d.getId()));
        reader.close();
    } catch (Exception re) {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception re2) {
            }
        }
        return false;
    }

    return true;
}

From source file:com.browseengine.bobo.impl.BrowseServiceImpl.java

License:Open Source License

public static BoboIndexReader newIndexReader(Directory idxDir) throws IOException {
    if (!IndexReader.indexExists(idxDir)) {
        return null;
    }//from ww w.  ja  v a 2 s.c o m

    long start = System.currentTimeMillis();

    IndexReader ir = IndexReader.open(idxDir, true);
    BoboIndexReader reader;

    try {
        reader = BoboIndexReader.getInstance(ir);
    } catch (IOException ioe) {
        try {
            ir.close();
        } catch (IOException ioe2) {
        }
        throw ioe;
    }

    long end = System.currentTimeMillis();

    if (logger.isDebugEnabled()) {
        logger.debug("New index loading took: " + (end - start));
    }

    return reader;
}

From source file:com.browseengine.bobo.service.BrowseServiceFactory.java

License:Open Source License

public static BoboIndexReader getBoboIndexReader(Directory idxDir) throws BrowseException {
    try {/*from   ww w  .ja  v a  2 s  .  c  o  m*/
        if (!BoboIndexReader.indexExists(idxDir)) {
            throw new BrowseException("Index does not exist at: " + idxDir);
        }
    } catch (IOException ioe) {
        throw new BrowseException(ioe.getMessage(), ioe);
    }

    IndexReader reader = null;
    try {
        reader = IndexReader.open(idxDir);
    } catch (IOException ioe) {
        throw new BrowseException(ioe.getMessage(), ioe);
    }

    BoboIndexReader bReader = null;
    try {
        bReader = BoboIndexReader.getInstance(reader);
    } catch (IOException ioe) {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
        throw new BrowseException(ioe.getMessage(), ioe);
    }
    return bReader;
}

From source file:com.browseengine.bobo.test.BoboTestCase.java

License:Open Source License

private BoboIndexReader newIndexReader(boolean readonly) throws IOException {
    IndexReader srcReader = IndexReader.open(_indexDir, readonly);
    try {// w w w  .  j av a  2 s. com
        BoboIndexReader reader = BoboIndexReader.getInstance(srcReader, _fconf);
        return reader;
    } catch (IOException ioe) {
        if (srcReader != null) {
            srcReader.close();
        }
        throw ioe;
    }
}

From source file:com.browseengine.bobo.test.BoboTestCase.java

License:Open Source License

private Directory createIndex() {
    RAMDirectory idxDir = new RAMDirectory();

    try {//  www  .j  a  va2  s  . c o m
        Document[] data = buildData();

        TestDataDigester testDigester = new TestDataDigester(_fconf, data);
        BoboIndexer indexer = new BoboIndexer(testDigester, idxDir);
        indexer.index();
        IndexReader r = IndexReader.open(idxDir, false);
        r.deleteDocument(r.maxDoc() - 1);
        //r.flush();
        r.close();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return idxDir;

}

From source file:com.browseengine.bobo.test.BoboTestCase.java

License:Open Source License

public void testLuceneSort() throws IOException {

    IndexReader srcReader = IndexReader.open(_indexDir, true);
    try {/*  www .  j  a  v a 2  s .  c  om*/
        List<FacetHandler<?>> facetHandlers = new ArrayList<FacetHandler<?>>();
        facetHandlers.add(new SimpleFacetHandler("id"));

        BoboIndexReader reader = BoboIndexReader.getInstance(srcReader, facetHandlers); // not facet handlers to help
        BoboBrowser browser = new BoboBrowser(reader);

        BrowseRequest browseRequest = new BrowseRequest();
        browseRequest.setCount(10);
        browseRequest.setOffset(0);
        browseRequest.addSortField(new SortField("date", SortField.STRING));

        doTest(browser, browseRequest, 7, null, new String[] { "1", "3", "5", "2", "4", "7", "6" });

    } catch (IOException ioe) {
        if (srcReader != null) {
            srcReader.close();
        }
        throw ioe;
    }
}

From source file:com.browseengine.bobo.test.FacetNameTest.java

License:Apache License

private Directory createIndex() {
    Directory dir = new RAMDirectory();
    try {/*w  w  w  .  j ava 2s  .c  o m*/
        Document[] data = createData();

        TestDataDigester testDigester = new TestDataDigester(_facetHandlers, data);
        BoboIndexer indexer = new BoboIndexer(testDigester, dir);
        indexer.index();
        IndexReader r = IndexReader.open(dir, false);
        r.close();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return dir;
}

From source file:com.browseengine.bobo.test.FacetNotValuesTest.java

License:Open Source License

private Directory createIndexTwo() {
    Directory dir = new RAMDirectory();
    try {// www.j a va2  s. c o m
        Document[] data = createDataTwo();

        TestDataDigester testDigester = new TestDataDigester(_facetHandlers, data);
        BoboIndexer indexer = new BoboIndexer(testDigester, dir);
        indexer.index();
        IndexReader r = IndexReader.open(dir, false);
        r.close();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return dir;
}