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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.lucene.benchmark.byTask.tasks;
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;
32 import java.io.IOException;
35 * Test Search task which counts number of searches.
37 public class CountingHighlighterTestTask extends SearchTravRetHighlightTask {
39 public static int numHighlightedResults = 0;
40 public static int numDocsRetrieved = 0;
42 public CountingHighlighterTestTask(PerfRunData runData) {
47 protected Document retrieveDoc(IndexReader ir, int id) throws IOException {
48 Document document = ir.document(id);
49 if (document != null) {
56 public BenchmarkHighlighter getBenchmarkHighlighter(Query q) {
57 highlighter = new Highlighter(new SimpleHTMLFormatter(), new QueryScorer(q));
58 return new BenchmarkHighlighter() {
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;