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

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

Introduction

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

Prototype

public Document doc(int docID) throws IOException 

Source Link

Document

Sugar for .getIndexReader().document(docID)

Usage

From source file:com.knowgate.lucene.NewsMessageSearcher.java

License:Open Source License

/**
 * Compose a Lucene query based on given parameters
 * @param sLuceneIndexPath String Base path for Lucene indexes excluding WorkArea and table name
 * @param sWorkArea String GUID of WorkArea to be searched, cannot be null
 * @param sGroup sNewsGroupCategoryName String GUID or Category Name of NewsGroup to which message belongs (optional, may be null)
 * @param sAuthor String//from   ww  w . j  ava 2s .c o  m
 * @param sTitle String
 * @param sText String
 * @param iLimit int
 * @param oSortBy Comparator
 * @return NewsMessageRecord[] An Array of NewsMessageRecord objects or <b>null</b> if no messages where found matching the given criteria
 * @throws ParseException
 * @throws IOException
 * @throws NullPointerException
 */
public static NewsMessageRecord[] search(String sLuceneIndexPath, String sWorkArea,
        String sNewsGroupCategoryName, String sAuthor, String sTitle, Date dtFromDate, Date dtToDate,
        String sText, int iLimit, Comparator oSortBy) throws ParseException, IOException, NullPointerException {

    if (null == sLuceneIndexPath)
        throw new NullPointerException("NewsMessageSearcher.search() luceneindex parameter cannot be null");

    if (null == sWorkArea)
        throw new NullPointerException("NewsMessageSearcher.search() workarea parameter cannot be null");

    if (DebugFile.trace) {
        DebugFile.writeln("Begin NewsMessageSearcher.search(" + sLuceneIndexPath + "," + sWorkArea + ","
                + sNewsGroupCategoryName + "," + sAuthor + "," + sTitle + "," + dtFromDate + "," + dtToDate
                + "," + sText + "," + String.valueOf(iLimit) + ")");
        DebugFile.incIdent();
    }

    NewsMessageRecord[] aRetArr;

    BooleanQuery oQrx = new BooleanQuery();

    oQrx.add(new TermQuery(new Term("workarea", sWorkArea)), BooleanClause.Occur.MUST);

    if (null != sNewsGroupCategoryName)
        oQrx.add(new TermQuery(new Term("container", sNewsGroupCategoryName)), BooleanClause.Occur.MUST);

    if (dtFromDate != null && dtToDate != null)
        oQrx.add(
                new TermRangeQuery("created", DateTools.dateToString(dtFromDate, DateTools.Resolution.DAY),
                        DateTools.dateToString(dtToDate, DateTools.Resolution.DAY), true, true),
                BooleanClause.Occur.MUST);
    else if (dtFromDate != null)
        oQrx.add(new TermRangeQuery("created", DateTools.dateToString(dtFromDate, DateTools.Resolution.DAY),
                null, true, false), BooleanClause.Occur.MUST);

    else if (dtToDate != null)
        oQrx.add(new TermRangeQuery("created", null, DateTools.dateToString(dtToDate, DateTools.Resolution.DAY),
                false, true), BooleanClause.Occur.MUST);

    BooleanQuery oQry = new BooleanQuery();

    if (null != sAuthor)
        oQry.add(new TermQuery(new Term("author", sAuthor)), BooleanClause.Occur.SHOULD);

    if (null != sTitle)
        oQry.add(new TermQuery(new Term("title", sTitle)), BooleanClause.Occur.SHOULD);

    if (null != sText)
        oQry.add(new TermQuery(new Term("text", escape(Gadgets.ASCIIEncode(sText).toLowerCase()))),
                BooleanClause.Occur.SHOULD);

    oQrx.add(oQry, BooleanClause.Occur.MUST);

    String sSegments = Gadgets.chomp(sLuceneIndexPath, File.separator) + "k_newsmsgs" + File.separator
            + sWorkArea;
    if (DebugFile.trace)
        DebugFile.writeln("new IndexSearcher(" + sSegments + ")");
    Directory oDir = Indexer.openDirectory(sSegments);
    IndexSearcher oSearch = new IndexSearcher(oDir);

    Document oDoc;

    if (DebugFile.trace)
        DebugFile.writeln("IndexSearcher.search(" + oQrx.toString() + ")");
    TopDocs oTopSet = oSearch.search(oQrx, null, iLimit > 0 ? iLimit : 2147483647);
    if (oTopSet.scoreDocs != null) {
        ScoreDoc[] oTopDoc = oTopSet.scoreDocs;
        final int iDocCount = oTopDoc.length <= iLimit ? oTopDoc.length : iLimit;
        aRetArr = new NewsMessageRecord[iDocCount];
        for (int d = 0; d < iDocCount; d++) {
            oDoc = oSearch.doc(oTopDoc[d].doc);
            try {
                aRetArr[d] = new NewsMessageRecord(oTopDoc[d].score, oDoc.get("workarea"), oDoc.get("guid"),
                        oDoc.get("thread"), oDoc.get("container"), oDoc.get("title"), oDoc.get("author"),
                        DateTools.stringToDate(oDoc.get("created")), oDoc.get("abstract"));
            } catch (java.text.ParseException neverthrown) {
                throw new ParseException("NewsMessageSearcher.search() Error parsing date "
                        + oDoc.get("created") + " of document " + oDoc.get("guid"));
            }
        } // next
    } else {
        aRetArr = null;
    }

    oSearch.close();
    oDir.close();

    if (oSortBy != null) {
        Arrays.sort(aRetArr, oSortBy);
    }

    if (DebugFile.trace) {
        DebugFile.decIdent();
        if (null == aRetArr)
            DebugFile.writeln("End NewsMessageSearcher.search() : no records found");
        else
            DebugFile.writeln("End NewsMessageSearcher.search() : " + String.valueOf(aRetArr.length));
    }
    return aRetArr;
}

