org.apache.solr.request.SolrQueryResponse Maven / Gradle / Ivy
The newest version!
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.request;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import java.util.*;
/**
* SolrQueryResponse
is used by a query handler to return
* the response to a query request.
*
*
* Note On Returnable Data...
* A SolrQueryResponse
may contain the following types of
* Objects generated by the SolrRequestHandler
that processed
* the request.
*
*
* - {@link String}
* - {@link Integer}
* - {@link Long}
* - {@link Float}
* - {@link Double}
* - {@link Boolean}
* - {@link Date}
* - {@link org.apache.solr.search.DocList}
* - {@link org.apache.solr.common.SolrDocument} (since 1.3)
* - {@link org.apache.solr.common.SolrDocumentList} (since 1.3)
* - {@link Map} containing any of the items in this list
* - {@link NamedList} containing any of the items in this list
* - {@link Collection} containing any of the items in this list
* - Array containing any of the items in this list
* - null
*
*
* Other data types may be added to the SolrQueryResponse, but there is no guarantee
* that QueryResponseWriters will be able to deal with unexpected types.
*
*
* @version $Id: SolrQueryResponse.java 826321 2009-10-18 00:31:01Z koji $
* @since solr 0.9
*/
public class SolrQueryResponse {
/**
* Container for user defined values
* @see #getValues
* @see #add
* @see #setAllValues
* @see Note on Returnable Data
*/
protected NamedList values = new SimpleOrderedMap();
/**
* Container for storing information that should be logged by Solr before returning.
*/
protected NamedList toLog = new SimpleOrderedMap();
protected Set defaultReturnFields;
// error if this is set...
protected Exception err;
/**
* Should this response be tagged with HTTP caching headers?
*/
protected boolean httpCaching=true;
/***
// another way of returning an error
int errCode;
String errMsg;
***/
public SolrQueryResponse() {
}
/**
* Gets data to be returned in this response
* @see Note on Returnable Data
*/
public NamedList getValues() { return values; }
/**
* Sets data to be returned in this response
* @see Note on Returnable Data
*/
public void setAllValues(NamedList nameValuePairs) {
values=nameValuePairs;
}
/**
* Sets the document field names of fields to return by default when
* returning DocLists
*/
public void setReturnFields(Set fields) {
defaultReturnFields=fields;
}
// TODO: should this be represented as a String[] such
// that order can be maintained if needed?
/**
* Gets the document field names of fields to return by default when
* returning DocLists
*/
public Set getReturnFields() {
return defaultReturnFields;
}
/**
* Appends a named value to the list of named values to be returned.
* @param name the name of the value - may be null if unnamed
* @param val the value to add - also may be null since null is a legal value
* @see Note on Returnable Data
*/
public void add(String name, Object val) {
values.add(name,val);
}
/**
* Causes an error to be returned instead of the results.
*/
public void setException(Exception e) {
err=e;
}
/**
* Returns an Exception if there was a fatal error in processing the request.
* Returns null if the request succeeded.
*/
public Exception getException() {
return err;
}
/**
* The endtime of the request in milliseconds.
* Used to calculate query time.
* @see #setEndTime(long)
* @see #getEndTime()
*/
protected long endtime;
/**
* Get the time in milliseconds when the response officially finished.
*/
public long getEndTime() {
if (endtime==0) {
setEndTime();
}
return endtime;
}
/**
* Stop the timer for how long this query took.
* @see #setEndTime(long)
*/
public long setEndTime() {
return setEndTime(System.currentTimeMillis());
}
/**
* Set the in milliseconds when the response officially finished.
* @see #setEndTime()
*/
public long setEndTime(long endtime) {
if (endtime!=0) {
this.endtime=endtime;
}
return this.endtime;
}
/** Repsonse header to be logged */
public NamedList getResponseHeader() {
SimpleOrderedMap