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.revorg.goat.IndexManager.java

License:Open Source License

/**
 * Checks to see whether an index exists or not
 *
 * @param indexPath Directory that contains the Lucene Collection
 * @throws Exception/*from   ww  w  .j  a  v a2s  .  c o m*/
 * @return ActionResult
 */
public static String isIndexExistant(String indexPath) {
    try {
        IndexReader reader = IndexReader.open(indexPath);
        boolean indexExists = reader.indexExists(indexPath);
        reader.close(); //Close Index
        if (indexExists) {
            ActionResult = "Yes";
        } else {
            ActionResult = "No";
        }
        return ActionResult;
    } catch (Exception e) {
        IndexManager.deleteIndex(indexPath); //Delete index
        ActionResultError = " caught a " + e.getClass() + " with message: " + e.getMessage();
        System.out.println("Failure to check index: " + indexPath);
    }
    ActionResult = "Failure";
    return ActionResult + ActionResultError;
}

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

License:Open Source License

/**
 * Searches the Lucene index based on file system documents.
 *
 * @param indexPath Directory that contains the Lucene Collection         
 * @param hitsPerPage           Number of hits per page
 * @param pageNum               The page number to search
 * @param searchString          The search string for searching
 * @param searchType            The type of results to return
 * @param sortField             The field to sort on if needed
 * @param sortDirection         The direction to sort by
 * @throws Exception//from   ww  w  .  j a v  a 2 s.  c om
 * @return ActionResult
 */
public static String doSingleSearch(String indexPath, int hitsPerPage, int pageNum, String searchString,
        String searchType, String sortField, String sortDirection) {
    try {
        if (searchString != null)
            searchString.trim().toLowerCase(); //Lower Case Search
        IndexReader reader = IndexReader.open(indexPath);
        IndexSearcher searcher = new IndexSearcher(indexPath);
        if (searcher == null) {
            throw new Exception("Unable to open index");
        }
        QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
        Query query = parser.parse(searchString);
        if (query == null) {
            throw new Exception("Unable to build Query");
        }
        String returnText = "";
        returnText = doPagingSearch(indexPath, searcher, query, hitsPerPage, pageNum, searchType, sortField,
                sortDirection);
        //System.out.println(returnText);
        reader.close();
        //Paging Results
        return returnText;
    }

    catch (Exception e) {
        ActionResultError = " caught a " + e.getClass() + " with message: " + e.getMessage();
        //System.out.println("Failure of DbSchema File: " + xmlFile);
    }
    ActionResult = "Failure";
    return ActionResult + ActionResultError;
}

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

License:Open Source License

/**
 * Searches the Lucene index based on database documents.
 *
 * @param indexPath Directory that contains the Lucene Collection         
 * @param hitsPerPage           Number of hits per page
 * @param pageNum               The page number to search
 * @param searchString          The search string for searching
 * @param searchType            The type of results to return
 * @param sortField             The field to sort on if needed
 * @param sortDirection         The direction to sort by
 * @throws Exception/*  ww w  .j  a va 2 s. co  m*/
 * @return ActionResult
 */

public static String doMultiSearch(String indexPath, int hitsPerPage, int pageNum, String searchFieldsArray[],
        String searchString, String searchType, String sortField, String sortDirection) {
    try {
        if (searchString != null)
            searchString.trim().toLowerCase(); //Lower Case Search
        IndexReader reader = IndexReader.open(indexPath);
        IndexSearcher searcher = new IndexSearcher(indexPath);
        if (searcher == null) {
            throw new Exception("Unable to open index");
        }
        QueryParser parser = new MultiFieldQueryParser(searchFieldsArray, new StandardAnalyzer());
        Query query = parser.parse(searchString);
        if (query == null) {
            throw new Exception("Unable to build Query");
        }
        String returnText = "";
        returnText = doPagingSearch(indexPath, searcher, query, hitsPerPage, pageNum, searchType, sortField,
                sortDirection);
        //System.out.println(returnText);
        reader.close();
        //Paging Results
        return returnText;
    }

    catch (Exception e) {
        ActionResultError = " caught a " + e.getClass() + " with message: " + e.getMessage();
        //System.out.println("Failure of DbSchema File: " + xmlFile);
    }
    ActionResult = "Failure";
    return ActionResult + ActionResultError;
}

From source file:com.ricky.codelab.lucene.LuceneIndexAndSearchDemo.java

