Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Lucene: Java-User

Highlighting results returned from MultiFieldQueryParser

 

 

Lucene java-user RSS feed   Index | Next | Previous | View Threaded


fayyazuddin at gmail

Jul 30, 2008, 6:54 AM

Post #1 of 1 (202 views)
Permalink
Highlighting results returned from MultiFieldQueryParser

Dear fellow Lucene/Java developers:

I have an index created from an XML file which I am trying to search using
the MultiFieldQueryParser. At present, I am using the QueryParser to
successfully return results that are highlighted. The code is listed here:

public List search(File indexDir, String q) throws Exception {

List searchResult = new ArrayList();
Directory fsDir=FSDirectory.getDirectory(indexDir);
IndexSearcher is=new IndexSearcher(fsDir);

Analyzer analyser = new StandardAnalyzer();
Query parser=new QueryParser("LINES", analyser).parse(q);
long start=new Date().getTime();
Hits hits=is.search(parser);
long end=new Date().getTime();
QueryScorer scorer = new QueryScorer(parser);
SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("", "");
Highlighter highlighter = new Highlighter(formatter, scorer);
Highlighter high = new Highlighter(formatter, scorer);
Fragmenter fragmenter = new NullFragmenter();
Fragmenter fragment = new SimpleFragmenter(250);
highlighter.setTextFragmenter(fragmenter);
high.setTextFragmenter(fragment);

for(int i=0; i<hits.length(); i++){
Document doc=hits.doc(i);
String lns = doc.get("LINES");
TokenStream lines = analyser.tokenStream("LINES", new
StringReader(lns));
CachingTokenFilter filter = new CachingTokenFilter(lines);
String highlightedLines = highlighter.getBestFragment(filter,
lns);
filter.reset();
String highlight = high.getBestFragment(filter, lns);
SearchResult resultBean = new SearchResult();
resultBean.setReference(hits.doc(i).get("REFERENCE"));
resultBean.setNarrator(hits.doc(i).get("SPEAKER"));
resultBean.setHitResult(highlight);
resultBean.setQuote(highlightedLines);
searchResult.add(resultBean);
System.out.println(resultBean.getReference());
System.out.println(resultBean.getNarrator());
System.out.println(resultBean.getHitResult());
System.out.println("");
System.out.println(resultBean.getQuote());
System.out.println("");
}

System.err.println("Found " + hits.length() + " document(s)(in " +
(end-start) + " milliseconds) that matched query '" + q + "':");

return searchResult;
}

Currently, I have an XML element which I am searching called <LINES>. Using
the MultiFieldQueryParser, I want the user to be able to search the <LINES>
and <SCENE-COMMENTARY> elements. The results found within the
<SCENE-COMMENTARY> element I wish to store in a separate javaBean object
that has only two attributes, get/setResult and get/setReference, and wish
to present the results to the user in order of best match (which is what it
is doing at present). How would I achieve the same results when I am using
the MultiFieldQueryParser class? Would I do anything different when trying
to return highlighted results from a wildcard search or a fuzzy search?

Thanks to everyone who replies.
Sincerely;
Fayyaz
--
View this message in context: http://www.nabble.com/Highlighting-results-returned-from-MultiFieldQueryParser-tp18733752p18733752.html
Sent from the Lucene - Java Users mailing list archive at Nabble.com.


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

Lucene java-user RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.