From source file:com.leavesfly.lia.advsearching.SortingExample.java

License:Apache License

public void displayResults(Query query, Sort sort) // #1
        throws IOException {
    IndexSearcher searcher = new IndexSearcher(directory);

    searcher.setDefaultFieldSortScoring(true, false); // #2

    TopDocs results = searcher.search(query, null, // #3
            20, sort); // #3

    System.out.println("\nResults for: " + // #4
            query.toString() + " sorted by " + sort);

    System.out.println(StringUtils.rightPad("Title", 30) + StringUtils.rightPad("pubmonth", 10)
            + StringUtils.center("id", 4) + StringUtils.center("score", 15));

    PrintStream out = new PrintStream(System.out, true, "UTF-8"); // #5

    DecimalFormat scoreFormatter = new DecimalFormat("0.######");
    for (ScoreDoc sd : results.scoreDocs) {
        int docID = sd.doc;
        float score = sd.score;
        Document doc = searcher.doc(docID);
        out.println(StringUtils.rightPad( // #6
                StringUtils.abbreviate(doc.get("title"), 29), 30) + // #6
                StringUtils.rightPad(doc.get("pubmonth"), 10) + // #6
                StringUtils.center("" + docID, 4) + // #6
                StringUtils.leftPad( // #6
                        scoreFormatter.format(score), 12)); // #6
        out.println("   " + doc.get("category"));
        // out.println(searcher.explain(query, docID)); // #7
    }//from www .  jav  a 2s .c  om

    searcher.close();
}

From source file:com.leavesfly.lia.commom.TestUtil.java

License:Apache License

public static boolean hitsIncludeTitle(IndexSearcher searcher, TopDocs hits, String title) throws IOException {
    for (ScoreDoc match : hits.scoreDocs) {
        Document doc = searcher.doc(match.doc);
        if (title.equals(doc.get("title"))) {
            return true;
        }//from  w w  w .  jav a 2 s.co m
    }
    System.out.println("title '" + title + "' not found");
    return false;
}

From source file:com.leavesfly.lia.commom.TestUtil.java

License:Apache License

public static void dumpHits(IndexSearcher searcher, TopDocs hits) throws IOException {
    if (hits.totalHits == 0) {
        System.out.println("No hits");
    }/*from  w  w  w .j  av a  2s.  c  om*/

    for (ScoreDoc match : hits.scoreDocs) {
        Document doc = searcher.doc(match.doc);
        System.out.println(match.score + ":" + doc.get("title"));
    }
}

From source file:com.leavesfly.lia.extsearch.payloads.PayloadsTest.java

License:Apache License

public void testPayloadTermQuery() throws Throwable {
    addDoc("Hurricane warning",
            "Bulletin: A hurricane warning was issued at " + "6 AM for the outer great banks");
    addDoc("Warning label maker", "The warning label maker is a delightful toy for "
            + "your precocious seven year old's warning needs");
    addDoc("Tornado warning",
            "Bulletin: There is a tornado warning for " + "Worcester county until 6 PM today");

    IndexReader r = writer.getReader();/*from w ww .j  av a 2 s  .  c o m*/
    writer.close();

    IndexSearcher searcher = new IndexSearcher(r);

    searcher.setSimilarity(new BoostingSimilarity());

    Term warning = new Term("contents", "warning");

    Query query1 = new TermQuery(warning);
    System.out.println("\nTermQuery results:");
    TopDocs hits = searcher.search(query1, 10);
    TestUtil.dumpHits(searcher, hits);

    assertEquals("Warning label maker", // #B
            searcher.doc(hits.scoreDocs[0].doc).get("title")); // #B

    Query query2 = new PayloadTermQuery(warning, new AveragePayloadFunction());
    System.out.println("\nPayloadTermQuery results:");
    hits = searcher.search(query2, 10);
    TestUtil.dumpHits(searcher, hits);

    assertEquals("Warning label maker", // #C
            searcher.doc(hits.scoreDocs[2].doc).get("title")); // #C
    r.close();
    searcher.close();
}

