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

« back to all changes in this revision

Viewing changes to lucene/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/SearchTravRetVectorHighlightTask.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
 
package org.apache.lucene.benchmark.byTask.tasks;
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 org.apache.lucene.analysis.Analyzer;
21
 
import org.apache.lucene.benchmark.byTask.PerfRunData;
22
 
import org.apache.lucene.document.Document;
23
 
import org.apache.lucene.index.IndexReader;
24
 
import org.apache.lucene.search.Query;
25
 
import org.apache.lucene.search.vectorhighlight.FastVectorHighlighter;
26
 
import org.apache.lucene.search.vectorhighlight.FieldQuery;
27
 
 
28
 
import java.util.Set;
29
 
import java.util.Collection;
30
 
import java.util.HashSet;
31
 
import java.util.Collections;
32
 
 
33
 
/**
34
 
 * Search and Traverse and Retrieve docs task.  Highlight the fields in the retrieved documents by using FastVectorHighlighter.
35
 
 *
36
 
 * <p>Note: This task reuses the reader if it is already open.
37
 
 * Otherwise a reader is opened at start and closed at the end.
38
 
 * </p>
39
 
 *
40
 
 * <p>Takes optional multivalued, comma separated param string as: size[&lt;traversal size&gt;],highlight[&lt;int&gt;],maxFrags[&lt;int&gt;],mergeContiguous[&lt;boolean&gt;],fields[name1;name2;...]</p>
41
 
 * <ul>
42
 
 * <li>traversal size - The number of hits to traverse, otherwise all will be traversed</li>
43
 
 * <li>highlight - The number of the hits to highlight.  Will always be less than or equal to traversal size.  Default is Integer.MAX_VALUE (i.e. hits.length())</li>
44
 
 * <li>maxFrags - The maximum number of fragments to score by the highlighter</li>
45
 
 * <li>fragSize - The length of fragments</li>
46
 
 * <li>fields - The fields to highlight.  If not specified all fields will be highlighted (or at least attempted)</li>
47
 
 * </ul>
48
 
 * Example:
49
 
 * <pre>"SearchVecHlgtSameRdr" SearchTravRetVectorHighlight(size[10],highlight[10],maxFrags[3],fields[body]) > : 1000
50
 
 * </pre>
51
 
 *
52
 
 * Fields must be stored and term vector offsets and positions in order must be true for this task to work.
53
 
 *
54
 
 * <p>Other side effects: counts additional 1 (record) for each traversed hit,
55
 
 * and 1 more for each retrieved (non null) document and 1 for each fragment returned.</p>
56
 
 */
57
 
public class SearchTravRetVectorHighlightTask extends SearchTravTask {
58
 
 
59
 
  protected int numToHighlight = Integer.MAX_VALUE;
60
 
  protected int maxFrags = 2;
61
 
  protected int fragSize = 100;
62
 
  protected Set<String> paramFields = Collections.emptySet();
63
 
  protected FastVectorHighlighter highlighter;
64
 
 
65
 
  public SearchTravRetVectorHighlightTask(PerfRunData runData) {
66
 
    super(runData);
67
 
  }
68
 
 
69
 
  @Override
70
 
  public void setup() throws Exception {
71
 
    super.setup();
72
 
    //check to make sure either the doc is being stored
73
 
    PerfRunData data = getRunData();
74
 
    if (data.getConfig().get("doc.stored", false) == false){
75
 
      throw new Exception("doc.stored must be set to true");
76
 
    }
77
 
    if (data.getConfig().get("doc.term.vector.offsets", false) == false){
78
 
      throw new Exception("doc.term.vector.offsets must be set to true");
79
 
    }
80
 
    if (data.getConfig().get("doc.term.vector.positions", false) == false){
81
 
      throw new Exception("doc.term.vector.positions must be set to true");
82
 
    }
83
 
  }
84
 
 
85
 
  @Override
86
 
  public boolean withRetrieve() {
87
 
    return true;
88
 
  }
89
 
 
90
 
  @Override
91
 
  public int numToHighlight() {
92
 
    return numToHighlight;
93
 
  }
94
 
  
95
 
  @Override
96
 
  protected BenchmarkHighlighter getBenchmarkHighlighter(Query q){
97
 
    highlighter = new FastVectorHighlighter( false, false );
98
 
    final Query myq = q;
99
 
    return new BenchmarkHighlighter(){
100
 
      @Override
101
 
      public int doHighlight(IndexReader reader, int doc, String field,
102
 
          Document document, Analyzer analyzer, String text) throws Exception {
103
 
        final FieldQuery fq = highlighter.getFieldQuery( myq, reader);
104
 
        String[] fragments = highlighter.getBestFragments(fq, reader, doc, field, fragSize, maxFrags);
105
 
        return fragments != null ? fragments.length : 0;
106
 
      }
107
 
    };
108
 
  }
109
 
 
110
 
  @Override
111
 
  protected Collection<String> getFieldsToHighlight(Document document) {
112
 
    Collection<String> result = super.getFieldsToHighlight(document);
113
 
    //if stored is false, then result will be empty, in which case just get all the param fields
114
 
    if (paramFields.isEmpty() == false && result.isEmpty() == false) {
115
 
      result.retainAll(paramFields);
116
 
    } else {
117
 
      result = paramFields;
118
 
    }
119
 
    return result;
120
 
  }
121
 
 
122
 
  @Override
123
 
  public void setParams(String params) {
124
 
    super.setParams(params);
125
 
    String [] splits = params.split(",");
126
 
    for (int i = 0; i < splits.length; i++) {
127
 
      if (splits[i].startsWith("size[") == true){
128
 
        traversalSize = (int)Float.parseFloat(splits[i].substring("size[".length(),splits[i].length() - 1));
129
 
      } else if (splits[i].startsWith("highlight[") == true){
130
 
        numToHighlight = (int)Float.parseFloat(splits[i].substring("highlight[".length(),splits[i].length() - 1));
131
 
      } else if (splits[i].startsWith("maxFrags[") == true){
132
 
        maxFrags = (int)Float.parseFloat(splits[i].substring("maxFrags[".length(),splits[i].length() - 1));
133
 
      } else if (splits[i].startsWith("fragSize[") == true){
134
 
        fragSize = (int)Float.parseFloat(splits[i].substring("fragSize[".length(),splits[i].length() - 1));
135
 
      } else if (splits[i].startsWith("fields[") == true){
136
 
        paramFields = new HashSet<String>();
137
 
        String fieldNames = splits[i].substring("fields[".length(), splits[i].length() - 1);
138
 
        String [] fieldSplits = fieldNames.split(";");
139
 
        for (int j = 0; j < fieldSplits.length; j++) {
140
 
          paramFields.add(fieldSplits[j]);          
141
 
        }
142
 
 
143
 
      }
144
 
    }
145
 
  }
146
 
}