License:Apache License

/**
 * //w  w w  .  j a va2 s.  co  m
 * ???
 * @param args
 */
public static void main(String[] args) {
    //Lucene Document??
    String fieldName = "text";
    //
    String text = "IK Analyzer???????";

    //IKAnalyzer?
    Analyzer analyzer = new IKAnalyzer(true);

    Directory directory = null;
    IndexWriter iwriter = null;
    IndexReader ireader = null;
    IndexSearcher isearcher = null;
    try {
        //
        directory = new RAMDirectory();

        //?IndexWriterConfig
        IndexWriterConfig iwConfig = new IndexWriterConfig(analyzer);
        iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
        iwriter = new IndexWriter(directory, iwConfig);
        //
        Document doc = new Document();
        doc.add(new StringField("ID", "10000", Field.Store.YES));
        doc.add(new TextField(fieldName, text, Field.Store.YES));
        iwriter.addDocument(doc);
        iwriter.close();

        //?**********************************
        //?   
        ireader = DirectoryReader.open(directory);
        isearcher = new IndexSearcher(ireader);

        String keyword = "?";
        //QueryParser?Query
        QueryParser qp = new QueryParser(fieldName, analyzer);
        qp.setDefaultOperator(QueryParser.AND_OPERATOR);
        Query query = qp.parse(keyword);
        System.out.println("Query = " + query);

        //?5?
        TopDocs topDocs = isearcher.search(query, 5);
        System.out.println("" + topDocs.totalHits);
        //
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (int i = 0; i < topDocs.totalHits; i++) {
            Document targetDoc = isearcher.doc(scoreDocs[i].doc);
            System.out.println("" + targetDoc.toString());
        }

    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (LockObtainFailedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    } finally {
        if (ireader != null) {
            try {
                ireader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (directory != null) {
            try {
                directory.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.rocana.lucene.codec.v1.RocanaBasePostingsFormatTestCase.java

License:Apache License

@Override
public void testInvertedWrite() throws Exception {
    Directory dir = newDirectory();/*w ww  . j  av  a  2  s.c o m*/
    MockAnalyzer analyzer = new MockAnalyzer(random());
    analyzer.setMaxTokenLength(TestUtil.nextInt(random(), 1, IndexWriter.MAX_TERM_LENGTH));
    IndexWriterConfig iwc = newIndexWriterConfig(analyzer);

    // Must be concurrent because thread(s) can be merging
    // while up to one thread flushes, and each of those
    // threads iterates over the map while the flushing
    // thread might be adding to it:
    final Map<String, TermFreqs> termFreqs = new ConcurrentHashMap<>();

    final AtomicLong sumDocFreq = new AtomicLong();
    final AtomicLong sumTotalTermFreq = new AtomicLong();

    // TODO: would be better to use / delegate to the current
    // Codec returned by getCodec()

    iwc.setCodec(new AssertingCodec() {
        @Override
        public PostingsFormat getPostingsFormatForField(String field) {

            PostingsFormat p = getCodec().postingsFormat();
            if (p instanceof PerFieldPostingsFormat) {
                p = ((PerFieldPostingsFormat) p).getPostingsFormatForField(field);
            }
            if (p instanceof RocanaPerFieldPostingsFormat) {
                p = ((RocanaPerFieldPostingsFormat) p).getPostingsFormatForField(field);
            }
            final PostingsFormat defaultPostingsFormat = p;

            final Thread mainThread = Thread.currentThread();

            if (field.equals("body")) {

                // A PF that counts up some stats and then in
                // the end we verify the stats match what the
                // final IndexReader says, just to exercise the
                // new freedom of iterating the postings more
                // than once at flush/merge:

                return new PostingsFormat(defaultPostingsFormat.getName()) {

                    @Override
                    public FieldsConsumer fieldsConsumer(final SegmentWriteState state) throws IOException {

                        final FieldsConsumer fieldsConsumer = defaultPostingsFormat.fieldsConsumer(state);

                        return new FieldsConsumer() {
                            @Override
                            public void write(Fields fields) throws IOException {
                                fieldsConsumer.write(fields);

                                boolean isMerge = state.context.context == IOContext.Context.MERGE;

                                // We only use one thread for flushing
                                // in this test:
                                assert isMerge || Thread.currentThread() == mainThread;

                                // We iterate the provided TermsEnum
                                // twice, so we excercise this new freedom
                                // with the inverted API; if
                                // addOnSecondPass is true, we add up
                                // term stats on the 2nd iteration:
                                boolean addOnSecondPass = random().nextBoolean();

                                //System.out.println("write isMerge=" + isMerge + " 2ndPass=" + addOnSecondPass);

                                // Gather our own stats:
                                Terms terms = fields.terms("body");
                                assert terms != null;

                                TermsEnum termsEnum = terms.iterator();
                                PostingsEnum docs = null;
                                while (termsEnum.next() != null) {
                                    BytesRef term = termsEnum.term();
                                    // TODO: also sometimes ask for payloads/offsets?
                                    boolean noPositions = random().nextBoolean();
                                    if (noPositions) {
                                        docs = termsEnum.postings(docs, PostingsEnum.FREQS);
                                    } else {
                                        docs = termsEnum.postings(null, PostingsEnum.POSITIONS);
                                    }
                                    int docFreq = 0;
                                    long totalTermFreq = 0;
                                    while (docs.nextDoc() != PostingsEnum.NO_MORE_DOCS) {
                                        docFreq++;
                                        totalTermFreq += docs.freq();
                                        int limit = TestUtil.nextInt(random(), 1, docs.freq());
                                        if (!noPositions) {
                                            for (int i = 0; i < limit; i++) {
                                                docs.nextPosition();
                                            }
                                        }
                                    }

                                    String termString = term.utf8ToString();

                                    // During merge we should only see terms
                                    // we had already seen during a
                                    // previous flush:
                                    assertTrue(isMerge == false || termFreqs.containsKey(termString));

                                    if (isMerge == false) {
                                        if (addOnSecondPass == false) {
                                            TermFreqs tf = termFreqs.get(termString);
                                            if (tf == null) {
                                                tf = new TermFreqs();
                                                termFreqs.put(termString, tf);
                                            }
                                            tf.docFreq += docFreq;
                                            tf.totalTermFreq += totalTermFreq;
                                            sumDocFreq.addAndGet(docFreq);
                                            sumTotalTermFreq.addAndGet(totalTermFreq);
                                        } else if (termFreqs.containsKey(termString) == false) {
                                            // Add placeholder (2nd pass will
                                            // set its counts):
                                            termFreqs.put(termString, new TermFreqs());
                                        }
                                    }
                                }

                                // Also test seeking the TermsEnum:
                                for (String term : termFreqs.keySet()) {
                                    if (termsEnum.seekExact(new BytesRef(term))) {
                                        // TODO: also sometimes ask for payloads/offsets?
                                        boolean noPositions = random().nextBoolean();
                                        if (noPositions) {
                                            docs = termsEnum.postings(docs, PostingsEnum.FREQS);
                                        } else {
                                            docs = termsEnum.postings(null, PostingsEnum.POSITIONS);
                                        }

                                        int docFreq = 0;
                                        long totalTermFreq = 0;
                                        while (docs.nextDoc() != PostingsEnum.NO_MORE_DOCS) {
                                            docFreq++;
                                            totalTermFreq += docs.freq();
                                            int limit = TestUtil.nextInt(random(), 1, docs.freq());
                                            if (!noPositions) {
                                                for (int i = 0; i < limit; i++) {
                                                    docs.nextPosition();
                                                }
                                            }
                                        }

                                        if (isMerge == false && addOnSecondPass) {
                                            TermFreqs tf = termFreqs.get(term);
                                            assert tf != null;
                                            tf.docFreq += docFreq;
                                            tf.totalTermFreq += totalTermFreq;
                                            sumDocFreq.addAndGet(docFreq);
                                            sumTotalTermFreq.addAndGet(totalTermFreq);
                                        }

                                        //System.out.println("  term=" + term + " docFreq=" + docFreq + " ttDF=" + termToDocFreq.get(term));
                                        assertTrue(docFreq <= termFreqs.get(term).docFreq);
                                        assertTrue(totalTermFreq <= termFreqs.get(term).totalTermFreq);
                                    }
                                }

                                // Also test seekCeil
                                for (int iter = 0; iter < 10; iter++) {
                                    BytesRef term = new BytesRef(
                                            TestUtil.randomRealisticUnicodeString(random()));
                                    SeekStatus status = termsEnum.seekCeil(term);
                                    if (status == SeekStatus.NOT_FOUND) {
                                        assertTrue(term.compareTo(termsEnum.term()) < 0);
                                    }
                                }
                            }

                            @Override
                            public void close() throws IOException {
                                fieldsConsumer.close();
                            }
                        };
                    }

                    @Override
                    public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException {
                        return defaultPostingsFormat.fieldsProducer(state);
                    }
                };
            } else {
                return defaultPostingsFormat;
            }
        }
    });

    RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);

    LineFileDocs docs = new LineFileDocs(random());
    int bytesToIndex = atLeast(100) * 1024;
    int bytesIndexed = 0;
    while (bytesIndexed < bytesToIndex) {
        Document doc = docs.nextDoc();
        w.addDocument(doc);
        bytesIndexed += RamUsageTester.sizeOf(doc);
    }

    IndexReader r = w.getReader();
    w.close();

    Terms terms = MultiFields.getTerms(r, "body");
    assertEquals(sumDocFreq.get(), terms.getSumDocFreq());
    assertEquals(sumTotalTermFreq.get(), terms.getSumTotalTermFreq());

    TermsEnum termsEnum = terms.iterator();
    long termCount = 0;
    boolean supportsOrds = true;
    while (termsEnum.next() != null) {
        BytesRef term = termsEnum.term();
        assertEquals(termFreqs.get(term.utf8ToString()).docFreq, termsEnum.docFreq());
        assertEquals(termFreqs.get(term.utf8ToString()).totalTermFreq, termsEnum.totalTermFreq());
        if (supportsOrds) {
            long ord;
            try {
                ord = termsEnum.ord();
            } catch (UnsupportedOperationException uoe) {
                supportsOrds = false;
                ord = -1;
            }
            if (ord != -1) {
                assertEquals(termCount, ord);
            }
        }
        termCount++;
    }
    assertEquals(termFreqs.size(), termCount);

    r.close();
    dir.close();
}

From source file:com.rocana.lucene.codec.v1.TestRocanaPerFieldPostingsFormat2.java

License:Apache License

public void assertQuery(Term t, Directory dir, int num) throws IOException {
    if (VERBOSE) {
        System.out.println("\nTEST: assertQuery " + t);
    }//from   w  ww.  java2 s .  com
    IndexReader reader = DirectoryReader.open(dir);
    IndexSearcher searcher = newSearcher(reader);
    TopDocs search = searcher.search(new TermQuery(t), num + 10);
    assertEquals(num, search.totalHits);
    reader.close();

}

From source file:com.rubenlaguna.en4j.searchlucene.NoteFinderLuceneImpl.java

License:Open Source License

public Collection<Note> find(String searchText) {
    if ("".equals(searchText.trim())) {
        return Collections.EMPTY_LIST;
    }/*from  www.java 2  s  .  co  m*/
    long start = System.currentTimeMillis();
    searchText = searchText.trim();
    String patternStr = "\\s+";
    String replaceStr = "* ";
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(searchText);
    searchText = matcher.replaceAll(replaceStr);
    if (Pattern.matches(".*\\w$", searchText)) {
        searchText = searchText + "*";
    }

    LOG.info("search text:" + searchText);
    final Collection<Note> toReturn = new ArrayList<Note>();

    try {
        IndexReader newReader = reader.reopen();
        if (newReader != reader) {
            reader.close();
        }
        reader = newReader;
        LOG.info("using index version: " + reader.getVersion());
        final IndexSearcher searcher = new IndexSearcher(reader);

        final Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);
        QueryParser parser = new CustomQueryParser("all", analyzer);
        parser.setDefaultOperator(QueryParser.Operator.AND);

        Query query = parser.parse(searchText);
        LOG.info("query =" + query.toString());
        //search the query
        Collector collector = new Collector() {

            private int docBase = 0;

            @Override
            public void setScorer(Scorer scorer) throws IOException {
            }

            @Override
            public void collect(int doc) throws IOException {
                int scoreId = doc + docBase;
                Document document = searcher.doc(scoreId);
                final String stringValue = document.getField("id").stringValue();
                int docId = Integer.parseInt(stringValue);
                LOG.fine("doc id " + stringValue + " matches the search.");
                toReturn.add(nr.get(docId, false));
            }

            @Override
            public void setNextReader(IndexReader reader, int docBase) throws IOException {
                this.docBase = docBase;
            }

            @Override
            public boolean acceptsDocsOutOfOrder() {
                return true;
            }
        };
        searcher.search(query, collector);
        searcher.close();
    } catch (ParseException ex) {
        Exceptions.printStackTrace(ex);
    } catch (CorruptIndexException ex) {
        Exceptions.printStackTrace(ex);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    } catch (IllegalStateException ex) {
        LOG.info("caught " + ex.getMessage() + ". Most likely the app is shutting down");
    }
    long delta = System.currentTimeMillis() - start;
    Installer.mbean.sampleSearchTime(delta);
    LOG.info("find took " + delta / 1000.0 + " secs. " + toReturn.size() + " results found");
    return toReturn;
}

From source file:com.scsb.crpro.lucene.SearchFiles.java

License:Apache License

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
    String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-raw] [-norms field] [-paging hitsPerPage]";
    usage += "\n\tSpecify 'false' for hitsPerPage to use streaming instead of paging search.";

    String[] aaa = { "-index", "d:\\index" };
    args = aaa;//w  w  w. j ava2 s  . co m

    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);
    }

    String index = "index";
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String normsField = null;
    boolean paging = true;
    int hitsPerPage = 3;

    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            index = args[i + 1];
            i++;
        } else if ("-field".equals(args[i])) {
            field = args[i + 1];
            i++;
        } else if ("-queries".equals(args[i])) {
            queries = args[i + 1];
            i++;
        } else if ("-repeat".equals(args[i])) {
            repeat = Integer.parseInt(args[i + 1]);
            i++;
        } else if ("-raw".equals(args[i])) {
            raw = true;
        } else if ("-norms".equals(args[i])) {
            normsField = args[i + 1];
            i++;
        } else if ("-paging".equals(args[i])) {
            if (args[i + 1].equals("false")) {
                paging = false;
            } else {
                hitsPerPage = Integer.parseInt(args[i + 1]);
                if (hitsPerPage == 0) {
                    paging = false;
                }
            }
            i++;
        }
    }

    IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)), true); // only searching, so read-only=true

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

    Searcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);

    BufferedReader in = null;
    if (queries != null) {
        in = new BufferedReader(new FileReader(queries));
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
    }
    QueryParser parser = new QueryParser(field, analyzer);
    while (true) {
        if (queries == null) // prompt the user
            System.out.println("Enter query: ");

        String line = in.readLine();

        if (line == null || line.length() == -1)
            break;

        line = line.trim();
        if (line.length() == 0)
            break;

        Query query = parser.parse(line);
        System.out.println("Searching for: " + query.toString(field));

        if (repeat > 0) { // repeat & time as benchmark
            Date start = new Date();
            for (int i = 0; i < repeat; i++) {
                searcher.search(query, null, 100);
            }
            Date end = new Date();
            System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms");
        }

        if (paging) {
            doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null);
        } else {
            doStreamingSearch(searcher, query);
        }
    }
    reader.close();
}

