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 {
47 protected Query[] prepareQueries() throws Exception {
49 Analyzer anlzr = NewAnalyzerTask.createAnalyzer(config.get("analyzer",
50 "org.apache.lucene.analysis.standard.StandardAnalyzer"));
51 String defaultField = config.get("file.query.maker.default.field", DocMaker.BODY_FIELD);
52 QueryParser qp = new QueryParser(Version.LUCENE_CURRENT, defaultField, anlzr);
53 qp.setAllowLeadingWildcard(true);
55 List<Query> qq = new ArrayList<Query>();
56 String fileName = config.get("file.query.maker.file", null);
59 File file = new File(fileName);
62 reader = new FileReader(file);
64 //see if we can find it as a resource
65 InputStream asStream = FileBasedQueryMaker.class.getClassLoader().getResourceAsStream(fileName);
66 if (asStream != null) {
67 reader = new InputStreamReader(asStream);
72 BufferedReader buffered = new BufferedReader(reader);
75 while ((line = buffered.readLine()) != null) {
77 if (line.length() != 0 && !line.startsWith("#")) {
79 qq.add(qp.parse(line));
80 } catch (ParseException e) {
81 System.err.println("Exception: " + e.getMessage() + " occurred while parsing line: " + lineNum + " Text: " + line);
90 System.err.println("No Reader available for: " + fileName);
94 return qq.toArray(new Query[qq.size()]) ;