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

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/handler/component/ResponseBuilder.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
 
/**
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
 
 
18
 
package org.apache.solr.handler.component;
19
 
 
20
 
import org.apache.lucene.search.Query;
21
 
import org.apache.lucene.search.grouping.SearchGroup;
22
 
import org.apache.lucene.search.grouping.TopGroups;
23
 
import org.apache.lucene.util.BytesRef;
24
 
import org.apache.solr.common.SolrDocument;
25
 
import org.apache.solr.common.SolrDocumentList;
26
 
import org.apache.solr.common.util.NamedList;
27
 
import org.apache.solr.common.util.RTimer;
28
 
import org.apache.solr.common.util.SimpleOrderedMap;
29
 
import org.apache.solr.request.SolrQueryRequest;
30
 
import org.apache.solr.response.SolrQueryResponse;
31
 
import org.apache.solr.search.DocListAndSet;
32
 
import org.apache.solr.search.QParser;
33
 
import org.apache.solr.search.SortSpec;
34
 
import org.apache.solr.search.SolrIndexSearcher;
35
 
import org.apache.solr.search.grouping.GroupingSpecification;
36
 
import org.apache.solr.search.grouping.distributed.command.QueryCommandResult;
37
 
 
38
 
import java.util.Collection;
39
 
import java.util.HashMap;
40
 
import java.util.List;
41
 
import java.util.Map;
42
 
 
43
 
/**
44
 
 * This class is experimental and will be changing in the future.
45
 
 *
46
 
 * @version $Id: ResponseBuilder.java 1171968 2011-09-17 12:46:06Z mvg $
47
 
 * @since solr 1.3
48
 
 */
49
 
public class ResponseBuilder
50
 
