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 return new FieldQuery( query, phraseHighlight, fieldMatch );
84 * return the best fragment.
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
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 );
102 * return the best fragments.
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
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 );
122 * return the best fragment.
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
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 );
146 * return the best fragments.
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
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 );
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 );
182 * return whether phraseHighlight or not.
184 * @return whether phraseHighlight or not
186 public boolean isPhraseHighlight(){ return phraseHighlight; }
189 * return whether fieldMatch or not.
191 * @return whether fieldMatch or not
193 public boolean isFieldMatch(){ return fieldMatch; }
196 * @return the maximum number of phrases to analyze when searching for the highest-scoring phrase.
198 public int getPhraseLimit () { return phraseLimit; }
201 * set the maximum number of phrases to analyze when searching for the highest-scoring phrase.
202 * The default is unlimited (Integer.MAX_VALUE).
204 public void setPhraseLimit (int phraseLimit) { this.phraseLimit = phraseLimit; }