add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / tasks / SearchWithSortTask.java
1 package org.apache.lucene.benchmark.byTask.tasks;
2 /**
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import org.apache.lucene.benchmark.byTask.PerfRunData;
20 import org.apache.lucene.benchmark.byTask.feeds.QueryMaker;
21 import org.apache.lucene.search.Sort;
22 import org.apache.lucene.search.SortField;
23
24 /**
25  * Does sort search on specified field.
26  * 
27  */
28 public class SearchWithSortTask extends ReadTask {
29
30   private boolean doScore = true;
31   private boolean doMaxScore = true;
32   private Sort sort;
33
34   public SearchWithSortTask(PerfRunData runData) {
35     super(runData);
36   }
37
38   /**
39    * SortFields: field:type,field:type[,noscore][,nomaxscore]
40    *
41    * If noscore is present, then we turn off score tracking
42    * in {@link org.apache.lucene.search.TopFieldCollector}.
43    * If nomaxscore is present, then we turn off maxScore tracking
44    * in {@link org.apache.lucene.search.TopFieldCollector}.
45    * 
46    * name:string,page:int,subject:string
47    * 
48    */
49   @Override
50   public void setParams(String sortField) {
51     super.setParams(sortField);
52     String[] fields = sortField.split(",");
53     SortField[] sortFields = new SortField[fields.length];
54     int upto = 0;
55     for (int i = 0; i < fields.length; i++) {
56       String field = fields[i];
57       SortField sortField0;
58       if (field.equals("doc")) {
59         sortField0 = SortField.FIELD_DOC;
60       } else if (field.equals("score")) {
61         sortField0 = SortField.FIELD_SCORE;
62       } else if (field.equals("noscore")) {
63         doScore = false;
64         continue;
65       } else if (field.equals("nomaxscore")) {
66         doMaxScore = false;
67         continue;
68       } else {
69         int index = field.lastIndexOf(":");
70         String fieldName;
71         String typeString;
72         if (index != -1) {
73           fieldName = field.substring(0, index);
74           typeString = field.substring(1+index, field.length());
75         } else {
76           throw new RuntimeException("You must specify the sort type ie page:int,subject:string");
77         }
78         int type = getType(typeString);
79         sortField0 = new SortField(fieldName, type);
80       }
81       sortFields[upto++] = sortField0;
82     }
83
84     if (upto < sortFields.length) {
85       SortField[] newSortFields = new SortField[upto];
86       System.arraycopy(sortFields, 0, newSortFields, 0, upto);
87       sortFields = newSortFields;
88     }
89     this.sort = new Sort(sortFields);
90   }
91
92   private int getType(String typeString) {
93     int type;
94     if (typeString.equals("float")) {
95       type = SortField.FLOAT;
96     } else if (typeString.equals("double")) {
97       type = SortField.DOUBLE;
98     } else if (typeString.equals("byte")) {
99       type = SortField.BYTE;
100     } else if (typeString.equals("short")) {
101       type = SortField.SHORT;
102     } else if (typeString.equals("int")) {
103       type = SortField.INT;
104     } else if (typeString.equals("long")) {
105       type = SortField.LONG;
106     } else if (typeString.equals("string")) {
107       type = SortField.STRING;
108     } else if (typeString.equals("string_val")) {
109       type = SortField.STRING_VAL;
110     } else {
111       throw new RuntimeException("Unrecognized sort field type " + typeString);
112     }
113     return type;
114   }
115
116   @Override
117   public boolean supportsParams() {
118     return true;
119   }
120
121   @Override
122   public QueryMaker getQueryMaker() {
123     return getRunData().getQueryMaker(this);
124   }
125
126   @Override
127   public boolean withRetrieve() {
128     return false;
129   }
130
131   @Override
132   public boolean withSearch() {
133     return true;
134   }
135
136   @Override
137   public boolean withTraverse() {
138     return false;
139   }
140
141   @Override
142   public boolean withWarm() {
143     return false;
144   }
145
146   @Override
147   public boolean withScore() {
148     return doScore;
149   }
150
151   @Override
152   public boolean withMaxScore() {
153     return doMaxScore;
154   }
155   
156   @Override
157   public Sort getSort() {
158     if (sort == null) {
159       throw new IllegalStateException("No sort field was set");
160     }
161     return sort;
162   }
163
164 }