Example usage for org.apache.lucene.search IndexSearcher search

List of usage examples for org.apache.lucene.search IndexSearcher search

Introduction

In this page you can find the example usage for org.apache.lucene.search IndexSearcher search.

Prototype

public <C extends Collector, T> T search(Query query, CollectorManager<C, T> collectorManager)
        throws IOException 

Source Link

Document

Lower-level search API.

Usage

From source file:ReadFiles.java

License:Apache License

public static Result doSearch(String path, DIRTYPE type, IndexReader ir) throws IOException {
    IndexReader reader;/*from w  ww. j  a  va 2 s.c  o m*/
    Result r = new Result();
    long beginTs, endTs;

    if (ir != null)
        reader = ir;
    else {
        beginTs = System.currentTimeMillis();
        switch (type) {
        default:
        case MMAP:
            reader = DirectoryReader.open(MMapDirectory.open(new File(path)));
            break;
        case NIO:
            reader = DirectoryReader.open(NIOFSDirectory.open(new File(path)));
            break;
        case SIMPLE:
            reader = DirectoryReader.open(SimpleFSDirectory.open(new File(path)));
            break;
        }
        endTs = System.currentTimeMillis();
        r.initTs += endTs - beginTs;
        r.initTsNr += 1;
    }

    System.out.println("-----Search it------");

    IndexSearcher searcher = new IndexSearcher(reader);

    Query q = NumericRangeQuery.newIntRange("foo", new Integer("100000"), null, false, false);
    beginTs = System.currentTimeMillis();
    ScoreDoc[] hits = searcher.search(q, searcher.getIndexReader().maxDoc()).scoreDocs;
    endTs = System.currentTimeMillis();
    r.searchTs += endTs - beginTs;
    r.searchTsNr += hits.length;
    System.out.println("Hits -> " + hits.length);

    boolean isSeq = true;
    int lastid = 0;
    beginTs = System.currentTimeMillis();
    for (int i = 0; i < hits.length; i++) {
        if (hits[i].doc < lastid)
            isSeq = false;
        Document doc = searcher.doc(hits[i].doc);
        doc.get("foo");
        doc.get("bar");
        //System.out.println("Key: " + doc.get("foo") + ", Value: " + doc.get("bar"));
    }
    System.out.println("Search DocID is SEQ? " + isSeq);
    endTs = System.currentTimeMillis();
    r.fetchTs += endTs - beginTs;
    r.fetchTsNr += hits.length;

    if (ir == null) {
        beginTs = System.currentTimeMillis();
        reader.close();
        endTs = System.currentTimeMillis();
        r.closeTs += endTs - beginTs;
        r.closeTsNr += 1;
    }

    return r;
}

From source file:IndexAndSearchOpenStreetMaps1D.java

License:Apache License

