2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.solr.highlight;
19
import org.apache.lucene.analysis.TokenStream;
20
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
21
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
22
import org.apache.lucene.search.highlight.Fragmenter;
23
import org.apache.lucene.search.highlight.NullFragmenter;
24
import org.apache.lucene.search.highlight.SimpleFragmenter;
25
import org.apache.solr.common.params.DefaultSolrParams;
26
import org.apache.solr.common.params.HighlightParams;
27
import org.apache.solr.common.params.SolrParams;
29
public class GapFragmenter extends HighlightingPluginBase implements SolrFragmenter
31
public Fragmenter getFragmenter(String fieldName, SolrParams params )
34
if( defaults != null ) {
35
params = new DefaultSolrParams( params, defaults );
38
int fragsize = params.getFieldInt( fieldName, HighlightParams.FRAGSIZE, 100 );
39
return (fragsize <= 0) ? new NullFragmenter() : new LuceneGapFragmenter(fragsize);
43
///////////////////////////////////////////////////////////////////////
44
//////////////////////// SolrInfoMBeans methods ///////////////////////
45
///////////////////////////////////////////////////////////////////////
48
public String getDescription() {
49
return "GapFragmenter";
53
public String getVersion() {
54
return "$Revision: 1065312 $";
58
public String getSourceId() {
59
return "$Id: GapFragmenter.java 1065312 2011-01-30 16:08:25Z rmuir $";
63
public String getSource() {
64
return "$URL: http://svn.apache.org/repos/asf/lucene/dev/tags/lucene_solr_3_5_0/solr/core/src/java/org/apache/solr/highlight/GapFragmenter.java $";
70
* A simple modification of SimpleFragmenter which additionally creates new
71
* fragments when an unusually-large position increment is encountered
72
* (this behaves much better in the presence of multi-valued fields).
74
class LuceneGapFragmenter extends SimpleFragmenter {
76
* When a gap in term positions is observed that is at least this big, treat
77
* the gap as a fragment delimiter.
79
public static final int INCREMENT_THRESHOLD = 50;
80
protected int fragOffset = 0;
82
private OffsetAttribute offsetAtt;
83
private PositionIncrementAttribute posIncAtt;
85
public LuceneGapFragmenter() {
88
public LuceneGapFragmenter(int fragsize) {
93
* @see org.apache.lucene.search.highlight.TextFragmenter#start(java.lang.String)
96
public void start(String originalText, TokenStream tokenStream) {
97
offsetAtt = tokenStream.getAttribute(OffsetAttribute.class);
98
posIncAtt = tokenStream.getAttribute(PositionIncrementAttribute.class);
103
* @see org.apache.lucene.search.highlight.TextFragmenter#isNewFragment(org.apache.lucene.analysis.Token)
106
public boolean isNewFragment() {
107
int endOffset = offsetAtt.endOffset();
109
endOffset >= fragOffset + getFragmentSize() ||
110
posIncAtt.getPositionIncrement() > INCREMENT_THRESHOLD;
112
fragOffset = endOffset;