pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / highlighter / src / java / org / apache / lucene / search / highlight / package.html
1 <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
2 <!--
3  Licensed to the Apache Software Foundation (ASF) under one or more
4  contributor license agreements.  See the NOTICE file distributed with
5  this work for additional information regarding copyright ownership.
6  The ASF licenses this file to You under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with
8  the License.  You may obtain a copy of the License at
9
10      http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 -->
18 <html>
19 <body>
20
21 The highlight package contains classes to provide "keyword in context" features
22 typically used to highlight search terms in the text of results pages.
23 The Highlighter class is the central component and can be used to extract the
24 most interesting sections of a piece of text and highlight them, with the help of
25 Fragmenter, fragment Scorer, and Formatter classes.
26
27 <h2>Example Usage</h2>
28
29 <pre class="prettyprint">
30   //... Above, create documents with two fields, one with term vectors (tv) and one without (notv)
31   IndexSearcher searcher = new IndexSearcher(directory);
32   QueryParser parser = new QueryParser("notv", analyzer);
33   Query query = parser.parse("million");
34
35   TopDocs hits = searcher.search(query, 10);
36
37   SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter();
38   Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));
39   for (int i = 0; i < 10; i++) {
40     int id = hits.scoreDocs[i].doc;
41     Document doc = searcher.doc(id);
42     String text = doc.get("notv");
43     TokenStream tokenStream = TokenSources.getAnyTokenStream(searcher.getIndexReader(), id, "notv", analyzer);
44     TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, text, false, 10);//highlighter.getBestFragments(tokenStream, text, 3, "...");
45     for (int j = 0; j < frag.length; j++) {
46       if ((frag[j] != null) && (frag[j].getScore() > 0)) {
47         System.out.println((frag[j].toString()));
48       }
49     }
50     //Term vector
51     text = doc.get("tv");
52     tokenStream = TokenSources.getAnyTokenStream(searcher.getIndexReader(), hits.scoreDocs[i].doc, "tv", analyzer);
53     frag = highlighter.getBestTextFragments(tokenStream, text, false, 10);
54     for (int j = 0; j < frag.length; j++) {
55       if ((frag[j] != null) && (frag[j].getScore() > 0)) {
56         System.out.println((frag[j].toString()));
57       }
58     }
59     System.out.println("-------------");
60   }
61 </pre>
62
63 <h2>New features 06/02/2005</h2>
64
65 This release adds options for encoding (thanks to Nicko Cadell).
66 An "Encoder" implementation such as the new SimpleHTMLEncoder class can be passed to the highlighter to encode
67 all those non-xhtml standard characters such as &amp; into legal values. This simple class may not suffice for
68 some languages -  Commons Lang has an implementation that could be used: escapeHtml(String) in
69 http://svn.apache.org/viewcvs.cgi/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringEscapeUtils.java?rev=137958&view=markup
70
71 <h2>New features 22/12/2004</h2>
72
73 This release adds some new capabilities:
74 <ol>
75         <li>Faster highlighting using Term vector support</li>
76         <li>New formatting options to use color intensity to show informational value</li>
77         <li>Options for better summarization by using term IDF scores to influence fragment selection</li>
78 </ol>
79
80 <p>
81 The highlighter takes a TokenStream as input. Until now these streams have typically been produced
82 using an Analyzer but the new class TokenSources provides helper methods for obtaining TokenStreams from
83 the new TermVector position support (see latest CVS version).</p>
84
85 <p>The new class GradientFormatter can use a scale of colors to highlight terms according to their score.
86 A subtle use of color can help emphasise the reasons for matching (useful when doing "MoreLikeThis" queries and
87 you want to see what the basis of the similarities are).</p>
88
89 <p>The QueryScorer class has a new constructor which can use an IndexReader to derive the IDF (inverse document frequency)
90 for each term in order to influence the score. This is useful for helping to extracting the most significant sections
91 of a document and in supplying scores used by the new GradientFormatter to color significant words more strongly.
92 The QueryScorer.getMaxWeight method is useful when passed to the GradientFormatter constructor to define the top score
93 which is associated with the top color.</p>
94
95
96
97
98 </body>
99 </html>