You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Mark Horan <Ma...@xplana.com> on 2005/09/08 16:36:33 UTC

Matching Search Terms Per Document

Hi,

Can anybody tell me how to match up query terms with appropriate
documents returned from a search? For example, in the code below I can
extract document objects from the Hits. With the Document object I can
find out, via the path field, which document filenames are implicated in
the search results. But how can I find out which search terms apply, or
matched on these documents. I've indexed a small subset of pages from
Moby Dick. If I do a single term query like "whale", I can assume this
word can be found in every document returned by Hits. But what if I do
"whale OR men"? How can I discover which term matched, or if both
matched against a document fetched from Hits? This is for the purpose of
highlighting the search terms found in the document. It seems there must
be a method supplied by Lucene for returning an array of matched terms
per document. Any suggestions?

    IndexSearcher searcher = new IndexSearcher(dir);
    Hits hits = searcher.search(query);
    System.out.println("Number of hits: " + hits.length());
    for(int i=0; i<hits.length(); i++) {
   
        Document doc = hits.doc(i);


Thanks,
Mark

Mark Horan
Web Application Developer
Xplana(TM) Learning
323 Newbury Street
Boston, MA 02115
 

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org