From source file:com.leavesfly.lia.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, // 4
            "contents", //4
            new StandardAnalyzer( //4
                    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  av  a2s. com

    is.close(); //9
}

From source file:com.leavesfly.lia.searching.BasicSearchingTest.java

License:Apache License

public void testQueryParser() throws Exception {
    Directory dir = TestUtil.getBookIndexDirectory();
    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 ww  . j  a v a 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();
}

From source file:com.leavesfly.lia.searching.Explainer.java

License:Apache License

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        System.err.println("Usage: Explainer <index dir> <query>");
        System.exit(1);//w  w  w.  j  a v a 2  s .  co m
    }

    String indexDir = args[0];
    String queryExpression = args[1];

    Directory directory = FSDirectory.open(new File(indexDir));
    QueryParser parser = new QueryParser(Version.LUCENE_30, "contents", new SimpleAnalyzer());
    Query query = parser.parse(queryExpression);

    System.out.println("Query: " + queryExpression);

    IndexSearcher searcher = new IndexSearcher(directory);
    TopDocs topDocs = searcher.search(query, 10);

    for (ScoreDoc match : topDocs.scoreDocs) {
        Explanation explanation = searcher.explain(query, match.doc); //#A

        System.out.println("----------");
        Document doc = searcher.doc(match.doc);
        System.out.println(doc.get("title"));
        System.out.println(explanation.toString()); //#B
    }
    searcher.close();
    directory.close();
}

From source file:com.leavesfly.lia.tool.HighlightTest.java

License:Apache License

public void testHits() throws Exception {
    IndexSearcher searcher = new IndexSearcher(TestUtil.getBookIndexDirectory());
    TermQuery query = new TermQuery(new Term("title", "action"));
    TopDocs hits = searcher.search(query, 10);

    QueryScorer scorer = new QueryScorer(query, "title");
    Highlighter highlighter = new Highlighter(scorer);
    highlighter.setTextFragmenter(new SimpleSpanFragmenter(scorer));

    Analyzer analyzer = new SimpleAnalyzer();

    for (ScoreDoc sd : hits.scoreDocs) {
        Document doc = searcher.doc(sd.doc);
        String title = doc.get("title");

        TokenStream stream = TokenSources.getAnyTokenStream(searcher.getIndexReader(), sd.doc, "title", doc,
                analyzer);//ww  w  .  j  a  v a2  s  .  co  m
        String fragment = highlighter.getBestFragment(stream, title);

        System.out.println(fragment);
    }
}

From source file:com.leavesfly.lia.tool.SpatialLuceneExample.java

License:Apache License

public void findNear(String what, double latitude, double longitude, double radius)
        throws CorruptIndexException, IOException {
    IndexSearcher searcher = new IndexSearcher(directory);

    DistanceQueryBuilder dq;/*  w ww .ja  v a  2  s.  co m*/
    dq = new DistanceQueryBuilder(latitude, // #A
            longitude, // #A
            radius, // #A
            latField, // #A
            lngField, // #A
            tierPrefix, // #A
            true); // #A

    Query tq;
    if (what == null)
        tq = new TermQuery(new Term("metafile", "doc")); // #B
    else
        tq = new TermQuery(new Term("name", what));

    DistanceFieldComparatorSource dsort; // #C
    dsort = new DistanceFieldComparatorSource( // #C
            dq.getDistanceFilter()); // #C
    Sort sort = new Sort(new SortField("foo", dsort)); // #C

    TopDocs hits = searcher.search(tq, dq.getFilter(), 10, sort);

    Map<Integer, Double> distances = // #D
            dq.getDistanceFilter().getDistances(); // #D

    System.out.println("Number of results: " + hits.totalHits);
    System.out.println("Found:");
    for (ScoreDoc sd : hits.scoreDocs) {
        int docID = sd.doc;
        Document d = searcher.doc(docID);

        String name = d.get("name");
        double rsLat = NumericUtils.prefixCodedToDouble(d.get(latField));
        double rsLng = NumericUtils.prefixCodedToDouble(d.get(lngField));
        Double geo_distance = distances.get(docID);

        System.out.printf(name + ": %.2f Miles\n", geo_distance);
        System.out.println("\t\t(" + rsLat + "," + rsLng + ")");
    }
}