mirror of https://github.com/apache/lucene.git
LUCENE-1114: Updated example
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@608126 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0ae1e1a905
commit
ed893f770c
|
@ -10,20 +10,35 @@ Fragmenter, FragmentScorer, Formatter classes.
|
||||||
<h2>Example Usage</h2>
|
<h2>Example Usage</h2>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
IndexSearcher searcher = new IndexSearcher(ramDir);
|
//... Above, create documents with two fields, one with term vectors (tv) and one without (notv)
|
||||||
Query query = QueryParser.parse("Kenne*", FIELD_NAME, analyzer);
|
IndexSearcher searcher = new IndexSearcher(directory);
|
||||||
query = query.rewrite(reader); //required to expand search terms
|
QueryParser parser = new QueryParser("notv", analyzer);
|
||||||
Hits hits = searcher.search(query);
|
Query query = parser.parse("million");
|
||||||
|
//query = query.rewrite(reader); //required to expand search terms
|
||||||
|
Hits hits = searcher.search(query);
|
||||||
|
|
||||||
Highlighter highlighter = new Highlighter(this, new QueryScorer(query));
|
SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter();
|
||||||
for (int i = 0; i < hits.length(); i++)
|
Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));
|
||||||
{
|
for (int i = 0; i < 10; i++) {
|
||||||
String text = hits.doc(i).get(FIELD_NAME);
|
String text = hits.doc(i).get("notv");
|
||||||
TokenStream tokenStream = analyzer.tokenStream(FIELD_NAME, new StringReader(text));
|
TokenStream tokenStream = TokenSources.getAnyTokenStream(searcher.getIndexReader(), hits.id(i), "notv", analyzer);
|
||||||
// Get 3 best fragments and seperate with a "..."
|
TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, text, false, 10);//highlighter.getBestFragments(tokenStream, text, 3, "...");
|
||||||
String result = highlighter.getBestFragments(tokenStream, text, 3, "...");
|
for (int j = 0; j < frag.length; j++) {
|
||||||
System.out.println(result);
|
if ((frag[j] != null) && (frag[j].getScore() > 0)) {
|
||||||
}
|
System.out.println((frag[j].toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Term vector
|
||||||
|
text = hits.doc(i).get("tv");
|
||||||
|
tokenStream = TokenSources.getAnyTokenStream(searcher.getIndexReader(), hits.id(i), "tv", analyzer);
|
||||||
|
frag = highlighter.getBestTextFragments(tokenStream, text, false, 10);
|
||||||
|
for (int j = 0; j < frag.length; j++) {
|
||||||
|
if ((frag[j] != null) && (frag[j].getScore() > 0)) {
|
||||||
|
System.out.println((frag[j].toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("-------------");
|
||||||
|
}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<h2>New features 06/02/2005</h2>
|
<h2>New features 06/02/2005</h2>
|
||||||
|
|
Loading…
Reference in New Issue