pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / test / org / apache / lucene / benchmark / byTask / tasks / CountingHighlighterTestTask.java
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
18 package org.apache.lucene.benchmark.byTask.tasks;
19
20 import org.apache.lucene.benchmark.byTask.PerfRunData;
21 import org.apache.lucene.analysis.TokenStream;
22 import org.apache.lucene.analysis.Analyzer;
23 import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
24 import org.apache.lucene.search.highlight.Highlighter;
25 import org.apache.lucene.search.highlight.TextFragment;
26 import org.apache.lucene.search.highlight.QueryScorer;
27 import org.apache.lucene.search.highlight.TokenSources;
28 import org.apache.lucene.search.Query;
29 import org.apache.lucene.document.Document;
30 import org.apache.lucene.index.IndexReader;
31
32 import java.io.IOException;
33
34 /**
35  * Test Search task which counts number of searches.
36  */
37 public class CountingHighlighterTestTask extends SearchTravRetHighlightTask {
38
39   public static int numHighlightedResults = 0;
40   public static int numDocsRetrieved = 0;
41
42   public CountingHighlighterTestTask(PerfRunData runData) {
43     super(runData);
44   }
45
46   @Override
47   protected Document retrieveDoc(IndexReader ir, int id) throws IOException {
48     Document document = ir.document(id);
49     if (document != null) {
50       numDocsRetrieved++;
51     }
52     return document;
53   }
54
55   @Override
56   public BenchmarkHighlighter getBenchmarkHighlighter(Query q) {
57     highlighter = new Highlighter(new SimpleHTMLFormatter(), new QueryScorer(q));
58     return new BenchmarkHighlighter() {
59       @Override
60       public int doHighlight(IndexReader reader, int doc, String field, Document document, Analyzer analyzer, String text) throws Exception {
61         TokenStream ts = TokenSources.getAnyTokenStream(reader, doc, field, document, analyzer);
62         TextFragment[] frag = highlighter.getBestTextFragments(ts, text, mergeContiguous, maxFrags);
63         numHighlightedResults += frag != null ? frag.length : 0;
64         return frag != null ? frag.length : 0;
65       }
66     };
67   }
68 }