pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / feeds / SimpleSloppyPhraseQueryMaker.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.StringTokenizer;
22
23 import org.apache.lucene.index.Term;
24 import org.apache.lucene.search.PhraseQuery;
25 import org.apache.lucene.search.Query;
26
27 /**
28  * Create sloppy phrase queries for performance test, in an index created using simple doc maker.
29  */
30 public class SimpleSloppyPhraseQueryMaker extends SimpleQueryMaker {
31
32   /* (non-Javadoc)
33    * @see org.apache.lucene.benchmark.byTask.feeds.SimpleQueryMaker#prepareQueries()
34    */
35   @Override
36   protected Query[] prepareQueries() throws Exception {
37     // extract some 100 words from doc text to an array
38     String words[];
39     ArrayList<String> w = new ArrayList<String>();
40     StringTokenizer st = new StringTokenizer(SingleDocSource.DOC_TEXT);
41     while (st.hasMoreTokens() && w.size()<100) {
42       w.add(st.nextToken());
43     }
44     words = w.toArray(new String[0]);
45
46     // create queries (that would find stuff) with varying slops
47     ArrayList<Query> queries = new ArrayList<Query>(); 
48     for (int slop=0; slop<8; slop++) {
49       for (int qlen=2; qlen<6; qlen++) {
50         for (int wd=0; wd<words.length-qlen-slop; wd++) {
51           // ordered
52           int remainedSlop = slop;
53           PhraseQuery q = new PhraseQuery();
54           q.setSlop(slop);
55           int wind = wd;
56           for (int i=0; i<qlen; i++) {
57             q.add(new Term(DocMaker.BODY_FIELD,words[wind++]));
58             if (remainedSlop>0) {
59               remainedSlop--;
60               wind++;
61             }
62           }
63           queries.add(q);
64           // reversed
65           remainedSlop = slop;
66           q = new PhraseQuery();
67           q.setSlop(slop+2*qlen);
68           wind = wd+qlen+remainedSlop-1;
69           for (int i=0; i<qlen; i++) {
70             q.add(new Term(DocMaker.BODY_FIELD,words[wind--]));
71             if (remainedSlop>0) {
72               remainedSlop--;
73               wind--;
74             }
75           }
76           queries.add(q);
77         }
78       }
79     }
80     return queries.toArray(new Query[0]);
81   }
82
83 }