From source file:com.searchcode.app.service.CodeSearcher.java

License:Open Source License

/**
 * Returns the total number of documents that are present in the index at this time
 *//*from  w  ww .j  a  v a  2 s.  co  m*/
public int getTotalNumberDocumentsIndexed() {
    int numDocs = 0;
    try {
        IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(this.INDEXPATH)));

        numDocs = reader.numDocs();
        reader.close();
    } catch (Exception ex) {
        LOGGER.info(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
    }

    return numDocs;
}

From source file:com.searchcode.app.service.CodeSearcher.java

License:Open Source License

/**
 * Given a query and what page of results we are on return the matching results for that search
 *//*w w  w  .  j  av a 2  s  .co m*/
public SearchResult search(String queryString, int page) {
    SearchResult searchResult = new SearchResult();
    statsService.incrementSearchCount();

    try {
        IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(this.INDEXPATH)));
        IndexSearcher searcher = new IndexSearcher(reader);

        Analyzer analyzer = new CodeAnalyzer();

        QueryParser parser = new QueryParser(CODEFIELD, analyzer);

        Query query = parser.parse(queryString);
        LOGGER.info("Searching for: " + query.toString(CODEFIELD));
        LOGGER.searchLog(query.toString(CODEFIELD) + " " + page);

        searchResult = this.doPagingSearch(reader, searcher, query, page);
        reader.close();
    } catch (Exception ex) {
        LOGGER.warning(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
    }

    return searchResult;
}