add --shared
[pylucene.git] / lucene-java-3.4.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     return new FieldQuery( query, phraseHighlight, fieldMatch );
81   }
82
83   /**
84    * return the best fragment.
85    * 
86    * @param fieldQuery {@link FieldQuery} object
87    * @param reader {@link IndexReader} of the index
88    * @param docId document id to be highlighted
89    * @param fieldName field of the document to be highlighted
90    * @param fragCharSize the length (number of chars) of a fragment
91    * @return the best fragment (snippet) string
92    * @throws IOException
93    */
94   public final String getBestFragment( final FieldQuery fieldQuery, IndexReader reader, int docId,
95       String fieldName, int fragCharSize ) throws IOException {
96     FieldFragList fieldFragList =
97       getFieldFragList( fragListBuilder, fieldQuery, reader, docId, fieldName, fragCharSize );
98     return fragmentsBuilder.createFragment( reader, docId, fieldName, fieldFragList );
99   }
100
101   /**
102    * return the best fragments.
103    * 
104    * @param fieldQuery {@link FieldQuery} object
105    * @param reader {@link IndexReader} of the index
106    * @param docId document id to be highlighted
107    * @param fieldName field of the document to be highlighted
108    * @param fragCharSize the length (number of chars) of a fragment
109    * @param maxNumFragments maximum number of fragments
110    * @return created fragments or null when no fragments created.
111    *         size of the array can be less than maxNumFragments
112    * @throws IOException
113    */
114   public final String[] getBestFragments( final FieldQuery fieldQuery, IndexReader reader, int docId,
115       String fieldName, int fragCharSize, int maxNumFragments ) throws IOException {
116     FieldFragList fieldFragList =
117       getFieldFragList( fragListBuilder, fieldQuery, reader, docId, fieldName, fragCharSize );
118     return fragmentsBuilder.createFragments( reader, docId, fieldName, fieldFragList, maxNumFragments );
119   }
120
121   /**
122    * return the best fragment.
123    * 
124    * @param fieldQuery {@link FieldQuery} object
125    * @param reader {@link IndexReader} of the index
126    * @param docId document id to be highlighted
127    * @param fieldName field of the document to be highlighted
128    * @param fragCharSize the length (number of chars) of a fragment
129    * @param fragListBuilder {@link FragListBuilder} object
130    * @param fragmentsBuilder {@link FragmentsBuilder} object
131    * @param preTags pre-tags to be used to highlight terms
132    * @param postTags post-tags to be used to highlight terms
133    * @param encoder an encoder that generates encoded text
134    * @return the best fragment (snippet) string
135    * @throws IOException
136    */
137   public final String getBestFragment( final FieldQuery fieldQuery, IndexReader reader, int docId,
138       String fieldName, int fragCharSize,
139       FragListBuilder fragListBuilder, FragmentsBuilder fragmentsBuilder,
140       String[] preTags, String[] postTags, Encoder encoder ) throws IOException {
141     FieldFragList fieldFragList = getFieldFragList( fragListBuilder, fieldQuery, reader, docId, fieldName, fragCharSize );
142     return fragmentsBuilder.createFragment( reader, docId, fieldName, fieldFragList, preTags, postTags, encoder );
143   }
144
145   /**
146    * return the best fragments.
147    * 
148    * @param fieldQuery {@link FieldQuery} object
149    * @param reader {@link IndexReader} of the index
150    * @param docId document id to be highlighted
151    * @param fieldName field of the document to be highlighted
152    * @param fragCharSize the length (number of chars) of a fragment
153    * @param maxNumFragments maximum number of fragments
154    * @param fragListBuilder {@link FragListBuilder} object
155    * @param fragmentsBuilder {@link FragmentsBuilder} object
156    * @param preTags pre-tags to be used to highlight terms
157    * @param postTags post-tags to be used to highlight terms
158    * @param encoder an encoder that generates encoded text
159    * @return created fragments or null when no fragments created.
160    *         size of the array can be less than maxNumFragments
161    * @throws IOException
162    */
163   public final String[] getBestFragments( final FieldQuery fieldQuery, IndexReader reader, int docId,
164       String fieldName, int fragCharSize, int maxNumFragments,
165       FragListBuilder fragListBuilder, FragmentsBuilder fragmentsBuilder,
166       String[] preTags, String[] postTags, Encoder encoder ) throws IOException {
167     FieldFragList fieldFragList =
168       getFieldFragList( fragListBuilder, fieldQuery, reader, docId, fieldName, fragCharSize );
169     return fragmentsBuilder.createFragments( reader, docId, fieldName, fieldFragList, maxNumFragments,
170         preTags, postTags, encoder );
171   }
172   
173   private FieldFragList getFieldFragList( FragListBuilder fragListBuilder,
174       final FieldQuery fieldQuery, IndexReader reader, int docId,
175       String fieldName, int fragCharSize ) throws IOException {
176     FieldTermStack fieldTermStack = new FieldTermStack( reader, docId, fieldName, fieldQuery );
177     FieldPhraseList fieldPhraseList = new FieldPhraseList( fieldTermStack, fieldQuery, phraseLimit );
178     return fragListBuilder.createFieldFragList( fieldPhraseList, fragCharSize );
179   }
180
181   /**
182    * return whether phraseHighlight or not.
183    * 
184    * @return whether phraseHighlight or not
185    */
186   public boolean isPhraseHighlight(){ return phraseHighlight; }
187
188   /**
189    * return whether fieldMatch or not.
190    * 
191    * @return whether fieldMatch or not
192    */
193   public boolean isFieldMatch(){ return fieldMatch; }
194   
195   /**
196    * @return the maximum number of phrases to analyze when searching for the highest-scoring phrase.
197    */
198   public int getPhraseLimit () { return phraseLimit; }
199   
200   /**
201    * set the maximum number of phrases to analyze when searching for the highest-scoring phrase.
202    * The default is unlimited (Integer.MAX_VALUE).
203    */
204   public void setPhraseLimit (int phraseLimit) { this.phraseLimit = phraseLimit; }
205 }