add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / quality / utils / SimpleQQParser.java
1 /**
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
8  *
9  *     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  */
17 package org.apache.lucene.benchmark.quality.utils;
18
19 import org.apache.lucene.analysis.standard.StandardAnalyzer;
20 import org.apache.lucene.benchmark.quality.QualityQuery;
21 import org.apache.lucene.benchmark.quality.QualityQueryParser;
22 import org.apache.lucene.queryParser.ParseException;
23 import org.apache.lucene.queryParser.QueryParser;
24 import org.apache.lucene.search.BooleanClause;
25 import org.apache.lucene.search.BooleanQuery;
26 import org.apache.lucene.search.Query;
27 import org.apache.lucene.util.Version;
28
29 /**
30  * Simplistic quality query parser. A Lucene query is created by passing 
31  * the value of the specified QualityQuery name-value pair(s) into 
32  * a Lucene's QueryParser using StandardAnalyzer. */
33 public class SimpleQQParser implements QualityQueryParser {
34
35   private String qqNames[];
36   private String indexField;
37   ThreadLocal<QueryParser> queryParser = new ThreadLocal<QueryParser>();
38
39   /**
40    * Constructor of a simple qq parser.
41    * @param qqNames name-value pairs of quality query to use for creating the query
42    * @param indexField corresponding index field  
43    */
44   public SimpleQQParser(String qqNames[], String indexField) {
45     this.qqNames = qqNames;
46     this.indexField = indexField;
47   }
48
49   /**
50    * Constructor of a simple qq parser.
51    * @param qqName name-value pair of quality query to use for creating the query
52    * @param indexField corresponding index field  
53    */
54   public SimpleQQParser(String qqName, String indexField) {
55     this(new String[] { qqName }, indexField);
56   }
57
58   /* (non-Javadoc)
59    * @see org.apache.lucene.benchmark.quality.QualityQueryParser#parse(org.apache.lucene.benchmark.quality.QualityQuery)
60    */
61   public Query parse(QualityQuery qq) throws ParseException {
62     QueryParser qp = queryParser.get();
63     if (qp==null) {
64       qp = new QueryParser(Version.LUCENE_CURRENT, indexField, new StandardAnalyzer(Version.LUCENE_CURRENT));
65       queryParser.set(qp);
66     }
67     BooleanQuery bq = new BooleanQuery();
68     for (int i = 0; i < qqNames.length; i++)
69       bq.add(qp.parse(QueryParser.escape(qq.getValue(qqNames[i]))), BooleanClause.Occur.SHOULD);
70     
71     return bq;
72   }
73
74 }