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

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

Introduction

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

Prototype

public IndexSearcher(IndexReaderContext context) 

Source Link

Document

Creates a searcher searching the provided top-level IndexReaderContext .

Usage

From source file:LuceneSearchFiles.java

License:Apache License

/** Simple command-line based search demo. */
public static void search(String phrase, String field, int hitsPerPage) {
    try {/*  w  w  w  . j a v  a  2s.c o m*/
        IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(m_index)));
        IndexSearcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);

        QueryParser parser = new QueryParser(Version.LUCENE_40, field, analyzer);

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

        searcher.search(query, null, hitsPerPage);

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

        for (ScoreDoc hit : hits) {
            Document doc = searcher.doc(hit.doc);
            //String path = doc.get("path");   
            String title = doc.get("title");
            System.out.println(hit.score + " -" + title);
        }
        reader.close();
    } catch (IOException e) {

    } catch (ParseException e) {

    }
}

From source file:TfIdfViewer.java

License:Apache License

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
    String usage = "Usage:\tjava QueryConvert [-index dir]";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);// w w  w  .  j  av a2s . c o  m
    }

    String index = "index";
    String field = "contents";
    String queries = null;
    String queryString = null;

    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++;
        }
    }

    // create a reader and a searcher for the index
    IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)));
    IndexSearcher searcher = new IndexSearcher(reader);

    // create the reader from where we'll read filenames
    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"));
    }

    while (true) {

        // get two filenames
        System.out.println("Enter filename 1 (or hit <RETURN>): ");
        String f1 = in.readLine();
        if (f1 == null || f1.length() == -1)
            break;
        f1 = f1.trim();
        if (f1.length() == 0)
            break;

        System.out.println("Enter filename 2: ");
        String f2 = in.readLine();

        // get the docId's of the two filenames in the index
        int id1 = findDocId(searcher, f1);
        if (id1 < 0) {
            System.out.println("No file " + f1 + " found in index!");
            break;
        }
        int id2 = findDocId(searcher, f2);
        if (id1 < 0) {
            System.out.println("No file " + f1 + " found in index!");
            break;
        }

        // convert them to tf-idf format
        TermWeight[] v1 = toTfIdf(reader, id1);
        TermWeight[] v2 = toTfIdf(reader, id2);

        // print them out,
        System.out.println("-----------------------");
        printTermWeightVector(v1);
        System.out.println("-----------------------");
        printTermWeightVector(v2);
        System.out.println("-----------------------");

        // and print their cosine similarity
        System.out.println("The cosine similarity of the two files is: " + cosineSimilarity(v1, v2));

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

From source file:action.indexing.IndexingTest.java

License:Apache License

protected int getHitCount(String fieldName, String searchString) throws IOException {
    IndexSearcher searcher = new IndexSearcher(directory); //4
    Term t = new Term(fieldName, searchString);
    Query query = new TermQuery(t); //5
    int hitCount = TestUtil.hitCount(searcher, query); //6
    searcher.close();//from  ww  w.  j a  v a  2  s  .  c o  m
    return hitCount;
}

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. ja  va  2  s  .c o  m*/
}

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   w  w w .j  a  v  a 2s .  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();/*w w w  .  ja  v  a  2s .  co m*/
    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();//  w  ww .  j  ava  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);/*from   w w  w  . j  av a 2 s .c o  m*/
    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();
}

From source file:al.franzis.lucene.header.search.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/java/4_0/demo.html for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);/*w  w  w .  ja  va  2  s.c o m*/
    }

    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++;
        }
    }

    IndexSearcher searcher = new IndexSearcher(FSDirectory.open(new File(index)));
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);

    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"));
    }
    QueryParser parser = new QueryParser(Version.LUCENE_31, 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);
        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");
        }

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

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

From source file:antnlp.opie.indexsearch.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);/*from  w  w w .  ja v  a2 s . c  o m*/
    }

    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++;
        }
    }

    IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    //Analyzer analyzer = new StandardAnalyzer();
    //Analyzer analyzer = new StandardAnalyzer(CharArraySet.EMPTY_SET);
    Analyzer analyzer = new WhitespaceAnalyzer();

    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);
    QueryBuilder builder = new QueryBuilder(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);
        Query query = builder.createPhraseQuery(field, 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, 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();
}