Example usage for org.apache.lucene.index DirectoryReader open

List of usage examples for org.apache.lucene.index DirectoryReader open

Introduction

In this page you can find the example usage for org.apache.lucene.index DirectoryReader open.

Prototype

public static DirectoryReader open(final IndexCommit commit) throws IOException 

Source Link

Document

Expert: returns an IndexReader reading the index in the given IndexCommit .

Usage

From source file:BlockBuilding.AbstractBlockBuilding.java

License:Apache License

public static IndexReader openReader(Directory directory) {
    try {/* w w w .j  a  v  a 2 s .  c om*/
        return DirectoryReader.open(directory);
    } catch (IOException ex) {
        LOGGER.log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:BlockBuilding.Utilities.java

License:Open Source License

public static IndexReader openReader(Directory directory) {
    try {/*from w w  w.  j a  v  a  2s . c  o m*/
        return DirectoryReader.open(directory);
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:book.Searcher.java

License:Apache License

public static void search(String indexDir, String q) throws IOException, ParseException {

    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexDir).toPath()));
    IndexSearcher is = new IndexSearcher(reader);

    //alt: Directory dir = FSDirectory.open(new File(indexDir)); // 3
    //alt: IndexSearcher is = IndexSearcher(dir); // 3

    QueryParser parser = new QueryParser("contents", // 4
            new StandardAnalyzer()); // 4
    Query query = parser.parse(q); // 4
    long start = System.currentTimeMillis();
    TopDocs hits = is.search(query, 10); // 5
    long end = System.currentTimeMillis();

    System.err.println("Found " + hits.totalHits + // 6
            " document(s) (in " + (end - start) + // 6
            " milliseconds) that matched query '" + // 6
            q + "':"); // 6

    for (ScoreDoc scoreDoc : hits.scoreDocs) {
        Document doc = is.doc(scoreDoc.doc); // 7
        System.out.println(doc.get("fullpath")); // 8
    }//  ww w . j a v  a  2 s . c o m

    //was: is.close(); // 9
}

From source file:bostoncase.widgets.SearchFiles.java

License:Apache License

private static void init(String index) throws IOException {
    reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
    searcher = new IndexSearcher(reader);
    analyzer = new StandardAnalyzer();
    parser = new QueryParser(field, analyzer);
}

From source file:bostoncase.widgets.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] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);/*w ww.j a v a  2 s . c o  m*/
    }

    String index = "/Users/michaelhundt/Documents/Meine/Studium/MASTER/MasterProject/data/lucene_index/";
    String field = "content";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = null;
    int hitsPerPage = 10;

    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 ("-query".equals(args[i])) {
            queryString = 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 ("-paging".equals(args[i])) {
            hitsPerPage = Integer.parseInt(args[i + 1]);
            if (hitsPerPage <= 0) {
                System.err.println("There must be at least 1 hit per page.");
                System.exit(1);
            }
            i++;
        }
    }

    IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
    //      IndexReader reader = StandardDirectoryReader.open(FSDirectory.open(Paths.get(index)));

    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer();

    BufferedReader in = null;
    if (queries != null) {
        in = Files.newBufferedReader(Paths.get(queries), StandardCharsets.UTF_8);
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
    }
    QueryParser parser = new QueryParser(field, analyzer);
    while (true) {
        if (queries == null && queryString == null) { // prompt the user
            System.out.println("Enter query: ");
        }

        String line = queryString != null ? queryString : in.readLine();

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

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

        Query query = parser.parse(line);
        if (line.equals("geo")) {
            //            query = new GeoPointInBBoxQuery("geo", 42.2279, 42.3969, -70.9235, -71.1908);
            //            query = new GeoPointInBBoxQuery("geo", -71.1908, -70.9235, 42.2279, 42.3969);
            query = new GeoPointInBBoxQuery("geo", 42.2279, 42.3969, -71.1908, -70.9235);
        }

        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, 100);
            }
            Date end = new Date();
            System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms");
        }

        doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);

        if (queryString != null) {
            break;
        }
    }
    reader.close();
}

From source file:br.andrew.lucene.testing.SearchFiles.java

License:Apache License