{
51
 
  public SolrQueryRequest req;
52
 
  public SolrQueryResponse rsp;
53
 
  public boolean doHighlights;
54
 
  public boolean doFacets;
55
 
  public boolean doStats;
56
 
  public boolean doTerms;
57
 
 
58
 
  private boolean needDocList = false;
59
 
  private boolean needDocSet = false;
60
 
  private int fieldFlags = 0;
61
 
  private boolean debug = false;
62
 
 
63
 
  private QParser qparser = null;
64
 
  private String queryString = null;
65
 
  private Query query = null;
66
 
  private List<Query> filters = null;
67
 
  private SortSpec sortSpec = null;
68
 
  private GroupingSpecification groupingSpec;
69
 
 
70
 
  private DocListAndSet results = null;
71
 
  private NamedList<Object> debugInfo = null;
72
 
  private RTimer timer = null;
73
 
 
74
 
  private Query highlightQuery = null;
75
 
 
76
 
  public List<SearchComponent> components;
77
 
 
78
 
  //////////////////////////////////////////////////////////
79
 
  //////////////////////////////////////////////////////////
80
 
  //// Distributed Search section
81
 
  //////////////////////////////////////////////////////////
82
 
  //////////////////////////////////////////////////////////
83
 
 
84
 
  public static final String FIELD_SORT_VALUES = "fsv";
85
 
  public static final String SHARDS = "shards";
86
 
  public static final String IDS = "ids";
87
 
 
88
 
  /***
89
 
  public static final String NUMDOCS = "nd";
90
 
  public static final String DOCFREQS = "tdf";
91
 
  public static final String TERMS = "terms";
92
 
  public static final String EXTRACT_QUERY_TERMS = "eqt";
93
 
  public static final String LOCAL_SHARD = "local";
94
 
  public static final String DOC_QUERY = "dq";
95
 
  ***/
96
 
 
97
 
  public static int STAGE_START           = 0;
98
 
  public static int STAGE_PARSE_QUERY     = 1000;
99
 
  public static int STAGE_TOP_GROUPS = 1500;
100
 
  public static int STAGE_EXECUTE_QUERY   = 2000;
101
 
  public static int STAGE_GET_FIELDS      = 3000;
102
 
  public static int STAGE_DONE            = Integer.MAX_VALUE;
103
 
 
104
 
  public int stage;  // What stage is this current request at?
105
 
 
106
 
  //The address of the Shard
107
 
  public String[] shards;
108
 
  public int shards_rows = -1;
109
 
  public int shards_start = -1;
110
 
  public List<ShardRequest> outgoing;  // requests to be sent
111
 
  public List<ShardRequest> finished;  // requests that have received responses from all shards
112
 
 
113
 
 
114
 
  public int getShardNum(String shard) {
115
 
    for (int i=0; i<shards.length; i++) {
116
 
      if (shards[i]==shard || shards[i].equals(shard)) return i;
117
 
    }
118
 
    return -1;
119
 
  }
120
 
 
121
 
  public void addRequest(SearchComponent me, ShardRequest sreq) {
122
 
    outgoing.add(sreq);
123
 
    if ((sreq.purpose & ShardRequest.PURPOSE_PRIVATE)==0) {
124
 
      // if this isn't a private request, let other components modify it.
125
 
      for (SearchComponent component : components) {
126
 
        if (component != me) {
127
 
          component.modifyRequest(this, me, sreq);
128
 
        }
129
 
      }
130
 
    }
131
 
  }
132
 
 
133
 
  public GlobalCollectionStat globalCollectionStat;
134
 
 
135
 
  public Map<Object, ShardDoc> resultIds;
136
 
  // Maps uniqueKeyValue to ShardDoc, which may be used to
137
 
  // determine order of the doc or uniqueKey in the final
138
 
  // returned sequence.
139
 
  // Only valid after STAGE_EXECUTE_QUERY has completed.
140
 
 
141
 
 
142
 
  public FacetComponent.FacetInfo _facetInfo;
143
 
  /* private... components that don't own these shouldn't use them */
144
 
  SolrDocumentList _responseDocs;
145
 
  StatsInfo _statsInfo;
146
 
  TermsComponent.TermsHelper _termsHelper;
147
 
 
148
 
  // Context fields for grouping
149
 
  public final Map<String, Collection<SearchGroup<String>>> mergedSearchGroups = new HashMap<String, Collection<SearchGroup<String>>>();
150
 
  public final Map<String, Map<SearchGroup<String>, String>> searchGroupToShard = new HashMap<String, Map<SearchGroup<String>, String>>();
151
 
  public final Map<String, TopGroups<String>> mergedTopGroups = new HashMap<String, TopGroups<String>>();
152
 
  public final Map<String, QueryCommandResult> mergedQueryCommandResults = new HashMap<String, QueryCommandResult>();
153
 
  public final Map<Object, SolrDocument> retrievedDocuments = new HashMap<Object, SolrDocument>();
154
 
 
155
 
  /**
156
 
   * Utility function to add debugging info.  This will make sure a valid
157
 
   * debugInfo exists before adding to it.
158
 
   */
159
 
  public void addDebugInfo( String name, Object val )
160
 
  {
161
 
    if( debugInfo == null ) {
162
 
      debugInfo = new SimpleOrderedMap<Object>();
163
 
    }
164
 
    debugInfo.add( name, val );
165
 
  }
166
 
 
167
 
  //-------------------------------------------------------------------------
168
 
  //-------------------------------------------------------------------------
169
 
 
170
 
  public boolean isDebug() {
171
 
    return debug;
172
 
  }
173
 
 
174
 
  public void setDebug(boolean debug) {
175
 
    this.debug = debug;
176
 
  }
177
 
 
178
 
  public NamedList<Object> getDebugInfo() {
179
 
    return debugInfo;
180
 
  }
181
 
 
182
 
  public void setDebugInfo(NamedList<Object> debugInfo) {
183
 
    this.debugInfo = debugInfo;
184
 
  }
185
 
 
186
 
  public int getFieldFlags() {
187
 
    return fieldFlags;
188
 
  }
189
 
 
190
 
  public void setFieldFlags(int fieldFlags) {
191
 
    this.fieldFlags = fieldFlags;
192
 
  }
193
 
 
194
 
  public List<Query> getFilters() {
195
 
    return filters;
196
 
  }
197
 
 
198
 
  public void setFilters(List<Query> filters) {
199
 
    this.filters = filters;
200
 
  }
201
 
 
202
 
  public Query getHighlightQuery() {
203
 
    return highlightQuery;
204
 
  }
205
 
 
206
 
  public void setHighlightQuery(Query highlightQuery) {
207
 
    this.highlightQuery = highlightQuery;
208
 
  }
209
 
 
210
 
  public boolean isNeedDocList() {
211
 
    return needDocList;
212
 
  }
213
 
 
214
 
  public void setNeedDocList(boolean needDocList) {
215
 
    this.needDocList = needDocList;
216
 
  }
217
 
 
218
 
  public boolean isNeedDocSet() {
219
 
    return needDocSet;
220
 
  }
221
 
 
222
 
  public void setNeedDocSet(boolean needDocSet) {
223
 
    this.needDocSet = needDocSet;
224
 
  }
225
 
 
226
 
  public QParser getQparser() {
227
 
    return qparser;
228
 
  }
229
 
 
230
 
  public void setQparser(QParser qparser) {
231
 
    this.qparser = qparser;
232
 
  }
233
 
 
234
 
  public String getQueryString() {
235
 
    return queryString;
236
 
  }
237
 
 
238
 
  public void setQueryString(String qstr) {
239
 
    this.queryString = qstr;
240
 
  }
241
 
 
242
 
  public Query getQuery() {
243
 
    return query;
244
 
  }
245
 
 
246
 
  public void setQuery(Query query) {
247
 
    this.query = query;
248
 
  }
249
 
 
250
 
  public DocListAndSet getResults() {
251
 
    return results;
252
 
  }
253
 
 
254
 
  public void setResults(DocListAndSet results) {
255
 
    this.results = results;
256
 
  }
257
 
 
258
 
  public SortSpec getSortSpec() {
259
 
    return sortSpec;
260
 
  }
261
 
 
262
 
  public void setSortSpec(SortSpec sort) {
263
 
    this.sortSpec = sort;
264
 
  }
265
 
 
266
 
  public GroupingSpecification getGroupingSpec() {
267
 
    return groupingSpec;
268
 
  }
269
 
 
270
 
  public void setGroupingSpec(GroupingSpecification groupingSpec) {
271
 
    this.groupingSpec = groupingSpec;
272
 
  }
273
 
 
274
 
  public boolean grouping() {
275
 
    return groupingSpec != null;
276
 
  }
277
 
 
278
 
  public RTimer getTimer() {
279
 
    return timer;
280
 
  }
281
 
 
282
 
  public void setTimer(RTimer timer) {
283
 
    this.timer = timer;
284
 
  }
285
 
 
286
 
 
287
 
  public static class GlobalCollectionStat {
288
 
    public final long numDocs;
289
 
 
290
 
    public final Map<String, Long> dfMap;
291
 
 
292
 
    public GlobalCollectionStat(int numDocs, Map<String, Long> dfMap) {
293
 
      this.numDocs = numDocs;
294
 
      this.dfMap = dfMap;
295
 
    }
296
 
  }
297
 
 
298
 
  /**
299
 
   * Creates a SolrIndexSearcher.QueryCommand from this
300
 
   * ResponseBuilder.  TimeAllowed is left unset.
301
 
   */
302
 
  public SolrIndexSearcher.QueryCommand getQueryCommand() {
303
 
    SolrIndexSearcher.QueryCommand cmd = new SolrIndexSearcher.QueryCommand();
304
 
    cmd.setQuery( getQuery() )
305
 
      .setFilterList( getFilters() )
306
 
      .setSort( getSortSpec().getSort() )
307
 
      .setOffset( getSortSpec().getOffset() )
308
 
      .setLen( getSortSpec().getCount() )
309
 
      .setFlags( getFieldFlags() )
310
 
      .setNeedDocSet( isNeedDocSet() );
311
 
    return cmd;
312
 
  }
313
 
 
314
 
  /**
315
 
   * Sets results from a SolrIndexSearcher.QueryResult.
316
 
   */
317
 
  public void setResult( SolrIndexSearcher.QueryResult result ) {
318
 
    setResults( result.getDocListAndSet() );
319
 
    if( result.isPartialResults() ) {
320
 
      rsp.getResponseHeader().add( "partialResults", Boolean.TRUE );
321
 
    }
322
 
  }
323
 
}