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.analysis.TokenStream;
22 import org.apache.lucene.benchmark.byTask.PerfRunData;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.index.IndexReader;
25 import org.apache.lucene.search.Query;
26 import org.apache.lucene.search.highlight.Highlighter;
27 import org.apache.lucene.search.highlight.QueryScorer;
28 import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
29 import org.apache.lucene.search.highlight.TextFragment;
30 import org.apache.lucene.search.highlight.TokenSources;
33 import java.util.Collection;
34 import java.util.HashSet;
35 import java.util.Collections;
38 * Search and Traverse and Retrieve docs task. Highlight the fields in the retrieved documents.
40 * Uses the {@link org.apache.lucene.search.highlight.SimpleHTMLFormatter} for formatting.
42 * <p>Note: This task reuses the reader if it is already open.
43 * Otherwise a reader is opened at start and closed at the end.
46 * <p>Takes optional multivalued, comma separated param string as: size[<traversal size>],highlight[<int>],maxFrags[<int>],mergeContiguous[<boolean>],fields[name1;name2;...]</p>
48 * <li>traversal size - The number of hits to traverse, otherwise all will be traversed</li>
49 * <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>
50 * <li>maxFrags - The maximum number of fragments to score by the highlighter</li>
51 * <li>mergeContiguous - true if contiguous fragments should be merged.</li>
52 * <li>fields - The fields to highlight. If not specified all fields will be highlighted (or at least attempted)</li>
55 * <pre>"SearchHlgtSameRdr" SearchTravRetHighlight(size[10],highlight[10],mergeContiguous[true],maxFrags[3],fields[body]) > : 1000
58 * Documents must be stored in order for this task to work. Additionally, term vector positions can be used as well.
60 * <p>Other side effects: counts additional 1 (record) for each traversed hit,
61 * and 1 more for each retrieved (non null) document and 1 for each fragment returned.</p>
63 public class SearchTravRetHighlightTask extends SearchTravTask {
65 protected int numToHighlight = Integer.MAX_VALUE;
66 protected boolean mergeContiguous;
67 protected int maxFrags = 2;
68 protected Set<String> paramFields = Collections.emptySet();
69 protected Highlighter highlighter;
70 protected int maxDocCharsToAnalyze;
72 public SearchTravRetHighlightTask(PerfRunData runData) {
77 public void setup() throws Exception {
79 //check to make sure either the doc is being stored
80 PerfRunData data = getRunData();
81 if (data.getConfig().get("doc.stored", false) == false){
82 throw new Exception("doc.stored must be set to true");
84 maxDocCharsToAnalyze = data.getConfig().get("highlighter.maxDocCharsToAnalyze", Highlighter.DEFAULT_MAX_CHARS_TO_ANALYZE);
88 public boolean withRetrieve() {
93 public int numToHighlight() {
94 return numToHighlight;
98 protected BenchmarkHighlighter getBenchmarkHighlighter(Query q){
99 highlighter = new Highlighter(new SimpleHTMLFormatter(), new QueryScorer(q));
100 highlighter.setMaxDocCharsToAnalyze(maxDocCharsToAnalyze);
101 return new BenchmarkHighlighter(){
103 public int doHighlight(IndexReader reader, int doc, String field,
104 Document document, Analyzer analyzer, String text) throws Exception {
105 TokenStream ts = TokenSources.getAnyTokenStream(reader, doc, field, document, analyzer);
106 TextFragment[] frag = highlighter.getBestTextFragments(ts, text, mergeContiguous, maxFrags);
107 return frag != null ? frag.length : 0;
113 protected Collection<String> getFieldsToHighlight(Document document) {
114 Collection<String> result = super.getFieldsToHighlight(document);
115 //if stored is false, then result will be empty, in which case just get all the param fields
116 if (paramFields.isEmpty() == false && result.isEmpty() == false) {
117 result.retainAll(paramFields);
119 result = paramFields;
125 public void setParams(String params) {
126 // can't call super because super doesn't understand our
128 this.params = params;
129 String [] splits = params.split(",");
130 for (int i = 0; i < splits.length; i++) {
131 if (splits[i].startsWith("size[") == true){
132 traversalSize = (int)Float.parseFloat(splits[i].substring("size[".length(),splits[i].length() - 1));
133 } else if (splits[i].startsWith("highlight[") == true){
134 numToHighlight = (int)Float.parseFloat(splits[i].substring("highlight[".length(),splits[i].length() - 1));
135 } else if (splits[i].startsWith("maxFrags[") == true){
136 maxFrags = (int)Float.parseFloat(splits[i].substring("maxFrags[".length(),splits[i].length() - 1));
137 } else if (splits[i].startsWith("mergeContiguous[") == true){
138 mergeContiguous = Boolean.valueOf(splits[i].substring("mergeContiguous[".length(),splits[i].length() - 1)).booleanValue();
139 } else if (splits[i].startsWith("fields[") == true){
140 paramFields = new HashSet<String>();
141 String fieldNames = splits[i].substring("fields[".length(), splits[i].length() - 1);
142 String [] fieldSplits = fieldNames.split(";");
143 for (int j = 0; j < fieldSplits.length; j++) {
144 paramFields.add(fieldSplits[j]);