01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.lucene.benchmark.quality.utils;
17:
18: import java.io.IOException;
19:
20: import org.apache.lucene.document.FieldSelector;
21: import org.apache.lucene.document.FieldSelectorResult;
22: import org.apache.lucene.search.Searcher;
23:
24: /**
25: * Utility: extract doc names from an index
26: */
27: public class DocNameExtractor {
28:
29: private FieldSelector fldSel;
30: private String docNameField;
31:
32: /**
33: * Constructor for DocNameExtractor.
34: * @param docNameField name of the stored field containing the doc name.
35: */
36: public DocNameExtractor(final String docNameField) {
37: this .docNameField = docNameField;
38: fldSel = new FieldSelector() {
39: public FieldSelectorResult accept(String fieldName) {
40: return fieldName.equals(docNameField) ? FieldSelectorResult.LOAD_AND_BREAK
41: : FieldSelectorResult.NO_LOAD;
42: }
43: };
44: }
45:
46: /**
47: * Extract the name of the input doc from the index.
48: * @param searcher access to the index.
49: * @param docid ID of doc whose name is needed.
50: * @return the name of the input doc as extracted from the index.
51: * @throws IOException if cannot extract the doc name from the index.
52: */
53: public String docName(Searcher searcher, int docid)
54: throws IOException {
55: return searcher.doc(docid, fldSel).get(docNameField);
56: }
57:
58: }
|