/** Simple command-line based search demo. */
public static void main(final String[] args) throws Exception {
    final String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);/* ww w  .  j  a  v  a 2s  .  c  om*/
    }

    String index = "index";
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = null;
    int hitsPerPage = 10;

    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 ("-query".equals(args[i])) {
            queryString = 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 ("-paging".equals(args[i])) {
            hitsPerPage = Integer.parseInt(args[i + 1]);
            if (hitsPerPage <= 0) {
                System.err.println("There must be at least 1 hit per page.");
                System.exit(1);
            }
            i++;
        }
    }

    final IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index)));
    final IndexSearcher searcher = new IndexSearcher(reader);
    final Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);

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

        String line = queryString != null ? queryString : in.readLine();

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

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

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

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

        SearchFiles.doPagingSearch(in, searcher, query, hitsPerPage, raw,
                queries == null && queryString == null);

        if (queryString != null) {
            break;
        }
    }
    reader.close();
}

From source file:br.bireme.ngrams.CompareResults.java

private static void compare(final String resultFile, final String indexPath, final String outputFile,
        final String encoding) throws IOException {
    assert resultFile != null;
    assert indexPath != null;
    assert outputFile != null;
    assert encoding != null;

    try (DirectoryReader ireader = DirectoryReader.open(new MMapDirectory(new File(indexPath).toPath()))) {
        final IndexSearcher isearcher = new IndexSearcher(ireader);

        try (BufferedReader breader = Files.newBufferedReader(new File(resultFile).toPath(),
                Charset.forName(encoding));
                BufferedWriter bwriter = Files.newBufferedWriter(new File(outputFile).toPath(),
                        Charset.forName(encoding))) {
            boolean first = true; // first line

            while (true) {
                final String line = breader.readLine();
                if (line == null)
                    break;
                if (first) {
                    first = false; // drop header line (first)
                } else {
                    final String line2 = line.trim();
                    if (!line2.isEmpty()) {
                        final String[] split = line2.split("\\|");
                        checkDocs(split[1], split[2], split[3], isearcher, bwriter);
                    }//from   ww w. j a v  a 2 s.  c om
                }
            }
        }
    }
}

From source file:br.bireme.ngrams.NGIndex.java

private IndexSearcher getIndexSearcher(final String indexPath) throws IOException {

    final DirectoryReader ireader = DirectoryReader.open(
            //FSDirectory.open(new File(indexPath).toPath()));
            new MMapDirectory(new File(indexPath).toPath()));
    //new RAMDirectory(FSDirectory.open(new File(indexPath).toPath()), IOContext.DEFAULT));
    //new RAMDirectory(FSDirectory.open(new File(indexPath).toPath()), IOContext.READONCE));

    return new IndexSearcher(ireader);
}

From source file:br.bireme.ngrams.TestIndex.java

public static boolean test(final String indexDir, final String schemaFile, final String schemaEncoding)
        throws IOException, ParserConfigurationException, SAXException {

    final NGSchema schema = new NGSchema("", schemaFile, schemaEncoding);
    final FSDirectory directory = FSDirectory.open(new File(indexDir).toPath());
    final DirectoryReader ireader = DirectoryReader.open(directory);

    return test(ireader, schema);
}

From source file:br.bireme.ngrams.Tools.java

public static void showTerms(final String indexName, final String fieldName) throws IOException {
    if (indexName == null) {
        throw new NullPointerException("indexName");
    }//from   w w  w . j a v  a 2  s. c om
    if (fieldName == null) {
        throw new NullPointerException("fieldName");
    }
    try (Directory directory = FSDirectory.open(new File(indexName).toPath())) {
        final DirectoryReader ireader = DirectoryReader.open(directory);
        final List<LeafReaderContext> leaves = ireader.leaves();
        if (leaves.isEmpty()) {
            throw new IOException("empty leaf readers list");
        }
        final Terms terms = leaves.get(0).reader().terms(fieldName);
        /*final Terms terms = SlowCompositeReaderWrapper.wrap(ireader)
            .terms(fieldName);*/
        if (terms != null) {
            final TermsEnum tenum = terms.iterator();
            int pos = 0;
            // PostingsEnum penum = null;

            while (true) {
                final BytesRef br = tenum.next();
                if (br == null) {
                    break;
                }
                System.out.println((++pos) + ") term=[" + br.utf8ToString() + "] ");
                /*
                penum = tenum.postings(penum, PostingsEnum.OFFSETS);
                while (penum.nextDoc() != PostingsEnum.NO_MORE_DOCS) {
                System.out.print(" startOffset=" + penum.startOffset());
                System.out.println(" endOffset:" + penum.endOffset());
                }
                */
            }
        }
    }
}