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:lucene.demo.search.FileSearcher.java

License:Apache License

public boolean search() throws IOException, ParseException {

    // field from the indexed file we are searching
    String field = "content";

    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexPath)));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_48);

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));

    org.apache.lucene.queryparser.classic.QueryParser parser = new org.apache.lucene.queryparser.classic.QueryParser(
            Version.LUCENE_48, field, analyzer);

    // prompt user for stuff to remove
    System.out.println("I want to remove reviews with(Enter to end): ");
    String line = in.readLine();//from   ww  w. j  av  a2s. c o m

    if (line == null || line.length() == -1) {
        return false;
    }

    line = line.trim();
    if (line.length() == 0) {
        return false;
    }

    Query query = null;
    try {
        query = parser.parse(line);
    } catch (org.apache.lucene.queryparser.classic.ParseException e) {
        e.printStackTrace();
    }

    System.out.println("Searching for: " + query.toString(field));
    doPagingSearch(in, searcher, query);

    reader.close();
    return true;
}

From source file:luceneexamples.NumericFieldDocument.java

License:Apache License

@Test
public void index() throws Exception {
    RAMDirectory directory = new RAMDirectory();
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
    IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer);
    IndexWriter writer = new IndexWriter(directory, iwc);

    for (int i = 8; i < 12; i++) {
        Document doc = new Document();
        doc.add(new NumericField("int_field", Field.Store.YES, true).setIntValue(i));
        System.out.println(doc);/*  ww  w .  ja va 2 s.  co  m*/
        writer.addDocument(doc);
    }
    writer.commit();

    IndexReader reader = IndexReader.open(writer, true);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs td = searcher.search(new MatchAllDocsQuery(), 1000,
            new Sort(new SortField("int_field", SortField.INT)));
    assertThat(td.totalHits, is(4));
    assertThat(searcher.doc(td.scoreDocs[0].doc).get("int_field"), equalTo("8"));
    assertThat(searcher.doc(td.scoreDocs[1].doc).get("int_field"), equalTo("9"));
    assertThat(searcher.doc(td.scoreDocs[2].doc).get("int_field"), equalTo("10"));
    assertThat(searcher.doc(td.scoreDocs[3].doc).get("int_field"), equalTo("11"));

    reader.close();
    writer.close();
    searcher.close();
    directory.close();
}

From source file:luceneexamples.SortDocuments.java

License:Apache License

