1
package org.apache.lucene.benchmark.byTask.tasks;
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 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;
29
import java.util.Collection;
30
import java.util.HashSet;
31
import java.util.Collections;
34
* Search and Traverse and Retrieve docs task. Highlight the fields in the retrieved documents by using FastVectorHighlighter.
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.
40
* <p>Takes optional multivalued, comma separated param string as: size[<traversal size>],highlight[<int>],maxFrags[<int>],mergeContiguous[<boolean>],fields[name1;name2;...]</p>
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>
49
* <pre>"SearchVecHlgtSameRdr" SearchTravRetVectorHighlight(size[10],highlight[10],maxFrags[3],fields[body]) > : 1000
52
* Fields must be stored and term vector offsets and positions in order must be true for this task to work.
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>
57
public class SearchTravRetVectorHighlightTask extends SearchTravTask {
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;
65
public SearchTravRetVectorHighlightTask(PerfRunData runData) {
70
public void setup() throws Exception {
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");
77
if (data.getConfig().get("doc.term.vector.offsets", false) == false){
78
throw new Exception("doc.term.vector.offsets must be set to true");
80
if (data.getConfig().get("doc.term.vector.positions", false) == false){
81
throw new Exception("doc.term.vector.positions must be set to true");
86
public boolean withRetrieve() {
91
public int numToHighlight() {
92
return numToHighlight;
96
protected BenchmarkHighlighter getBenchmarkHighlighter(Query q){
97
highlighter = new FastVectorHighlighter( false, false );
99
return new BenchmarkHighlighter(){
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;
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);
117
result = paramFields;
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]);