pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / highlighter / src / java / org / apache / lucene / search / vectorhighlight / FastVectorHighlighter.java
1 package org.apache.lucene.search.vectorhighlight;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.search.Query;
24 import org.apache.lucene.search.highlight.Encoder;
25
26 /**
27  * Another highlighter implementation.
28  *
29  */
30 public class FastVectorHighlighter {
31
32   public static final boolean DEFAULT_PHRASE_HIGHLIGHT = true;
33   public static final boolean DEFAULT_FIELD_MATCH = true;
34   private final boolean phraseHighlight;
35   private final boolean fieldMatch;
36   private final FragListBuilder fragListBuilder;
37   private final FragmentsBuilder fragmentsBuilder;
38   private int phraseLimit = Integer.MAX_VALUE;
39
40   /**
41    * the default constructor.
42    */
43   public FastVectorHighlighter(){
44     this( DEFAULT_PHRASE_HIGHLIGHT, DEFAULT_FIELD_MATCH );
45   }
46
47   /**
48    * a constructor. Using {@link SimpleFragListBuilder} and {@link ScoreOrderFragmentsBuilder}.
49    * 
50    * @param phraseHighlight true or false for phrase highlighting
51    * @param fieldMatch true of false for field matching
52    */
53   public FastVectorHighlighter( boolean phraseHighlight, boolean fieldMatch ){
54     this( phraseHighlight, fieldMatch, new SimpleFragListBuilder(), new ScoreOrderFragmentsBuilder() );
55   }
56
57   /**
58    * a constructor. A {@link FragListBuilder} and a {@link FragmentsBuilder} can be specified (plugins).
59    * 
60    * @param phraseHighlight true of false for phrase highlighting
61    * @param fieldMatch true of false for field matching
62    * @param fragListBuilder an instance of {@link FragListBuilder}
63    * @param fragmentsBuilder an instance of {@link FragmentsBuilder}
64    */
65   public FastVectorHighlighter( boolean phraseHighlight, boolean fieldMatch,
66       FragListBuilder fragListBuilder, FragmentsBuilder fragmentsBuilder ){
67     this.phraseHighlight = phraseHighlight;
68     this.fieldMatch = fieldMatch;
69     this.fragListBuilder = fragListBuilder;
70     this.fragmentsBuilder = fragmentsBuilder;
71   }
72
73   /**
74    * create a {@link FieldQuery} object.
75    * 
76    * @param query a query
77    * @return the created {@link FieldQuery} object
78    */
79   public FieldQuery getFieldQuery( Query query ) {
80     // TODO: should we deprecate this? 
81     // because if there is no reader, then we cannot rewrite MTQ.
82     try {
83       return new FieldQuery( query, null, phraseHighlight, fieldMatch );
84     } catch (IOException e) {
85       // should never be thrown when reader is null
86       throw new RuntimeException (e);
87     }
88   }
89   
90   /**
91    * create a {@link FieldQuery} object.
92    * 
93    * @param query a query
94    * @return the created {@link FieldQuery} object
95    */
96   public FieldQuery getFieldQuery( Query query, IndexReader reader ) throws IOException {
97     return new FieldQuery( query, reader, phraseHighlight, fieldMatch );
98   }
99
100   /**
101    * return the best fragment.
102    * 
103    * @param fieldQuery {@link FieldQuery} object
104    * @param reader {@link IndexReader} of the index
105    * @param docId document id to be highlighted
106    * @param fieldName field of the document to be highlighted
107    * @param fragCharSize the length (number of chars) of a fragment
108    * @return the best fragment (snippet) string
109    * @throws IOException
110    */
111   public final String getBestFragment( final FieldQuery fieldQuery, IndexReader reader, int docId,
112       String fieldName, int fragCharSize ) throws IOException {
113     FieldFragList fieldFragList =
114       getFieldFragList( fragListBuilder, fieldQuery, reader, docId, fieldName, fragCharSize );
115     return fragmentsBuilder.createFragment( reader, docId, fieldName, fieldFragList );
116   }
117
118   /**
119    * return the best fragments.
120    * 
121    * @param fieldQuery {@link FieldQuery} object
122    * @param reader {@link IndexReader} of the index
123    * @param docId document id to be highlighted
124    * @param fieldName field of the document to be highlighted
125    * @param fragCharSize the length (number of chars) of a fragment
126    * @param maxNumFragments maximum number of fragments
127    * @return created fragments or null when no fragments created.
128    *         size of the array can be less than maxNumFragments
129    * @throws IOException
130    */
131   public final String[] getBestFragments( final FieldQuery fieldQuery, IndexReader reader, int docId,
132       String fieldName, int fragCharSize, int maxNumFragments ) throws IOException {
133     FieldFragList fieldFragList =
134       getFieldFragList( fragListBuilder, fieldQuery, reader, docId, fieldName, fragCharSize );
135     return fragmentsBuilder.createFragments( reader, docId, fieldName, fieldFragList, maxNumFragments );
136   }
137
138   /**
139    * return the best fragment.
140    * 
141    * @param fieldQuery {@link FieldQuery} object
142    * @param reader {@link IndexReader} of the index
143    * @param docId document id to be highlighted
144    * @param fieldName field of the document to be highlighted
145    * @param fragCharSize the length (number of chars) of a fragment
146    * @param fragListBuilder {@link FragListBuilder} object
147    * @param fragmentsBuilder {@link FragmentsBuilder} object
148    * @param preTags pre-tags to be used to highlight terms
149    * @param postTags post-tags to be used to highlight terms
150    * @param encoder an encoder that generates encoded text
151    * @return the best fragment (snippet) string
152    * @throws IOException
153    */
154   public final String getBestFragment( final FieldQuery fieldQuery, IndexReader reader, int docId,
155       String fieldName, int fragCharSize,
156       FragListBuilder fragListBuilder, FragmentsBuilder fragmentsBuilder,
157       String[] preTags, String[] postTags, Encoder encoder ) throws IOException {
158     FieldFragList fieldFragList = getFieldFragList( fragListBuilder, fieldQuery, reader, docId, fieldName, fragCharSize );
159     return fragmentsBuilder.createFragment( reader, docId, fieldName, fieldFragList, preTags, postTags, encoder );
160   }
161
162   /**
163    * return the best fragments.
164    * 
165    * @param fieldQuery {@link FieldQuery} object
166    * @param reader {@link IndexReader} of the index
167    * @param docId document id to be highlighted
168    * @param fieldName field of the document to be highlighted
169    * @param fragCharSize the length (number of chars) of a fragment
170    * @param maxNumFragments maximum number of fragments
171    * @param fragListBuilder {@link FragListBuilder} object
172    * @param fragmentsBuilder {@link FragmentsBuilder} object
173    * @param preTags pre-tags to be used to highlight terms
174    * @param postTags post-tags to be used to highlight terms
175    * @param encoder an encoder that generates encoded text
176    * @return created fragments or null when no fragments created.
177    *         size of the array can be less than maxNumFragments
178    * @throws IOException
179    */
180   public final String[] getBestFragments( final FieldQuery fieldQuery, IndexReader reader, int docId,
181       String fieldName, int fragCharSize, int maxNumFragments,
182       FragListBuilder fragListBuilder, FragmentsBuilder fragmentsBuilder,
183       String[] preTags, String[] postTags, Encoder encoder ) throws IOException {
184     FieldFragList fieldFragList =
185       getFieldFragList( fragListBuilder, fieldQuery, reader, docId, fieldName, fragCharSize );
186     return fragmentsBuilder.createFragments( reader, docId, fieldName, fieldFragList, maxNumFragments,
187         preTags, postTags, encoder );
188   }
189   
190   private FieldFragList getFieldFragList( FragListBuilder fragListBuilder,
191       final FieldQuery fieldQuery, IndexReader reader, int docId,
192       String fieldName, int fragCharSize ) throws IOException {
193     FieldTermStack fieldTermStack = new FieldTermStack( reader, docId, fieldName, fieldQuery );
194     FieldPhraseList fieldPhraseList = new FieldPhraseList( fieldTermStack, fieldQuery, phraseLimit );
195     return fragListBuilder.createFieldFragList( fieldPhraseList, fragCharSize );
196   }
197
198   /**
199    * return whether phraseHighlight or not.
200    * 
201    * @return whether phraseHighlight or not
202    */
203   public boolean isPhraseHighlight(){ return phraseHighlight; }
204
205   /**
206    * return whether fieldMatch or not.
207    * 
208    * @return whether fieldMatch or not
209    */
210   public boolean isFieldMatch(){ return fieldMatch; }
211   
212   /**
213    * @return the maximum number of phrases to analyze when searching for the highest-scoring phrase.
214    */
215   public int getPhraseLimit () { return phraseLimit; }
216   
217   /**
218    * set the maximum number of phrases to analyze when searching for the highest-scoring phrase.
219    * The default is unlimited (Integer.MAX_VALUE).
220    */
221   public void setPhraseLimit (int phraseLimit) { this.phraseLimit = phraseLimit; }
222 }