Package org.apache.solr.client.solrj.request.json

## 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, see `DirectJsonQueryRequest`

See Also:
  Serialized Form

### Nested Class Summary

### Nested classes/interfaces inherited from class org.apache.solr.client.solrj.SolrRequest

`SolrRequest.METHOD, SolrRequest.SolrClientContext, SolrRequest.SolrRequestType`

### Field Summary

### Fields inherited from class org.apache.solr.client.solrj.SolrRequest

`SUPPORTED_METHODS, useBinaryV2, usev2`

### Constructor Summary

Constructors
`JsonQueryRequest()` Creates a `JsonQueryRequest` with an empty `SolrParams` object
`JsonQueryRequest​(SolrParams params)` Creates a `JsonQueryRequest` using the provided `SolrParams`

### Method Summary

All Methods Instance Methods Concrete Methods
`RequestWriter.ContentWriter` `getContentWriter​(String expectedType)` If a request object wants to do a push write, implement this method.
`JsonQueryRequest` `returnFields​(Iterable<String> fieldNames)` Specify fields which should be returned by the JSON request.
`JsonQueryRequest` `returnFields​(String... fieldNames)` Specify fields which should be returned by the JSON request.
`JsonQueryRequest` `setLimit​(int limit)` Specify how many results should be returned from the JSON request
`void` `setMethod​(SolrRequest.METHOD m)`
`JsonQueryRequest` `setOffset​(int offset)` Specify whether results should be fetched starting from a particular offset (or 'start').
`JsonQueryRequest` `setQuery​(String query)` Specify the query sent as a part of this JSON request
`JsonQueryRequest` `setQuery​(Map<String,​Object> queryJson)` Specify the query sent as a part of this JSON request.
`JsonQueryRequest` `setQuery​(MapWriter queryWriter)` Specify the query sent as a part of this JSON request.
`JsonQueryRequest` `setSort​(String sort)` Specify how results to the JSON request should be sorted before being returned by Solr
`JsonQueryRequest` `withFacet​(String facetName, Map<String,​Object> facetJson)` Specify a facet sent as a part of this JSON request.
`JsonQueryRequest` `withFacet​(String facetName, MapWriter facetWriter)` Specify a facet sent as a part of this JSON request.
`JsonQueryRequest` `withFilter​(String filterQuery)` Add a filter query to run as a part of the JSON request
`JsonQueryRequest` `withFilter​(Map<String,​Object> filterQuery)` Add a filter query to run as a part of the JSON request
`JsonQueryRequest` `withParam​(String name, Object value)` Add a property to the "params" block supported by the JSON query DSL
`JsonQueryRequest` `withStatFacet​(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, getRequestType`

### Methods inherited from class org.apache.solr.client.solrj.SolrRequest

`addHeader, getBasePath, getBasicAuthPassword, getBasicAuthUser, getCollection, getContentStreams, getHeaders, getMethod, getQueryParams, getResponseParser, getStreamingResponseCallback, getUserPrincipal, process, process, setBasePath, setBasicAuthCredentials, setPath, setQueryParams, setResponseParser, setStreamingResponseCallback, setUseBinaryV2, setUserPrincipal, setUseV2`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### JsonQueryRequest

    public JsonQueryRequest()

Creates a `JsonQueryRequest` with an empty `SolrParams` object

#### JsonQueryRequest

    public JsonQueryRequest​(SolrParams params)

Creates a `JsonQueryRequest` using the provided `SolrParams`

### 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` - if `query` is 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` - if `queryJson` is 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` - if `queryWriter` is 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 different `facetName` value will add a new top-level facet. Repeating `facetName` values will cause previous facets with that `facetName` to 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 different `facetName` value will add a new top-level facet. Repeating `facetName` values will cause previous facets with that `facetName` to 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 different `facetName` value will add a new top-level facet. Repeating `facetName` values will cause previous facets with that `facetName` to 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` - if `offset` is 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` - if `limit` is 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` - if `sort` is 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` - if `filterQuery` is 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 request

Example: 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` - if `filterQuery` is 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` - if `fieldNames` is 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 `name` will add a new param name/value to the params subtree. Invocations that repeat a `name` will overwrite the previously specified parameter values associated with that name.

Parameters:
  `name` - the name of the parameter to add
  `value` - the value of the parameter to add. Usually a String, Number (Integer, Long, Double), or Boolean.
Throws:
  `IllegalArgumentException` - if either `name` or `value` are null

#### getContentWriter

    public RequestWriter.ContentWriter getContentWriter​(String expectedType)

Description copied from class: `SolrRequest`
If a request object wants to do a push write, implement this method.

Overrides:
  `getContentWriter` in class `SolrRequest<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:
  `setMethod` in class `SolrRequest<QueryResponse>`