JSON Query DSL

The JSON Query DSL provides a simple yet powerful query language for the JSON Request API.

Structure of JSON Query DSL

A JSON query can be:

  • A valid query string for default deftype (the standard query parser in most cases), as in, title:solr.
  • A valid local parameters query string, as in, {!dismax qf=myfield}solr rocks.
  • A JSON object with query parser name and arguments. The special key v in local parameters is replaced by key query in JSON object query, as in this example:
{
  "query-parser-name" : {
     "param1": "value1",
     "param2": "value2",
     "query": "a-json-query",
     "another-param": "another-json-query"
  }
}

Basic Examples

The four requests below are equivalent for searching for solr lucene in a field named content:

  1. Passing all parameters on URI, with "lucene" as the default query parser.

    curl -XGET "http://localhost:8983/solr/books/query?q=content:(solr lucene)"
  2. Using the JSON Query DSL with valid query string for default deftype, with "lucene" as default query parser.

    curl -XGET http://localhost:8983/solr/books/query -d '
    {"query": "content:(solr lucene)"}'
  3. Using JSON Query DSL with valid local parameters query defining the "lucene" query parser.

    curl -XGET http://localhost:8983/solr/books/query -d '
    {"query": "{!lucene df=content v='solr lucene'}"}'
  4. Using JSON Query DSL in verbose way, as a valid JSON object with parser name and arguments.

    curl -XGET http://localhost:8983/solr/books/query -d '
    {"query": {"lucene": {
                "df": "content",
                "query": "solr lucene"
            }
        }}'

Note that the JSON query in the examples above is provided under the key query of JSON Request API.

Nested Queries

Some query parsers accept a query as an argument. JSON Query DSL makes it easier to write and read such complex query.

The three requests below are equivalent for wrapping the above example query (searching for solr lucene in field content) with a boost query:

  1. Passing all parameters on URI.

    http://localhost:8983/solr/books/query?q={!boost b=log(popularity) v='{!lucene df=content}(lucene solr)'}
  2. Converted into JSON Query DSL with use of local parameters. As you can see, the special key v is replaced by key query.

    curl -XGET http://localhost:8983/solr/books/query -d '
    {
        "query" : {
            "boost": {
                "query": {!lucene df=content}(lucene solr),
                "b": "log(popularity)"
            }
        }
    }'
  3. Using a verbose JSON Query DSL without local parameters.

    curl -XGET http://localhost:8983/solr/books/query -d '
    {
        "query": {
            "boost": {
                "query": {
                    "lucene": {
                        "df": "content",
                        "query": "solr lucene"
                    }
                },
                "b": "log(popularity)"
            }
        }
    }'

Compound Queries

With the support of the BoolQParser, the JSON Query DSL can create a very powerful nested query.

This query searches for books where content contains lucene or solr, title contains solr and their ranking must larger than 3.0:

curl -XGET http://localhost:8983/solr/books/query -d '
{
    "query": {
        "bool": {
            "must": [
                "title:solr",
                {"lucene": {"df: "content", query: "lucene solr"}}
            ],
            "must_not": [
                {"frange": {"u": "3.0", query: "ranking"}}
            ]
        }
    }
}'

If lucene is the default query parser query, the above can be rewritten in much less verbose way as in:

curl -XGET http://localhost:8983/solr/books/query -d '
{
    "query": {
        "bool": {
            "must": [
                "title:solr",
                "content:(lucene solr)"
            ],
            "must_not": "{!frange u:3.0}ranking"
        }
    }
}'

Use JSON Query DSL in JSON Request API

JSON Query DSL is not only supported with the key query but also with the key filter of the JSON Request API.

For example, the above query can be rewritten using filter clause like this:

curl -XGET http://localhost:8983/solr/books/query -d '
{
    "query": {
        "bool": {
            "must_not": "{!frange u:3.0}ranking"
        }
    },
    "filter: [
        "title:solr",
        { "lucene" : {"df: "content", query : "lucene solr" }}
    ]
}'

Tagging in JSON Query DSL

Queries and filters might be tagged by inserting JSON object with the single property with name starting with a hash (#). Tags can be applied to the single string syntax and to a full JSON object query as well:

curl -XGET http://localhost:8983/solr/books/query -d '
{
    "filter: [          // this applies `titletag`
        {"#titletag": "title:solr"},
        {               // this applies `tagcontent`
          "#tagcontent":
            {"lucene": {"df:"content", query :"lucene solr"}}
        }
    ]
}'

There might be many comma separated tags like {"#title,tag2,tag3":"title:solr"}. Tags can be used in old facets and in JSON Facets' excludeTags as well. Notice that the leading hash requires to use double quotes as in regular JSON.