add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / feeds / EnwikiQueryMaker.java
1 package org.apache.lucene.benchmark.byTask.feeds;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
23
24 import org.apache.lucene.analysis.Analyzer;
25 import org.apache.lucene.analysis.standard.StandardAnalyzer;
26 import org.apache.lucene.index.Term;
27 import org.apache.lucene.queryParser.QueryParser;
28 import org.apache.lucene.search.MultiTermQuery;
29 import org.apache.lucene.search.Query;
30 import org.apache.lucene.search.WildcardQuery;
31 import org.apache.lucene.search.spans.SpanFirstQuery;
32 import org.apache.lucene.search.spans.SpanNearQuery;
33 import org.apache.lucene.search.spans.SpanQuery;
34 import org.apache.lucene.search.spans.SpanTermQuery;
35 import org.apache.lucene.benchmark.byTask.tasks.NewAnalyzerTask;
36 import org.apache.lucene.util.Version;
37
38 /**
39  * A QueryMaker that uses common and uncommon actual Wikipedia queries for
40  * searching the English Wikipedia collection. 90 queries total.
41  */
42 public class EnwikiQueryMaker extends AbstractQueryMaker implements
43     QueryMaker {
44
45   // common and a few uncommon queries from wikipedia search logs
46   private static String[] STANDARD_QUERIES = { "Images catbox gif",
47       "Imunisasi haram", "Favicon ico", "Michael jackson", "Unknown artist",
48       "Lily Thai", "Neda", "The Last Song", "Metallica", "Nicola Tesla",
49       "Max B", "Skil Corporation", "\"The 100 Greatest Artists of All Time\"",
50       "\"Top 100 Global Universities\"", "Pink floyd", "Bolton Sullivan",
51       "Frank Lucas Jr", "Drake Woods", "Radiohead", "George Freeman",
52       "Oksana Grigorieva", "The Elder Scrolls V", "Deadpool", "Green day",
53       "\"Red hot chili peppers\"", "Jennifer Bini Taylor",
54       "The Paradiso Girls", "Queen", "3Me4Ph", "Paloma Jimenez", "AUDI A4",
55       "Edith Bouvier Beale: A Life In Pictures", "\"Skylar James Deleon\"",
56       "Simple Explanation", "Juxtaposition", "The Woody Show", "London WITHER",
57       "In A Dark Place", "George Freeman", "LuAnn de Lesseps", "Muhammad.",
58       "U2", "List of countries by GDP", "Dean Martin Discography", "Web 3.0",
59       "List of American actors", "The Expendables",
60       "\"100 Greatest Guitarists of All Time\"", "Vince Offer.",
61       "\"List of ZIP Codes in the United States\"", "Blood type diet",
62       "Jennifer Gimenez", "List of hobbies", "The beatles", "Acdc",
63       "Nightwish", "Iron maiden", "Murder Was the Case", "Pelvic hernia",
64       "Naruto Shippuuden", "campaign", "Enthesopathy of hip region",
65       "operating system", "mouse",
66       "List of Xbox 360 games without region encoding", "Shakepearian sonnet",
67       "\"The Monday Night Miracle\"", "India", "Dad's Army",
68       "Solanum melanocerasum", "\"List of PlayStation Portable Wi-Fi games\"",
69       "Little Pixie Geldof", "Planes, Trains & Automobiles", "Freddy Ingalls",
70       "The Return of Chef", "Nehalem", "Turtle", "Calculus", "Superman-Prime",
71       "\"The Losers\"", "pen-pal", "Audio stream input output", "lifehouse",
72       "50 greatest gunners", "Polyfecalia", "freeloader", "The Filthy Youth" };
73
74   private static Query[] getPrebuiltQueries(String field) {
75     WildcardQuery wcq = new WildcardQuery(new Term(field, "fo*"));
76     wcq .setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);
77     // be wary of unanalyzed text
78     return new Query[] {
79         new SpanFirstQuery(new SpanTermQuery(new Term(field, "ford")), 5),
80         new SpanNearQuery(new SpanQuery[] {
81             new SpanTermQuery(new Term(field, "night")),
82             new SpanTermQuery(new Term(field, "trading")) }, 4, false),
83         new SpanNearQuery(new SpanQuery[] {
84             new SpanFirstQuery(new SpanTermQuery(new Term(field, "ford")), 10),
85             new SpanTermQuery(new Term(field, "credit")) }, 10, false), wcq, };
86   }
87
88   /**
89    * Parse the strings containing Lucene queries.
90    * 
91    * @param qs array of strings containing query expressions
92    * @param a analyzer to use when parsing queries
93    * @return array of Lucene queries
94    */
95   private static Query[] createQueries(List<Object> qs, Analyzer a) {
96     QueryParser qp = new QueryParser(Version.LUCENE_CURRENT, DocMaker.BODY_FIELD, a);
97     List<Object> queries = new ArrayList<Object>();
98     for (int i = 0; i < qs.size(); i++) {
99       try {
100
101         Object query = qs.get(i);
102         Query q = null;
103         if (query instanceof String) {
104           q = qp.parse((String) query);
105
106         } else if (query instanceof Query) {
107           q = (Query) query;
108
109         } else {
110           System.err.println("Unsupported Query Type: " + query);
111         }
112
113         if (q != null) {
114           queries.add(q);
115         }
116
117       } catch (Exception e) {
118         e.printStackTrace();
119       }
120     }
121
122     return queries.toArray(new Query[0]);
123   }
124
125   @Override
126   protected Query[] prepareQueries() throws Exception {
127     // analyzer (default is standard analyzer)
128     Analyzer anlzr = NewAnalyzerTask.createAnalyzer(config.get("analyzer", StandardAnalyzer.class.getName()));
129
130     List<Object> queryList = new ArrayList<Object>(20);
131     queryList.addAll(Arrays.asList(STANDARD_QUERIES));
132     if(!config.get("enwikiQueryMaker.disableSpanQueries", false))
133       queryList.addAll(Arrays.asList(getPrebuiltQueries(DocMaker.BODY_FIELD)));
134     return createQueries(queryList, anlzr);
135   }
136
137 }