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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.solr.handler.component;
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;
38
import java.util.Collection;
39
import java.util.HashMap;
40
import java.util.List;
44
* This class is experimental and will be changing in the future.
46
* @version $Id: ResponseBuilder.java 1171968 2011-09-17 12:46:06Z mvg $
49
public class ResponseBuilder
51
public SolrQueryRequest req;
52
public SolrQueryResponse rsp;
53
public boolean doHighlights;
54
public boolean doFacets;
55
public boolean doStats;
56
public boolean doTerms;
58
private boolean needDocList = false;
59
private boolean needDocSet = false;
60
private int fieldFlags = 0;
61
private boolean debug = false;
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;
70
private DocListAndSet results = null;
71
private NamedList<Object> debugInfo = null;
72
private RTimer timer = null;
74
private Query highlightQuery = null;
76
public List<SearchComponent> components;
78
//////////////////////////////////////////////////////////
79
//////////////////////////////////////////////////////////
80
//// Distributed Search section
81
//////////////////////////////////////////////////////////
82
//////////////////////////////////////////////////////////
84
public static final String FIELD_SORT_VALUES = "fsv";
85
public static final String SHARDS = "shards";
86
public static final String IDS = "ids";
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";
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;
104
public int stage; // What stage is this current request at?
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
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;
121
public void addRequest(SearchComponent me, ShardRequest 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);
133
public GlobalCollectionStat globalCollectionStat;
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.
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;
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>();
156
* Utility function to add debugging info. This will make sure a valid
157
* debugInfo exists before adding to it.
159
public void addDebugInfo( String name, Object val )
161
if( debugInfo == null ) {
162
debugInfo = new SimpleOrderedMap<Object>();
164
debugInfo.add( name, val );
167
//-------------------------------------------------------------------------
168
//-------------------------------------------------------------------------
170
public boolean isDebug() {
174
public void setDebug(boolean debug) {
178
public NamedList<Object> getDebugInfo() {
182
public void setDebugInfo(NamedList<Object> debugInfo) {
183
this.debugInfo = debugInfo;
186
public int getFieldFlags() {
190
public void setFieldFlags(int fieldFlags) {
191
this.fieldFlags = fieldFlags;
194
public List<Query> getFilters() {
198
public void setFilters(List<Query> filters) {
199
this.filters = filters;
202
public Query getHighlightQuery() {
203
return highlightQuery;
206
public void setHighlightQuery(Query highlightQuery) {
207
this.highlightQuery = highlightQuery;
210
public boolean isNeedDocList() {
214
public void setNeedDocList(boolean needDocList) {
215
this.needDocList = needDocList;
218
public boolean isNeedDocSet() {
222
public void setNeedDocSet(boolean needDocSet) {
223
this.needDocSet = needDocSet;
226
public QParser getQparser() {
230
public void setQparser(QParser qparser) {
231
this.qparser = qparser;
234
public String getQueryString() {
238
public void setQueryString(String qstr) {
239
this.queryString = qstr;
242
public Query getQuery() {
246
public void setQuery(Query query) {
250
public DocListAndSet getResults() {
254
public void setResults(DocListAndSet results) {
255
this.results = results;
258
public SortSpec getSortSpec() {
262
public void setSortSpec(SortSpec sort) {
263
this.sortSpec = sort;
266
public GroupingSpecification getGroupingSpec() {
270
public void setGroupingSpec(GroupingSpecification groupingSpec) {
271
this.groupingSpec = groupingSpec;
274
public boolean grouping() {
275
return groupingSpec != null;
278
public RTimer getTimer() {
282
public void setTimer(RTimer timer) {
287
public static class GlobalCollectionStat {
288
public final long numDocs;
290
public final Map<String, Long> dfMap;
292
public GlobalCollectionStat(int numDocs, Map<String, Long> dfMap) {
293
this.numDocs = numDocs;
299
* Creates a SolrIndexSearcher.QueryCommand from this
300
* ResponseBuilder. TimeAllowed is left unset.
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() );
315
* Sets results from a SolrIndexSearcher.QueryResult.
317
public void setResult( SolrIndexSearcher.QueryResult result ) {
318
setResults( result.getDocListAndSet() );
319
if( result.isPartialResults() ) {
320
rsp.getResponseHeader().add( "partialResults", Boolean.TRUE );