lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark Horan" <>
Subject Matching Search Terms Per Document
Date Thu, 08 Sep 2005 14:36:33 GMT

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 =;
    System.out.println("Number of hits: " + hits.length());
    for(int i=0; i<hits.length(); i++) {
        Document doc = hits.doc(i);


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

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message