1 package org.apache.lucene.search.vectorhighlight;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.search.Query;
24 import org.apache.lucene.search.highlight.Encoder;
27 * Another highlighter implementation.
30 public class FastVectorHighlighter {
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;
41 * the default constructor.
43 public FastVectorHighlighter(){
44 this( DEFAULT_PHRASE_HIGHLIGHT, DEFAULT_FIELD_MATCH );
48 * a constructor. Using {@link SimpleFragListBuilder} and {@link ScoreOrderFragmentsBuilder}.
50 * @param phraseHighlight true or false for phrase highlighting
51 * @param fieldMatch true of false for field matching
53 public FastVectorHighlighter( boolean phraseHighlight, boolean fieldMatch ){
54 this( phraseHighlight, fieldMatch, new SimpleFragListBuilder(), new ScoreOrderFragmentsBuilder() );
58 * a constructor. A {@link FragListBuilder} and a {@link FragmentsBuilder} can be specified (plugins).
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}
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;
74 * create a {@link FieldQuery} object.
76 * @param query a query
77 * @return the created {@link FieldQuery} object
79 public FieldQuery getFieldQuery( Query query ) {
80 // TODO: should we deprecate this?
81 // because if there is no reader, then we cannot rewrite MTQ.
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);
91 * create a {@link FieldQuery} object.
93 * @param query a query
94 * @return the created {@link FieldQuery} object
96 public FieldQuery getFieldQuery( Query query, IndexReader reader ) throws IOException {
97 return new FieldQuery( query, reader, phraseHighlight, fieldMatch );
101 * return the best fragment.
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
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 );
119 * return the best fragments.
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
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 );
139 * return the best fragment.
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
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 );
163 * return the best fragments.
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
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 );
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 );
199 * return whether phraseHighlight or not.
201 * @return whether phraseHighlight or not
203 public boolean isPhraseHighlight(){ return phraseHighlight; }
206 * return whether fieldMatch or not.
208 * @return whether fieldMatch or not
210 public boolean isFieldMatch(){ return fieldMatch; }
213 * @return the maximum number of phrases to analyze when searching for the highest-scoring phrase.
215 public int getPhraseLimit () { return phraseLimit; }
218 * set the maximum number of phrases to analyze when searching for the highest-scoring phrase.
219 * The default is unlimited (Integer.MAX_VALUE).
221 public void setPhraseLimit (int phraseLimit) { this.phraseLimit = phraseLimit; }