private static void queryIndex() throws IOException {
    Directory dir = FSDirectory.open(Paths.get("/l/tmp/1dkd" + (USE_NF ? "_nf" : "")));
    System.out.println("DIR: " + dir);
    IndexReader r = DirectoryReader.open(dir);
    System.out.println("maxDoc=" + r.maxDoc());

    IndexSearcher s = new IndexSearcher(r);

    //System.out.println("reader MB heap=" + (reader.ramBytesUsed()/1024/1024.));

    // London, UK:
    int STEPS = 5;
    double MIN_LAT = 51.0919106;
    double MAX_LAT = 51.6542719;
    double MIN_LON = -0.3867282;
    double MAX_LON = 0.8492337;
    byte[] scratch1 = new byte[4];
    byte[] scratch2 = new byte[4];
    for (int iter = 0; iter < 100; iter++) {
        long tStart = System.nanoTime();
        long totHits = 0;
        int queryCount = 0;
        for (int latStep = 0; latStep < STEPS; latStep++) {
            double lat = MIN_LAT + latStep * (MAX_LAT - MIN_LAT) / STEPS;
            for (int lonStep = 0; lonStep < STEPS; lonStep++) {
                double lon = MIN_LON + lonStep * (MAX_LON - MIN_LON) / STEPS;
                for (int latStepEnd = latStep + 1; latStepEnd <= STEPS; latStepEnd++) {
                    double latEnd = MIN_LAT + latStepEnd * (MAX_LAT - MIN_LAT) / STEPS;
                    for (int lonStepEnd = lonStep + 1; lonStepEnd <= STEPS; lonStepEnd++) {
                        double lonEnd = MIN_LON + lonStepEnd * (MAX_LON - MIN_LON) / STEPS;

                        Query q;//from   ww w  .  j a  v a  2  s.  c o m
                        if (USE_NF) {
                            q = LegacyNumericRangeQuery.newIntRange("latnum", (int) (1000000. * lat),
                                    (int) (1000000. * latEnd), true, true);
                        } else {
                            q = IntPoint.newRangeQuery("lat", (int) (1000000. * lat),
                                    (int) (1000000. * latEnd));
                        }

                        TotalHitCountCollector c = new TotalHitCountCollector();
                        //long t0 = System.nanoTime();
                        s.search(q, c);

                        //System.out.println("\nITER: now query lat=" + lat + " latEnd=" + latEnd + " lon=" + lon + " lonEnd=" + lonEnd);
                        //Bits hits = reader.intersect(lat, latEnd, lon, lonEnd);
                        //System.out.println("  total hits: " + hitCount);
                        //totHits += ((FixedBitSet) hits).cardinality();
                        //System.out.println("  add tot " + c.getTotalHits());
                        totHits += c.getTotalHits();
                        queryCount++;
                    }
                }
            }
        }

        long tEnd = System.nanoTime();
        System.out.println("ITER: " + iter + " " + ((tEnd - tStart) / 1000000000.0) + " sec; totHits=" + totHits
                + "; " + queryCount + " queries");

        if (iter == 0) {
            long bytes = 0;
            for (LeafReaderContext ctx : r.leaves()) {
                CodecReader cr = (CodecReader) ctx.reader();
                System.out.println(Accountables.toString(cr));
                bytes += cr.ramBytesUsed();
            }
            System.out.println("READER MB: " + (bytes / 1024. / 1024.));
            System.out.println("RAM: " + Accountables.toString((Accountable) r.leaves().get(0).reader()));
        }
    }

    IOUtils.close(r, dir);
}

From source file:SearchFilesTest.java

License:Apache License

public static void doPagingSearch(BufferedReader in, IndexSearcher searcher, Query query) throws IOException {

    TopDocs results = searcher.search(query, 100);
    ScoreDoc[] hits = results.scoreDocs;

    int numTotalHits = results.totalHits;
    System.out.println(numTotalHits + " total matching documents");

    for (int i = 0; i < hits.length; i++) {
        Document doc = searcher.doc(hits[i].doc);
        String path = doc.get("path");
        if (path != null) {
            System.out.println((i + 1) + ". " + path + " score = " + hits[i].score);
            String title = doc.get("title");
            if (title != null) {
                System.out.println("   Title: " + doc.get("title"));
            }//from  w  ww . j  av  a  2  s  .co m

        } else {
            System.out.println((i + 1) + ". " + "No path for this document");
        }
    }
}

From source file:luceneInterface.java

License:Apache License

public static List<Document> query(String index, String stoppath, String question, int numResult, String sim)
        throws Exception {
    IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
    IndexSearcher searcher = new IndexSearcher(reader);

    Analyzer analyzer = new EnglishAnalyzer(StopFilter.makeStopSet(mygetStopwords(stoppath)));

    if (sim.equals("TFIDF"))
        searcher.setSimilarity(new ClassicSimilarity());
    else if (sim.equals("BM25"))
        searcher.setSimilarity(new BM25Similarity());
    else//from w  w  w .ja  va  2s  .co m
        searcher.setSimilarity(new BM25Similarity());

    String field = "contents";
    QueryParser parser = new QueryParser(field, analyzer);
    Query query = parser.parse(parser.escape(question));

    BooleanQuery.Builder bqb = new BooleanQuery.Builder();
    bqb.add(new TermQuery(new Term("contents", parser.escape(question))), BooleanClause.Occur.SHOULD);
    bqb.add(new TermQuery(new Term("sec", parser.escape(question))), BooleanClause.Occur.SHOULD);

    //        Term term = new Term(field, question);
    //        Query query = new TermQuery(term);

    //        TopDocs results = searcher.search(query, numResult);
    TopDocs results = searcher.search(parser.parse(bqb.build().toString()), numResult);

    ScoreDoc[] hits = results.scoreDocs;
    List<Document> docs = new ArrayList<Document>();

    int numTotalHits = results.totalHits;
    //        System.out.println(numTotalHits + " total matching documents");

    int end = Math.min(numTotalHits, numResult);

    String searchResult = "";
    //        System.out.println("Only results 1 - " + hits.length);

    for (int i = 0; i < end; i++) {
        Document doc = searcher.doc(hits[i].doc);
        docs.add(doc);
    }

    return docs;
}

