~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/FileBasedQueryMaker.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.benchmark.byTask.feeds;
2
 
 
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;
9
 
 
10
 
import java.io.*;
11
 
import java.util.ArrayList;
12
 
import java.util.List;
13
 
 
14
 
/**
15
 
 * Copyright 2004 The Apache Software Foundation
16
 
 * <p/>
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
20
 
 * <p/>
21
 
 * http://www.apache.org/licenses/LICENSE-2.0
22
 
 * <p/>
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.
28
 
 */
29
 
 
30
 
/**
31
 
 * Create queries from a FileReader.  One per line, pass them through the
32
 
 * QueryParser.  Lines beginning with # are treated as comments
33
 
 *
34
 
 * File can be specified as a absolute, relative or resource.
35
 
 * Two properties can be set:
36
 
 * file.query.maker.file=&lt;Full path to file containing queries&gt;
37
 
 * <br/>
38
 
 * file.query.maker.default.field=&lt;Name of default field - Default value is "body"&gt;
39
 
 *
40
 
 * Example:
41
 
 * file.query.maker.file=c:/myqueries.txt
42
 
 * file.query.maker.default.field=body
43
 
 */
44
 
public class FileBasedQueryMaker extends AbstractQueryMaker {
45
 
 
46
 
  @Override
47
 
  protected Query[] prepareQueries() throws Exception {
48
 
 
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);
54
 
 
55
 
    List<Query> qq = new ArrayList<Query>();
56
 
    String fileName = config.get("file.query.maker.file", null);
57
 
    if (fileName != null)
58
 
    {
59
 
      File file = new File(fileName);
60
 
      Reader reader = null;
61
 
      if (file.exists()) {
62
 
        reader = new FileReader(file);
63
 
      } else {
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);
68
 
        }
69
 
      }
70
 
      if (reader != null) {
71
 
        try {
72
 
          BufferedReader buffered = new BufferedReader(reader);
73
 
          String line = null;
74
 
          int lineNum = 0;
75
 
          while ((line = buffered.readLine()) != null) {
76
 
            line = line.trim();
77
 
            if (line.length() != 0 && !line.startsWith("#")) {
78
 
              try {
79
 
                qq.add(qp.parse(line));
80
 
              } catch (ParseException e) {
81
 
                System.err.println("Exception: " + e.getMessage() + " occurred while parsing line: " + lineNum + " Text: " + line);
82
 
              }
83
 
            }
84
 
            lineNum++;
85
 
          }
86
 
        } finally {
87
 
          reader.close();
88
 
        }
89
 
      } else {
90
 
        System.err.println("No Reader available for: " + fileName);
91
 
      }
92
 
      
93
 
    }
94
 
    return qq.toArray(new Query[qq.size()]) ;
95
 
  }
96
 
}