Class JsonQueryRequest
- java.lang.Object
-
- org.apache.solr.client.solrj.SolrRequest<QueryResponse>
-
- org.apache.solr.client.solrj.request.QueryRequest
-
- org.apache.solr.client.solrj.request.json.JsonQueryRequest
-
- All Implemented Interfaces:
Serializable
public class JsonQueryRequest extends QueryRequest
Represents a query using the JSON Query DSL This class constructs the request using setters for individual properties. For a more monolithic approach to constructing the JSON request, seeDirectJsonQueryRequest- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.solr.client.solrj.SolrRequest
SolrRequest.METHOD
-
-
Field Summary
-
Fields inherited from class org.apache.solr.client.solrj.SolrRequest
SUPPORTED_METHODS, useBinaryV2, usev2
-
-
Constructor Summary
Constructors Constructor Description JsonQueryRequest()Creates aJsonQueryRequestwith an emptySolrParamsobjectJsonQueryRequest(SolrParams params)Creates aJsonQueryRequestusing the providedSolrParams
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RequestWriter.ContentWritergetContentWriter(String expectedType)If a request object wants to do a push write, implement this method.JsonQueryRequestreturnFields(Iterable<String> fieldNames)Specify fields which should be returned by the JSON request.JsonQueryRequestreturnFields(String... fieldNames)Specify fields which should be returned by the JSON request.JsonQueryRequestsetLimit(int limit)Specify how many results should be returned from the JSON requestvoidsetMethod(SolrRequest.METHOD m)JsonQueryRequestsetOffset(int offset)Specify whether results should be fetched starting from a particular offset (or 'start').JsonQueryRequestsetQuery(String query)Specify the query sent as a part of this JSON request This method may be called multiple times, but each call overwrites the value specified by previous calls.JsonQueryRequestsetQuery(Map<String,Object> queryJson)Specify the query sent as a part of this JSON request.JsonQueryRequestsetQuery(MapWriter queryWriter)Specify the query sent as a part of this JSON request.JsonQueryRequestsetSort(String sort)Specify how results to the JSON request should be sorted before being returned by SolrJsonQueryRequestwithFacet(String facetName, Map<String,Object> facetJson)Specify a facet sent as a part of this JSON request.JsonQueryRequestwithFacet(String facetName, MapWriter facetWriter)Specify a facet sent as a part of this JSON request.JsonQueryRequestwithFilter(String filterQuery)Add a filter query to run as a part of the JSON request This method may be called multiple times; each call will add a new filter to the requestJsonQueryRequestwithFilter(Map<String,Object> filterQuery)Add a filter query to run as a part of the JSON request This method may be called multiple times; each call will add a new filter to the requestJsonQueryRequestwithParam(String name, Object value)Add a property to the "params" block supported by the JSON query DSL The JSON query DSL has special support for a few query parameters (limit/rows, offset/start, filter/fq, etc.).JsonQueryRequestwithStatFacet(String facetName, String facetValue)Specify a simple stat or aggregation facet to be sent as a part of this JSON request.-
Methods inherited from class org.apache.solr.client.solrj.request.QueryRequest
createResponse, getParams, getPath
-
Methods inherited from class org.apache.solr.client.solrj.SolrRequest
getBasePath, getBasicAuthPassword, getBasicAuthUser, getCollection, getContentStreams, getMethod, getQueryParams, getResponseParser, getStreamingResponseCallback, getUserPrincipal, process, process, setBasePath, setBasicAuthCredentials, setPath, setQueryParams, setResponseParser, setStreamingResponseCallback, setUseBinaryV2, setUserPrincipal, setUseV2
-
-
-
-
Constructor Detail
-
JsonQueryRequest
public JsonQueryRequest()
Creates aJsonQueryRequestwith an emptySolrParamsobject
-
JsonQueryRequest
public JsonQueryRequest(SolrParams params)
Creates aJsonQueryRequestusing the providedSolrParams
-
-
Method Detail
-
setQuery
public JsonQueryRequest setQuery(String query)
Specify the query sent as a part of this JSON request This method may be called multiple times, but each call overwrites the value specified by previous calls.- Parameters:
query- a String in either of two formats: a query string for the default deftype (e.g. "title:solr"), or a localparams query (e.g. "{!lucene df=text v='solr'}" )- Throws:
IllegalArgumentException- ifqueryis null
-
setQuery
public JsonQueryRequest setQuery(Map<String,Object> queryJson)
Specify the query sent as a part of this JSON request. This method may be called multiple times, but each call overwrites the value specified by previous calls.Example: You wish to send the JSON request: "{'limit': 5, 'query': {'lucene': {'df':'genre_s', 'query': 'scifi'}}}". The query subtree of this request is: "{'lucene': {'df': 'genre_s', 'query': 'scifi'}}". You would represent this query JSON as follows:
final Map<String, Object> queryMap = new HashMap<>(); final Map<String, Object> luceneQueryParamMap = new HashMap<>(); queryMap.put("lucene", luceneQueryParamMap); luceneQueryParamMap.put("df", "genre_s"); luceneQueryParamMap.put("query", "scifi");- Parameters:
queryJson- a Map of values representing the query subtree of the JSON request you wish to send.- Throws:
IllegalArgumentException- ifqueryJsonis null.
-
setQuery
public JsonQueryRequest setQuery(MapWriter queryWriter)
Specify the query sent as a part of this JSON request. This method may be called multiple times, but each call overwrites the value specified by previous calls.Example: You wish to send the JSON request: "{'limit': 5, 'query': {'lucene': {'df':'genre_s', 'query': 'scifi'}}}". The query subtree of this request is: "{'lucene': {'df': 'genre_s', 'query': 'scifi'}}". You would represent this query JSON as follows:
final MapWriter queryWriter = new MapWriter() { @Override public void writeMap(EntryWriter ew) throws IOException { ew.put("lucene", (MapWriter) queryParamWriter -> { queryParamWriter.put("df", "genre_s"); queryParamWriter.put("query", "scifi"); }); } };- Parameters:
queryWriter- a MapWriter capable of writing out the query subtree of the JSON request you wish to send.- Throws:
IllegalArgumentException- ifqueryWriteris null.
-
withFacet
public JsonQueryRequest withFacet(String facetName, Map<String,Object> facetJson)
Specify a facet sent as a part of this JSON request. This method may be called multiple times. Each call made with a differentfacetNamevalue will add a new top-level facet. RepeatingfacetNamevalues will cause previous facets with thatfacetNameto be overwritten.Example: You wish to send the JSON request: {"query": "*:*", "facet": { "top_cats":{"type": "terms", "field":"cat"}}}. You would represent (and attach) the facet in this request as follows:
final Map<String, Object> catFacetMap = new HashMap<>(); catFacetMap.put("type", "terms"); catFacetMap.put("field", "cat"); jsonQueryRequest.withStatFacet("top_cats", catFacetMap);- Parameters:
facetName- the name of the top-level facet you'd like to add. Avoid choosing facet names which overload properties already present in the JSON response schema (e.g. "count", "val", "minX", etc.)facetJson- a Map of values representing the facet you wish to add to the request
-
withFacet
public JsonQueryRequest withFacet(String facetName, MapWriter facetWriter)
Specify a facet sent as a part of this JSON request. This method may be called multiple times. Each call made with a differentfacetNamevalue will add a new top-level facet. RepeatingfacetNamevalues will cause previous facets with thatfacetNameto be overwritten.Example: You wish to send the JSON request: {"query": "*:*", "facet": { "top_cats":{"type": "terms", "field":"cat"}}}. You would represent the facet in this request as follows:
final MapWriter facetWriter = new MapWriter() { @Override public void writeMap(EntryWriter ew) throws IOException { ew.put("type", "terms"); ew.put("field", "cat"); } };- Parameters:
facetName- the name of the top-level facet you'd like to add. Avoid choosing facet names which overload properties already present in the JSON response schema (e.g. "count", "val", "minX", etc.)facetWriter- a MapWriter representing the facet you wish to add to the request
-
withStatFacet
public JsonQueryRequest withStatFacet(String facetName, String facetValue)
Specify a simple stat or aggregation facet to be sent as a part of this JSON request. This method may be called multiple times. Each call made with a differentfacetNamevalue will add a new top-level facet. RepeatingfacetNamevalues will cause previous facets with thatfacetNameto be overwritten.Example: You wish to send the JSON request: {"query": "*:*", "facet": {"avg_price": "avg(price)"}}. You would represent the facet in this request as follows:
jsonQueryRequest.withStatFacet("avg_price", "avg(price)");- Parameters:
facetName- the name of the top-level stat/agg facet you'd like to add. Avoid choosing facet names which overload properties already present in the JSON response schema (e.g. "count", "val", "minX", etc.)facetValue- a String representing the stat/agg facet computation to perform.
-
setOffset
public JsonQueryRequest setOffset(int offset)
Specify whether results should be fetched starting from a particular offset (or 'start'). Defaults to 0 if not set.- Parameters:
offset- a non-negative integer representing the offset (or 'start') to use when returning results- Throws:
IllegalArgumentException- ifoffsetis negative
-
setLimit
public JsonQueryRequest setLimit(int limit)
Specify how many results should be returned from the JSON request- Parameters:
limit- a non-negative integer representing the maximum results to return from a search- Throws:
IllegalArgumentException- iflimitis negative
-
setSort
public JsonQueryRequest setSort(String sort)
Specify how results to the JSON request should be sorted before being returned by Solr- Parameters:
sort- a string representing the desired result sort order (e.g. "price asc")- Throws:
IllegalArgumentException- ifsortis null
-
withFilter
public JsonQueryRequest withFilter(String filterQuery)
Add a filter query to run as a part of the JSON request This method may be called multiple times; each call will add a new filter to the request- Parameters:
filterQuery- a String in either of two formats: a query string for the default deftype (e.g. "title:solr"), or a localparams query (e.g. "{!lucene df=text v='solr'}" )- Throws:
IllegalArgumentException- iffilterQueryis null
-
withFilter
public JsonQueryRequest withFilter(Map<String,Object> filterQuery)
Add a filter query to run as a part of the JSON request This method may be called multiple times; each call will add a new filter to the requestExample: You wish to send the JSON request: "{'query':'*:*', 'filter': [{'lucene': {'df':'genre_s', 'query': 'scifi'}}]}". The filter you want to add is: "{'lucene': {'df': 'genre_s', 'query': 'scifi'}}". You would represent this filter query as follows:
final Map<String, Object> filterMap = new HashMap<>(); final Map<String, Object> luceneQueryParamMap = new HashMap<>(); filterMap.put("lucene", luceneQueryParamMap); luceneQueryParamMap.put("df", "genre_s"); luceneQueryParamMap.put("query", "scifi");- Parameters:
filterQuery- a Map of values representing the filter request you wish to send.- Throws:
IllegalArgumentException- iffilterQueryis null
-
returnFields
public JsonQueryRequest returnFields(String... fieldNames)
Specify fields which should be returned by the JSON request. This method may be called multiple times; each call will add a new field to the list of those to be returned.- Parameters:
fieldNames- the field names that should be returned by the request
-
returnFields
public JsonQueryRequest returnFields(Iterable<String> fieldNames)
Specify fields which should be returned by the JSON request. This method may be called multiple times; each call will add a new field to the list of those to be returned.- Parameters:
fieldNames- the field names that should be returned by the request- Throws:
IllegalArgumentException- iffieldNamesis null
-
withParam
public JsonQueryRequest withParam(String name, Object value)
Add a property to the "params" block supported by the JSON query DSL The JSON query DSL has special support for a few query parameters (limit/rows, offset/start, filter/fq, etc.). But many other query parameters are not explicitly covered by the query DSL. This method can be used to add any of these other parameters to the JSON request.This method may be called multiple times; each call with a different
namewill add a new param name/value to the params subtree. Invocations that repeat anamewill overwrite the previously specified parameter values associated with that name.- Parameters:
name- the name of the parameter to addvalue- the value of the parameter to add. Usually a String, Number (Integer, Long, Double), or Boolean.- Throws:
IllegalArgumentException- if eithernameorvalueare null
-
getContentWriter
public RequestWriter.ContentWriter getContentWriter(String expectedType)
Description copied from class:SolrRequestIf a request object wants to do a push write, implement this method.- Overrides:
getContentWriterin classSolrRequest<QueryResponse>- Parameters:
expectedType- This is the type that the RequestWriter would like to get. But, it is OK to send any format
-
setMethod
public void setMethod(SolrRequest.METHOD m)
- Overrides:
setMethodin classSolrRequest<QueryResponse>
-
-