add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / tasks / SearchTravRetVectorHighlightTask.java
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 FieldQuery fq = highlighter.getFieldQuery( 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         String[] fragments = highlighter.getBestFragments(fq, reader, doc, field, fragSize, maxFrags);
104         return fragments != null ? fragments.length : 0;
105       }
106     };
107   }
108
109   @Override
110   protected Collection<String> getFieldsToHighlight(Document document) {
111     Collection<String> result = super.getFieldsToHighlight(document);
112     //if stored is false, then result will be empty, in which case just get all the param fields
113     if (paramFields.isEmpty() == false && result.isEmpty() == false) {
114       result.retainAll(paramFields);
115     } else {
116       result = paramFields;
117     }
118     return result;
119   }
120
121   @Override
122   public void setParams(String params) {
123     super.setParams(params);
124     String [] splits = params.split(",");
125     for (int i = 0; i < splits.length; i++) {
126       if (splits[i].startsWith("size[") == true){
127         traversalSize = (int)Float.parseFloat(splits[i].substring("size[".length(),splits[i].length() - 1));
128       } else if (splits[i].startsWith("highlight[") == true){
129         numToHighlight = (int)Float.parseFloat(splits[i].substring("highlight[".length(),splits[i].length() - 1));
130       } else if (splits[i].startsWith("maxFrags[") == true){
131         maxFrags = (int)Float.parseFloat(splits[i].substring("maxFrags[".length(),splits[i].length() - 1));
132       } else if (splits[i].startsWith("fragSize[") == true){
133         fragSize = (int)Float.parseFloat(splits[i].substring("fragSize[".length(),splits[i].length() - 1));
134       } else if (splits[i].startsWith("fields[") == true){
135         paramFields = new HashSet<String>();
136         String fieldNames = splits[i].substring("fields[".length(), splits[i].length() - 1);
137         String [] fieldSplits = fieldNames.split(";");
138         for (int j = 0; j < fieldSplits.length; j++) {
139           paramFields.add(fieldSplits[j]);          
140         }
141
142       }
143     }
144   }
145 }