~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/highlight/GapFragmenter.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
package org.apache.solr.highlight;
18
 
 
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;
28
 
 
29
 
public class GapFragmenter extends HighlightingPluginBase implements SolrFragmenter
30
 
{
31
 
  public Fragmenter getFragmenter(String fieldName, SolrParams params )
32
 
  {
33
 
    numRequests++;
34
 
    if( defaults != null ) {
35
 
      params = new DefaultSolrParams( params, defaults );
36
 
    }
37
 
    
38
 
    int fragsize = params.getFieldInt( fieldName, HighlightParams.FRAGSIZE, 100 );
39
 
    return (fragsize <= 0) ? new NullFragmenter() : new LuceneGapFragmenter(fragsize);
40
 
  }
41
 
  
42
 
 
43
 
  ///////////////////////////////////////////////////////////////////////
44
 
  //////////////////////// SolrInfoMBeans methods ///////////////////////
45
 
  ///////////////////////////////////////////////////////////////////////
46
 
 
47
 
  @Override
48
 
  public String getDescription() {
49
 
    return "GapFragmenter";
50
 
  }
51
 
 
52
 
  @Override
53
 
  public String getVersion() {
54
 
      return "$Revision: 1065312 $";
55
 
  }
56
 
 
57
 
  @Override
58
 
  public String getSourceId() {
59
 
    return "$Id: GapFragmenter.java 1065312 2011-01-30 16:08:25Z rmuir $";
60
 
  }
61
 
 
62
 
  @Override
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 $";
65
 
  }
66
 
}
67
 
 
68
 
 
69
 
/**
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).
73
 
 */
74
 
class LuceneGapFragmenter extends SimpleFragmenter {
75
 
  /** 
76
 
   * When a gap in term positions is observed that is at least this big, treat
77
 
   * the gap as a fragment delimiter.
78
 
   */
79
 
  public static final int INCREMENT_THRESHOLD = 50;
80
 
  protected int fragOffset = 0;
81
 
  
82
 
  private OffsetAttribute offsetAtt;
83
 
  private PositionIncrementAttribute posIncAtt;
84
 
  
85
 
  public LuceneGapFragmenter() {
86
 
  }
87
 
  
88
 
  public LuceneGapFragmenter(int fragsize) {
89
 
     super(fragsize);
90
 
  }
91
 
  
92
 
  /* (non-Javadoc)
93
 
   * @see org.apache.lucene.search.highlight.TextFragmenter#start(java.lang.String)
94
 
   */
95
 
  @Override
96
 
  public void start(String originalText, TokenStream tokenStream) {
97
 
    offsetAtt = tokenStream.getAttribute(OffsetAttribute.class);
98
 
    posIncAtt = tokenStream.getAttribute(PositionIncrementAttribute.class);
99
 
    fragOffset = 0;
100
 
  }
101
 
 
102
 
  /* (non-Javadoc)
103
 
   * @see org.apache.lucene.search.highlight.TextFragmenter#isNewFragment(org.apache.lucene.analysis.Token)
104
 
   */
105
 
  @Override
106
 
  public boolean isNewFragment() {
107
 
    int endOffset = offsetAtt.endOffset();
108
 
    boolean isNewFrag = 
109
 
      endOffset >= fragOffset + getFragmentSize() ||
110
 
      posIncAtt.getPositionIncrement() > INCREMENT_THRESHOLD;
111
 
    if(isNewFrag) {
112
 
        fragOffset = endOffset;
113
 
    }
114
 
    return isNewFrag;
115
 
  }
116
 
}