1 package org.apache.lucene.benchmark.byTask.feeds;
3 import org.apache.lucene.analysis.Analyzer;
4 import org.apache.lucene.queryParser.QueryParser;
5 import org.apache.lucene.queryParser.ParseException;
6 import org.apache.lucene.search.Query;
7 import org.apache.lucene.benchmark.byTask.tasks.NewAnalyzerTask;
8 import org.apache.lucene.util.Version;
11 import java.util.ArrayList;
12 import java.util.List;
15 * Copyright 2004 The Apache Software Foundation
17 * Licensed under the Apache License, Version 2.0 (the "License");
18 * you may not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
21 * http://www.apache.org/licenses/LICENSE-2.0
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS,
25 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
31 * Create queries from a FileReader. One per line, pass them through the
32 * QueryParser. Lines beginning with # are treated as comments
34 * File can be specified as a absolute, relative or resource.
35 * Two properties can be set:
36 * file.query.maker.file=<Full path to file containing queries>
38 * file.query.maker.default.field=<Name of default field - Default value is "body">
41 * file.query.maker.file=c:/myqueries.txt
42 * file.query.maker.default.field=body
44 public class FileBasedQueryMaker extends AbstractQueryMaker implements QueryMaker{
48 protected Query[] prepareQueries() throws Exception {
50 Analyzer anlzr = NewAnalyzerTask.createAnalyzer(config.get("analyzer",
51 "org.apache.lucene.analysis.standard.StandardAnalyzer"));
52 String defaultField = config.get("file.query.maker.default.field", DocMaker.BODY_FIELD);
53 QueryParser qp = new QueryParser(Version.LUCENE_CURRENT, defaultField, anlzr);
54 qp.setAllowLeadingWildcard(true);
56 List<Query> qq = new ArrayList<Query>();
57 String fileName = config.get("file.query.maker.file", null);
60 File file = new File(fileName);
63 reader = new FileReader(file);
65 //see if we can find it as a resource
66 InputStream asStream = FileBasedQueryMaker.class.getClassLoader().getResourceAsStream(fileName);
67 if (asStream != null) {
68 reader = new InputStreamReader(asStream);
73 BufferedReader buffered = new BufferedReader(reader);
76 while ((line = buffered.readLine()) != null) {
78 if (line.length() != 0 && !line.startsWith("#")) {
80 qq.add(qp.parse(line));
81 } catch (ParseException e) {
82 System.err.println("Exception: " + e.getMessage() + " occurred while parsing line: " + lineNum + " Text: " + line);
91 System.err.println("No Reader available for: " + fileName);
95 return qq.toArray(new Query[qq.size()]) ;