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 syedfa <fa...@gmail.com> on 2008/07/30 15:54:00 UTC
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@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org