@Test
public void index() throws Exception {
    RAMDirectory directory = new RAMDirectory();
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
    IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer);
    IndexWriter writer = new IndexWriter(directory, iwc);

    Document doc = new Document();
    doc.add(new Field("str_field", "abc", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc);//w  w w.j  a v a 2 s  .c om
    Document doc2 = new Document();
    doc2.add(new Field("str_field", "def", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc2);
    Document doc3 = new Document();
    doc3.add(new Field("str_field", "hij", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc3);
    writer.commit();

    IndexReader reader = IndexReader.open(writer, true);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs td = searcher.search(new MatchAllDocsQuery(), 1000,
            new Sort(new SortField("str_field", SortField.STRING)));
    assertThat(td.totalHits, is(3));
    assertThat(searcher.doc(td.scoreDocs[0].doc).get("str_field"), equalTo("abc"));
    assertThat(searcher.doc(td.scoreDocs[1].doc).get("str_field"), equalTo("def"));
    assertThat(searcher.doc(td.scoreDocs[2].doc).get("str_field"), equalTo("hij"));

    td = searcher.search(new MatchAllDocsQuery(), 1000,
            new Sort(new SortField("str_field", SortField.STRING, true)));
    assertThat(td.totalHits, is(3));
    assertThat(searcher.doc(td.scoreDocs[0].doc).get("str_field"), equalTo("hij"));
    assertThat(searcher.doc(td.scoreDocs[1].doc).get("str_field"), equalTo("def"));
    assertThat(searcher.doc(td.scoreDocs[2].doc).get("str_field"), equalTo("abc"));

    reader.close();
    writer.close();
    searcher.close();
    directory.close();
}

From source file:luceneGazateer.EntryData.java

License:Apache License

public ArrayList<EntryData> searchDocuments(String indexerPath, String inputRecord, DocType recordType)
        throws IOException {

    File indexfile = new File(indexerPath);
    indexDir = FSDirectory.open(indexfile.toPath());

    //inputRecord.replace(","," ");
    if (!DirectoryReader.indexExists(indexDir)) {
        LOG.log(Level.SEVERE, "No Lucene Index Dierctory Found, Invoke indexBuild() First !");
        System.out.println("No Lucene Index Dierctory Found, Invoke indexBuild() First !");
        System.exit(1);//from  w  ww.  j av  a 2  s  .c  om
    }

    IndexReader reader = DirectoryReader.open(indexDir);

    IndexSearcher searcher = new IndexSearcher(reader);

    Query q = null;

    HashMap<String, ArrayList<ArrayList<String>>> allCandidates = new HashMap<String, ArrayList<ArrayList<String>>>();

    if (!allCandidates.containsKey(inputRecord)) {
        try {
            ArrayList<ArrayList<String>> topHits = new ArrayList<ArrayList<String>>();
            //System.out.println("query is : "+inputRecord);
            q = new MultiFieldQueryParser(new String[] { "DATA" }, analyzer).parse(inputRecord);

            TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage);
            searcher.search(q, collector);
            ScoreDoc[] hits = collector.topDocs().scoreDocs;
            for (int i = 0; i < hits.length; ++i) {
                ArrayList<String> tmp1 = new ArrayList<String>();
                int docId = hits[i].doc;
                Document d;
                try {
                    d = searcher.doc(docId);
                    tmp1.add(d.get("ID"));
                    tmp1.add(d.get("DATA"));
                    tmp1.add(((Float) hits[i].score).toString());

                } catch (IOException e) {
                    e.printStackTrace();
                }
                topHits.add(tmp1);
            }
            allCandidates.put(inputRecord, topHits);
        } catch (org.apache.lucene.queryparser.classic.ParseException e) {
            e.printStackTrace();
        }
    }

    ArrayList<EntryData> resolvedEntities = new ArrayList<EntryData>();
    pickBestCandidates(resolvedEntities, allCandidates);
    reader.close();

    return resolvedEntities;

}

From source file:lucenePackage.SearchFiles.java

License:Apache License

public static Results doSearch(String indexDirectory, String queryString, String maxresults, int totalAlldoc)
        throws Exception {
    result = new Results();
    String index = indexDirectory;
    String field = "contents";
    int repeat = 0;
    boolean raw = false;
    String normsField = null;/*from   w  w  w .ja v  a 2s  .co m*/
    boolean paging = true;
    int hitsPerPage = Integer.parseInt(maxresults);
    IndexReader reader = IndexReader.open(index);

    if (normsField != null)
        reader = new OneNormsReader(reader, normsField);

    Searcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new IKAnalyzer();

    String line = queryString;

    line = line.trim();
    Query query = IKQueryParser.parse(field, line);
    //     QueryParser parser = new QueryParser(field, analyzer);
    //     Query query = parser.parse(line);

    if (paging) {
        doPagingSearch(searcher, query, hitsPerPage, totalAlldoc);
    } else {
        doStreamingSearch(searcher, query);
    }
    reader.close();
    return result;
}

From source file:lucli.LuceneMethods.java

License:Apache License

public void info() throws java.io.IOException {
    IndexReader indexReader = IndexReader.open(indexName);

    getFieldInfo();/*from ww  w  .ja v  a  2 s .  c  o  m*/
    numDocs = indexReader.numDocs();
    message("Index has " + numDocs + " documents ");
    message("All Fields:" + fields.toString());
    message("Indexed Fields:" + indexedFields.toString());

    if (IndexReader.isLocked(indexName)) {
        message("Index is locked");
    }
    //IndexReader.getCurrentVersion(indexName);
    //System.out.println("Version:" + version);

    indexReader.close();
}

From source file:lucli.LuceneMethods.java

License:Apache License

private void getFieldInfo() throws IOException {
    IndexReader indexReader = IndexReader.open(indexName);
    fields = new ArrayList();
    indexedFields = new ArrayList();

    //get the list of all field names
    fieldIterator = indexReader.getFieldNames(FieldOption.ALL).iterator();
    while (fieldIterator.hasNext()) {
        Object field = fieldIterator.next();
        if (field != null && !field.equals(""))
            fields.add(field.toString());
    }/*from   w w  w  .  j  a v  a  2s.  c  om*/
    //
    //get the list of indexed field names
    fieldIterator = indexReader.getFieldNames(FieldOption.INDEXED).iterator();
    while (fieldIterator.hasNext()) {
        Object field = fieldIterator.next();
        if (field != null && !field.equals(""))
            indexedFields.add(field.toString());
    }
    indexReader.close();
}

From source file:lucli.LuceneMethods.java

License:Apache License

/** Provides a list of the top terms of the index.
 *
 * @param field  - the name of the command or null for all of them.
 *//*from   ww w . j a v  a  2 s  .c o  m*/
public void terms(String field) throws IOException {
    TreeMap termMap = new TreeMap();
    IndexReader indexReader = IndexReader.open(indexName);
    TermEnum terms = indexReader.terms();
    while (terms.next()) {
        Term term = terms.term();
        //message(term.field() + ":" + term.text() + " freq:" + terms.docFreq());
        //if we're either not looking by field or we're matching the specific field
        if ((field == null) || field.equals(term.field()))
            termMap.put(term.field() + ":" + term.text(), new Integer((terms.docFreq())));
    }

    Iterator termIterator = termMap.keySet().iterator();
    for (int ii = 0; termIterator.hasNext() && ii < 100; ii++) {
        String termDetails = (String) termIterator.next();
        Integer termFreq = (Integer) termMap.get(termDetails);
        message(termDetails + ": " + termFreq);
    }
    indexReader.close();
}

From source file:magoffin.matt.lucene.LuceneSearchService.java

License:Open Source License

private void closeIndexReader(IndexReader reader, String type) {
    if (reader != null) {
        if (traceLog.isInfoEnabled()) {
            traceLog.info(TraceOp.CONCURRENCY + "Closing Lucene IndexReader " + reader + " [" + type + "]");
        }//from w  w  w  . ja  va2s.c o  m
        try {
            reader.close();
        } catch (IOException e) {

            throw new RuntimeException("Unable to close Lucene index reader", e);
        }
    }
}

From source file:magoffin.matt.ma.biz.impl.SearchBizLuceneImpl.java

License:Open Source License

private boolean deleteItemFromIndex(Integer itemId) throws MediaAlbumException {
    try {//from  ww w.  j a v a  2s  .  com
        Term idTerm = new Term(FIELD_ITEM_ID, itemId.toString());
        if (getItemIndexReader().docFreq(idTerm) > 0) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Deleting MediaItem " + itemId + " from index");
            }

            closeItemIndexWriter();

            // get new reader
            IndexReader reader = IndexReader.open(itemDirectory);
            reader.delete(idTerm);
            reader.close();

            return true;
        }
    } catch (IOException e) {
        throw new MediaAlbumException("IOException looking for existing item in index", e);
    }

    return false;
}