From source file:TfIdfViewer.java

License:Apache License

private static int findDocId(IndexSearcher searcher, String filename) throws Exception {
    Term t = new Term("path", filename);
    Query q = new TermQuery(t);
    TopDocs td = searcher.search(q, 2); // get a list of docs matching the query
    if (td.totalHits < 1)
        return -1; // no hits found
    else/*  ww  w . j a v  a2 s.  c  o m*/
        return td.scoreDocs[0].doc; // returns first matching docId
}

From source file:action.meetlucene.Fragments.java

License:Apache License

public static void simpleSearch() throws IOException {
    Directory dir = FSDirectory.open(new File("index"));
    IndexSearcher searcher = new IndexSearcher(dir);
    Query q = new TermQuery(new Term("contents", "licenses"));
    TopDocs hits = searcher.search(q, 10);
    System.out.println(hits.scoreDocs.length);
    searcher.close();/*from w  w w . j  ava  2s. c om*/
}

From source file:action.meetlucene.Searcher.java

License:Apache License

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

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

    QueryParser parser = new QueryParser(Version.LUCENE_30, "contents",
            new StandardAnalyzer(Version.LUCENE_30)); //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  
    }//from   ww  w . j  a v a2 s .  c  o  m

    is.close(); //9
}

From source file:action.searching.BasicSearchingTest.java

License:Apache License

public void testTerm() throws Exception {
    Directory dir = FSDirectory.open(new File(System.getProperty("index.dir"))); //A
    IndexSearcher searcher = new IndexSearcher(dir); //B

    Term t = new Term("subject", "ant");
    Query query = new TermQuery(t);
    TopDocs docs = searcher.search(query, 10);
    assertEquals("Ant in Action", //C
            1, docs.totalHits); //C

    t = new Term("subject", "junit");
    docs = searcher.search(new TermQuery(t), 10);
    assertEquals("Ant in Action, " + //D
            "JUnit in Action, Second Edition", //D
            2, docs.totalHits); //D

    searcher.close();//from  ww  w .  j a  v  a2 s .c om
    dir.close();
}

From source file:action.searching.BasicSearchingTest.java

License:Apache License

public void testKeyword() throws Exception {
    Directory dir = FSDirectory.open(new File(System.getProperty("index.dir")));
    IndexSearcher searcher = new IndexSearcher(dir);

    Term t = new Term("isbn", "9781935182023");
    Query query = new TermQuery(t);
    TopDocs docs = searcher.search(query, 10);
    assertEquals("JUnit in Action, Second Edition", 1, docs.totalHits);

    searcher.close();/*from  w w  w. j a v a  2  s  .  c  o m*/
    dir.close();
}

From source file:action.searching.BasicSearchingTest.java

License:Apache License

public void testQueryParser() throws Exception {
    Directory dir = FSDirectory.open(new File(System.getProperty("index.dir")));
    IndexSearcher searcher = new IndexSearcher(dir);

    QueryParser parser = new QueryParser(Version.LUCENE_30, //A
            "contents", //A
            new SimpleAnalyzer()); //A

    Query query = parser.parse("+JUNIT +ANT -MOCK"); //B
    TopDocs docs = searcher.search(query, 10);
    assertEquals(1, docs.totalHits);/*w w w  . java  2  s. c  om*/
    Document d = searcher.doc(docs.scoreDocs[0].doc);
    assertEquals("Ant in Action", d.get("title"));

    query = parser.parse("mock OR junit"); //B
    docs = searcher.search(query, 10);
    assertEquals("Ant in Action, " + "JUnit in Action, Second Edition", 2, docs.totalHits);

    searcher.close();
    dir.close();
}