Apache Solr

Apache Solr Standard Query Parser Example

1. Introduction

In this example, we are going to talk about one key component of Apache Solr: the Standard Query Parser. It is also known as “Lucene” query parser because Apache Solr is built upon Apache Lucene. Then we will show you how to use it in queries against our sample articles collection.

 

2. Technologies Used

The steps and commands described in this example are for Apache Solr 8.5 on Windows 10. The JDK version we use to run the SolrCloud in this example is OpenJDK 13.

Before we start, please make sure your computer meets the system requirements. Also, please download the binary release of Apache Solr 8.5.

In addition, it will save you some time if you can follow the Apache Solr Clustering Example to get a SolrCloud up and running on your local machine.

3. Apache Solr Standard Query Parser

3.1 The Basics

A search process starts with a user’s search request. A search request is handled by a request handler in Apache Solr. To process a search query, a request handler calls a query parser to interpret the terms and parameters of a query. There are several different built-in query parsers in Apache Solr and the standard query parser is the default one to be used. Different query parsers have different syntax. The syntax of the standard query parser is more intuitive but less error-tolerant comparing to other query parsers such as DisMax query parser.

3.2 Standard Query Parser Parameters

One of the responsibilities of a query parser is to interpret the parameters of a query. There are several Common Query Parameters supported by all query parsers. We list some of them as well as the parameters specific to the standard query parser in the table below:

Parameter NameDescription
qMandatory. Defines a query using standard query syntax.
q.opOperator for query expressions: “AND” or “OR”.
dfThe default search field.
sowSplit on whitespace. The default is false; term sequences as a whole are passed to text analysis, so functions such as multi-word synonyms can work on term sequences. If set to true, text analysis is invoked separately for each individual whitespace-separated term.
flSpace-separated or comma-separated list of field names included in a query response
sortSearch results order: “asc” or “desc”
debugInclude debug information in search response. Possible values: “query”, “timing”, “results” or “all”. The default behavior is not to include debugging information.
Table. 1. Standard Query Parser Parameters

4. Standard Query Parser Examples

4.1 Preparation

We suggest downloading the sample data file of this article containing jcg_example_configs.zip and articles.csv. Then follow the steps described in Section 4.1 Upload a ConfigSet and Section 4.2 Indexing Data in Apache Solr Function Query Example to get the article’s data indexed and stored in jcgArticles collection. In the jcg_example_configs, the default search field of /select request handler is set to field title. Additionally, instead of using Solr Admin in the browser, we use a command-line tool curl to run all queries in the examples of this article.

4.2 Specifying Terms

4.2.1 Basic Term Searches

When a query is sent to the standard query parser, it is broken up into terms and operators. There are two types of terms: single terms and phrases.

  • A single term: a single word such as java or geek
  • A phrase: double-quoted words such as "apache solr"

Multiple terms can be combined together with Boolean operators to form more complex queries.

The following example searches a single term java:

curl http://localhost:8983/solr/jcgArticles/select?q=java

The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":21,
    "params":{
      "q":"java"}},
  "response":{"numFound":4,"start":0,"maxScore":0.63013375,"docs":[
      {
        "id":"055357342Y",
        "category":["java"],
        "title":["Java StringTokenizer Example"],
        "published":true,
        "author":["Kevin Yang"],
        "views":699,
        "likes":30,
        "dislikes":0,
        "comments":0,
        "publish_date":"2020-06-01T00:00:00Z",
        "_version_":1669837407943393280},
      {
        "id":"0553573333",
        "category":["java"],
        "title":["Java Array Example"],
        "published":true,
        "author":["Kevin Yang"],
        "views":2560,
        "likes":256,
        "dislikes":6,
        "comments":3,
        "publish_date":"2020-05-06T00:00:00Z",
        "_version_":1669837409270890496},
      {
        "id":"0553579908",
        "category":["java"],
        "title":["Java RMI Example"],
        "published":true,
        "author":["Kevin Yang"],
        "views":389,
        "likes":26,
        "dislikes":3,
        "comments":0,
        "publish_date":"2010-05-23T00:00:00Z",
        "_version_":1669837409283473408},
      {
        "id":"0553292123",
        "category":["java"],
        "title":["Java HashMap Example"],
        "published":true,
        "author":["Evan Swing"],
        "views":5897,
        "likes":1033,
        "dislikes":1,
        "comments":86,
        "publish_date":"2018-03-23T00:00:00Z",
        "_version_":1669837409288716288}]
  }}

The following example searches a phase "java array":

curl -G -v http://localhost:8983/solr/jcgArticles/select --data-urlencode "q=\"java array\""

Note that --data-urlencode is used to encode the query parameter.-G puts the post data in the URL and use GET. Also -v is used to show tracing info which is very helpful when debugging.

The output would be:

* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8983 (#0)
> GET /solr/jcgArticles/select?q=%22java%20array%22 HTTP/1.1
> Host: localhost:8983
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Security-Policy: default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self'; font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< Content-Type: text/plain;charset=utf-8
< Content-Length: 535
<
{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":14,
    "params":{
      "q":"\"java array\""}},
  "response":{"numFound":1,"start":0,"maxScore":0.8502861,"docs":[
      {
        "id":"0553573333",
        "category":["java"],
        "title":["Java Array Example"],
        "published":true,
        "author":["Kevin Yang"],
        "views":2560,
        "likes":256,
        "dislikes":6,
        "comments":3,
        "publish_date":"2020-05-06T00:00:00Z",
        "_version_":1669837409270890496}]
  }}
* Connection #0 to host localhost left intact

In a real-world application, searching by terms or phrases are often not enough. Is it possible for Solr to support wildcard searches or fuzzy searches? The answer is yes. Term modifiers are ways to add this flexibility. The examples below will show you how they work.

4.2.2 Wildcard Searches

There are two types of wildcard supported by the standard query parser: ? and *. Please see the examples below:

? – matches a single character

curl http://localhost:8983/solr/jcgArticles/select?q=s?lr

This query matches any word starts with s, followed by a single character and then ends with lr. The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":21,
    "params":{
      "q":"s?lr"}},
  "response":{"numFound":3,"start":0,"maxScore":1.0,"docs":[
      {
        "id":"0812521390",
        "category":["solr"],
        "title":["The Solr Runbook"],
        "published":false,
        "author":["James Cook"],
        "views":300,
        "likes":10,
        "dislikes":0,
        "comments":0,
        "publish_date":"2020-02-09T00:00:00Z",
        "_version_":1669837408365969408},
      {
        "id":"0441385532",
        "category":["solr"],
        "title":["The Solr REST API"],
        "published":false,
        "author":["Steven Thomas"],
        "views":530,
        "likes":30,
        "dislikes":20,
        "comments":2,
        "publish_date":"2020-01-06T00:00:00Z",
        "_version_":1669837408379600896},
      {
        "id":"0812550706",
        "category":["solr"],
        "title":["The Apache Solr Cookbook"],
        "published":true,
        "author":["James Cook"],
        "views":180,
        "likes":6,
        "dislikes":1,
        "comments":1,
        "publish_date":"2019-12-10T00:00:00Z",
        "_version_":1669837409291862016}]
  }}

* – matches zero or more sequential characters

curl http://localhost:8983/solr/jcgArticles/select?q=s*r

This query matches any word starts with s and ends with r. As we can see from the output below, StringTokenizer is a matched result.

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":34,
    "params":{
      "q":"s*r"}},
  "response":{"numFound":4,"start":0,"maxScore":1.0,"docs":[
      {
        "id":"055357342Y",
        "category":["java"],
        "title":["Java StringTokenizer Example"],
        "published":true,
        "author":["Kevin Yang"],
        "views":699,
        "likes":30,
        "dislikes":0,
        "comments":0,
        "publish_date":"2020-06-01T00:00:00Z",
        "_version_":1669837407943393280},
      {
        "id":"0812521390",
        "category":["solr"],
        "title":["The Solr Runbook"],
        "published":false,
        "author":["James Cook"],
        "views":300,
        "likes":10,
        "dislikes":0,
        "comments":0,
        "publish_date":"2020-02-09T00:00:00Z",
        "_version_":1669837408365969408},
      {
        "id":"0441385532",
        "category":["solr"],
        "title":["The Solr REST API"],
        "published":false,
        "author":["Steven Thomas"],
        "views":530,
        "likes":30,
        "dislikes":20,
        "comments":2,
        "publish_date":"2020-01-06T00:00:00Z",
        "_version_":1669837408379600896},
      {
        "id":"0812550706",
        "category":["solr"],
        "title":["The Apache Solr Cookbook"],
        "published":true,
        "author":["James Cook"],
        "views":180,
        "likes":6,
        "dislikes":1,
        "comments":1,
        "publish_date":"2019-12-10T00:00:00Z",
        "_version_":1669837409291862016}]
  }}

4.2.3 Fuzzy Searches

The syntax of a fuzzy search is by appending the tilde symbol ~ to the end of a single-word term. It matches similar terms to the specified term. For example:

curl http://localhost:8983/solr/jcgArticles/select?q=array~

The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":17,
    "params":{
      "q":"array~"}},
  "response":{"numFound":2,"start":0,"maxScore":0.6837484,"docs":[
      {
        "id":"0553573333",
        "category":["java"],
        "title":["Java Array Example"],
        "published":true,
        "author":["Kevin Yang"],
        "views":2560,
        "likes":256,
        "dislikes":6,
        "comments":3,
        "publish_date":"2020-05-06T00:00:00Z",
        "_version_":1669841842345082880},
      {
        "id":"0626166238",
        "category":["java"],
        "title":["Java Arrays Showcases"],
        "published":true,
        "author":["Kevin Yang"],
        "views":565,
        "likes":234,
        "dislikes":8,
        "comments":14,
        "publish_date":"2020-03-06T00:00:00Z",
        "_version_":1669841842373394432}]
  }}

Note that the article with the title “Java ArrayList 101” is not present in the search results. This is because the similarity calculation is based on Damerau–Levenshtein distance.

4.2.4 Proximity Searches

The syntax of a proximity search is by appending the tilde symbol ~ and a numeric value to the end of a search phase. It matches terms within a specific distance (the number of term movements needed) from one another. For example:

curl -G http://localhost:8983/solr/jcgArticles/select --data-urlencode "q=\"java example\"~3"

The query above searches any article title for java and example within 3 words of each other. The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":11,
    "params":{
      "q":"\"java example\"~3"}},
  "response":{"numFound":5,"start":0,"maxScore":0.4862815,"docs":[
      {
        "id":"055357342Y",
        "category":["java"],
        "title":["Java StringTokenizer Example"],
        "published":true,
        "author":["Kevin Yang"],
        "views":699,
        "likes":30,
        "dislikes":0,
        "comments":0,
        "publish_date":"2020-06-01T00:00:00Z",
        "_version_":1669843269986549760},
      {
        "id":"0928237471",
        "category":["java"],
        "title":["Java HashSet Example"],
        "published":true,
        "author":["Evan Swing"],
        "views":3828,
        "likes":123,
        "dislikes":8,
        "comments":2,
        "publish_date":"2018-02-16T00:00:00Z",
        "_version_":1669843269989695488},
      {
        "id":"0553573333",
        "category":["java"],
        "title":["Java Array Example"],
        "published":true,
        "author":["Kevin Yang"],
        "views":2560,
        "likes":256,
        "dislikes":6,
        "comments":3,
        "publish_date":"2020-05-06T00:00:00Z",
        "_version_":1669843269982355456},
      {
        "id":"0553292123",
        "category":["java"],
        "title":["Java HashMap Example"],
        "published":true,
        "author":["Evan Swing"],
        "views":5897,
        "likes":1033,
        "dislikes":1,
        "comments":86,
        "publish_date":"2018-03-23T00:00:00Z",
        "_version_":1669843269998084096},
      {
        "id":"0553579908",
        "category":["java"],
        "title":["Java Remote Method Invocation Example"],
        "published":true,
        "author":["Kevin Yang"],
        "views":389,
        "likes":26,
        "dislikes":3,
        "comments":0,
        "publish_date":"2010-05-23T00:00:00Z",
        "_version_":1669843269993889792}]
  }}

4.2.5 Existence Searches

The syntax of an existing search is by using a wildcard with a field instead of a term. It matches all documents where the specified field has any value. For example:

curl http://localhost:8983/solr/jcgArticles/select?q=author:*

The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":13,
    "params":{
      "q":"author:*"}},
  "response":{"numFound":13,"start":0,"maxScore":1.0,"docs":[
      {
        "id":"0626166238",
        "category":["java"],
        "title":["Java Arrays Showcases"],
        "published":true,
        "author":["Kevin Yang"],
        "views":565,
        "likes":234,
        "dislikes":8,
        "comments":14,
        "publish_date":"2020-03-06T00:00:00Z",
        "_version_":1669843269972918272},
      {
        "id":"055357342Y",
        "category":["java"],
        "title":["Java StringTokenizer Example"],
        "published":true,
        "author":["Kevin Yang"],
        "views":699,
        "likes":30,
        "dislikes":0,
        "comments":0,
        "publish_date":"2020-06-01T00:00:00Z",
        "_version_":1669843269986549760},
      {
        "id":"0928237471",
        "category":["java"],
        "title":["Java HashSet Example"],
        "published":true,
        "author":["Evan Swing"],
        "views":3828,
        "likes":123,
        "dislikes":8,
        "comments":2,
        "publish_date":"2018-02-16T00:00:00Z",
        "_version_":1669843269989695488},
      {
        "id":"0818231712",
        "category":["solr"],
        "title":["Apache SolrCloud Example"],
        "published":true,
        "author":["Kevin Yang"],
        "views":2000,
        "likes":1000,
        "dislikes":10,
        "comments":200,
        "publish_date":"2020-06-05T00:00:00Z",
        "_version_":1669843269990744064},
      {
        "id":"0812521390",
        "category":["solr"],
        "title":["The Solr Runbook"],
        "published":false,
        "author":["James Cook"],
        "views":300,
        "likes":10,
        "dislikes":0,
        "comments":0,
        "publish_date":"2020-02-09T00:00:00Z",
        "_version_":1669843269992841216},
      {
        "id":"0441385532",
        "category":["solr"],
        "title":["The Solr REST API"],
        "published":false,
        "author":["Steven Thomas"],
        "views":530,
        "likes":30,
        "dislikes":20,
        "comments":2,
        "publish_date":"2020-01-06T00:00:00Z",
        "_version_":1669843269993889792},
      {
        "id":"0380014300",
        "category":["solr"],
        "title":["SolrCloud Tutorial"],
        "published":true,
        "author":["Roger Goodwill"],
        "views":2000,
        "likes":1000,
        "dislikes":500,
        "comments":10,
        "publish_date":"2020-06-05T00:00:00Z",
        "_version_":1669843269995986944},
      {
        "id":"0553573333",
        "category":["java"],
        "title":["Java Array Example"],
        "published":true,
        "author":["Kevin Yang"],
        "views":2560,
        "likes":256,
        "dislikes":6,
        "comments":3,
        "publish_date":"2020-05-06T00:00:00Z",
        "_version_":1669843269982355456},
      {
        "id":"0221234283",
        "category":["java"],
        "title":["Java ArrayList 101"],
        "published":true,
        "author":["Kevin Yang"],
        "views":875,
        "likes":65,
        "dislikes":2,
        "comments":2,
        "publish_date":"2020-03-13T00:00:00Z",
        "_version_":1669843269992841216},
      {
        "id":"0553579908",
        "category":["java"],
        "title":["Java Remote Method Invocation Example"],
        "published":true,
        "author":["Kevin Yang"],
        "views":389,
        "likes":26,
        "dislikes":3,
        "comments":0,
        "publish_date":"2010-05-23T00:00:00Z",
        "_version_":1669843269993889792}]
  }}

4.2.6 Range Searches

The syntax of a range search is by specifying an upper bound and a lower bound for a field. It matches documents whose values of the specified field fall within the range. Square brackets [ and ] include the bound values and curly brackets { and } exclude the bound values. Wildcard * can be used for bound values. For example:

curl -G http://localhost:8983/solr/jcgArticles/select --data-urlencode "q=views:{180 TO 300]"

The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":13,
    "params":{
      "q":"views:{180 TO 300]"}},
  "response":{"numFound":1,"start":0,"maxScore":1.0,"docs":[
      {
        "id":"0812521390",
        "category":["solr"],
        "title":["The Solr Runbook"],
        "published":false,
        "author":["James Cook"],
        "views":300,
        "likes":10,
        "dislikes":0,
        "comments":0,
        "publish_date":"2020-02-09T00:00:00Z",
        "_version_":1669843269992841216}]
  }}

We can see from the search results above that the article with views 180 is not returned because we use the curly bracket { to exclude it.

4.2.7 Term Boosting with ^

Search results relevance is one of the key factors we need to consider when we build an application on top of full-text search engines like Apache Solr. When searching for something, one or some of the terms in the search phrase may be more relevant. By using Term Boosting we can control the relevance of a document by boosting its term. The syntax to boot a term is very simple. We just need to use the caret symbol ^ with a boost factor (a positive number) at the end of the term we are searching for. The higher the boost factor, the more relevant the term will be. For example, comparing the search results of the following two queries, we can see that the relevance score of article “Apache SolrCloud Example” in the second query with term boosting set toapache^10 is higher than the score in the first query.

Note that we use an additional parameter fl to specify the fields to be returned in the search results.

The first query:

curl -G http://localhost:8983/solr/jcgArticles/select --data-urlencode "q=apache solr" --data-urlencode fl=title,author,score

The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":17,
    "params":{
      "q":"apache solr",
      "fl":"title,author,score"}},
  "response":{"numFound":4,"start":0,"maxScore":1.2642963,"docs":[
      {
        "title":["The Apache Solr Cookbook"],
        "author":["James Cook"],
        "score":1.2642963},
      {
        "title":["Apache SolrCloud Example"],
        "author":["Kevin Yang"],
        "score":0.76089835},
      {
        "title":["The Solr Runbook"],
        "author":["James Cook"],
        "score":0.5287049},
      {
        "title":["The Solr REST API"],
        "author":["Steven Thomas"],
        "score":0.46526033}]
  }}

The second query:

curl -G http://localhost:8983/solr/jcgArticles/select --data-urlencode "q=apache^10 solr" --data-urlencode fl=title,author,score

The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":23,
    "params":{
      "q":"apache^10 solr",
      "fl":"title,author,score"}},
  "response":{"numFound":4,"start":0,"maxScore":7.608984,"docs":[
      {
        "title":["Apache SolrCloud Example"],
        "author":["Kevin Yang"],
        "score":7.608984},
      {
        "title":["The Apache Solr Cookbook"],
        "author":["James Cook"],
        "score":6.95363},
      {
        "title":["The Solr Runbook"],
        "author":["James Cook"],
        "score":0.5287049},
      {
        "title":["The Solr REST API"],
        "author":["Steven Thomas"],
        "score":0.46526033}]
  }}

4.2.8 Constant Score with ^=

Sometimes we don’t want other relevant factors but just care about a particular search clause. In this case, we can use constant score query as below:

curl -G http://localhost:8983/solr/jcgArticles/select --data-urlencode "q=(title:solr or author:kevin)^=2.0" --data-urlencode fl=title,author,score

As we can see from output, the relevance score of each matching article is set to 2.0:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":28,
    "params":{
      "q":"(title:solr or author:kevin)^=2.0",
      "fl":"title,author,score"}},
  "response":{"numFound":10,"start":0,"maxScore":2.0,"docs":[
      {
        "title":["Java Array Example"],
        "author":["Kevin Yang"],
        "score":2.0},
      {
        "title":["Java ArrayList 101"],
        "author":["Kevin Yang"],
        "score":2.0},
      {
        "title":["Java Remote Method Invocation Example"],
        "author":["Kevin Yang"],
        "score":2.0},
      {
        "title":["Thread"],
        "author":["Kevin Yang"],
        "score":2.0},
      {
        "title":["The Apache Solr Cookbook"],
        "author":["James Cook"],
        "score":2.0},
      {
        "title":["Java Arrays Showcases"],
        "author":["Kevin Yang"],
        "score":2.0},
      {
        "title":["Java StringTokenizer Example"],
        "author":["Kevin Yang"],
        "score":2.0},
      {
        "title":["Apache SolrCloud Example"],
        "author":["Kevin Yang"],
        "score":2.0},
      {
        "title":["The Solr Runbook"],
        "author":["James Cook"],
        "score":2.0},
      {
        "title":["The Solr REST API"],
        "author":["Steven Thomas"],
        "score":2.0}]
  }}

4.3 Querying Specific Fields

Solr stores indexed data in documents and each document consists of several fields. To search for a term in a specific field, we can use the syntax below:

field:term
field:"phrase"

For example, the following query search for articles written by Kevin in the author field:

curl -G http://localhost:8983/solr/jcgArticles/select?q=author:kevin --data-urlencode fl=title,author

The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":13,
    "params":{
      "q":"author:kevin",
      "fl":"title,author"}},
  "response":{"numFound":7,"start":0,"maxScore":0.37576297,"docs":[
      {
        "title":["Java Arrays Showcases"],
        "author":["Kevin Yang"]},
      {
        "title":["Java StringTokenizer Example"],
        "author":["Kevin Yang"]},
      {
        "title":["Apache SolrCloud Example"],
        "author":["Kevin Yang"]},
      {
        "title":["Java Array Example"],
        "author":["Kevin Yang"]},
      {
        "title":["Java ArrayList 101"],
        "author":["Kevin Yang"]},
      {
        "title":["Java Remote Method Invocation Example"],
        "author":["Kevin Yang"]},
      {
        "title":["Thread"],
        "author":["Kevin Yang"]}]
  }}

4.4 Boolean Operators

In addition to interpreting terms in a query, the standard query parser supports the Boolean operators listed in the table below:

Boolean OperatorDescription
AND (&&)Requires both terms on either side of the Boolean operator to be present for a match.
OR (||)Requires that either term (or both terms) be present for a match.
NOT (!)Requires that the following term not be present.
+Requires that the following term be present.
Prohibits the following term. Same as NOT(!) operator.
Table. 2. Boolean Operators

AND (&&) example:

curl -G http://localhost:8983/solr/jcgArticles/select --data-urlencode q="java AND invocation" --data-urlencode fl=title,author

The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":16,
    "params":{
      "q":"java AND invocation",
      "fl":"title,author"}},
  "response":{"numFound":1,"start":0,"maxScore":0.7284967,"docs":[
      {
        "title":["Java Remote Method Invocation Example"],
        "author":["Kevin Yang"]}]
  }}

OR (||) example:

curl -G http://localhost:8983/solr/jcgArticles/select --data-urlencode q="hashmap OR hashset" --data-urlencode fl=title,author

The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":15,
    "params":{
      "q":"hashmap OR hashset",
      "fl":"title,author"}},
  "response":{"numFound":2,"start":0,"maxScore":0.76089835,"docs":[
      {
        "title":["Java HashSet Example"],
        "author":["Evan Swing"]},
      {
        "title":["Java HashMap Example"],
        "author":["Evan Swing"]}]
  }}

NOT (!) example:

curl -G http://localhost:8983/solr/jcgArticles/select --data-urlencode q="solrcloud !example" --data-urlencode fl=title,author

The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":415,
    "params":{
      "q":"solrcloud !example",
      "fl":"title,author"}},
  "response":{"numFound":1,"start":0,"maxScore":0.61218464,"docs":[
      {
        "title":["SolrCloud Tutorial"],
        "author":["Roger Goodwill"]}]
  }}

+ example:

curl -G http://localhost:8983/solr/jcgArticles/select --data-urlencode q="+solrcloud example" --data-urlencode fl=title,author

The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":16,
    "params":{
      "q":"+solrcloud example",
      "fl":"title,author"}},
  "response":{"numFound":2,"start":0,"maxScore":0.9044678,"docs":[
      {
        "title":["Apache SolrCloud Example"],
        "author":["Kevin Yang"]},
      {
        "title":["SolrCloud Tutorial"],
        "author":["Roger Goodwill"]}]
  }}

- example:

curl -G http://localhost:8983/solr/jcgArticles/select --data-urlencode q="solrcloud -apache" --data-urlencode fl=title,author

The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":19,
    "params":{
      "q":"solrcloud -apache",
      "fl":"title,author"}},
  "response":{"numFound":1,"start":0,"maxScore":0.61218464,"docs":[
      {
        "title":["SolrCloud Tutorial"],
        "author":["Roger Goodwill"]}]
  }}

4.5 Grouping Terms

The standard query parser supports grouping search clauses by using parentheses. For example, the following query searches for either example or tutorial articles for solrcloud.

curl -G http://localhost:8983/solr/jcgArticles/select --data-urlencode q="solrcloud AND (example OR tutorial)" --data-urlencode fl=title,author

The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":468,
    "params":{
      "q":"solrcloud AND (example OR tutorial)",
      "fl":"title,author"}},
  "response":{"numFound":2,"start":0,"maxScore":1.4932249,"docs":[
      {
        "title":["SolrCloud Tutorial"],
        "author":["Roger Goodwill"]},
      {
        "title":["Apache SolrCloud Example"],
        "author":["Kevin Yang"]}]
  }}

4.6 Comments in Queries

We can also add c-style comments in a query string. For example:

curl -G http://localhost:8983/solr/jcgArticles/select --data-urlencode q="example /* This is a comment in query string */ OR tutorial" --data-urlencode fl=title,author

The output would be:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":16,
    "params":{
      "q":"example /* This is a comment in query string */ OR tutorial",
      "fl":"title,author"}},
  "response":{"numFound":7,"start":0,"maxScore":0.8810402,"docs":[
      {
        "title":["SolrCloud Tutorial"],
        "author":["Roger Goodwill"]},
      {
        "title":["Java StringTokenizer Example"],
        "author":["Kevin Yang"]},
      {
        "title":["Java HashSet Example"],
        "author":["Evan Swing"]},
      {
        "title":["Apache SolrCloud Example"],
        "author":["Kevin Yang"]},
      {
        "title":["Java Array Example"],
        "author":["Kevin Yang"]},
      {
        "title":["Java HashMap Example"],
        "author":["Evan Swing"]},
      {
        "title":["Java Remote Method Invocation Example"],
        "author":["Kevin Yang"]}]
  }}

5. Download the Sample Data File

Download
You can download the sample data file of this example here: Apache Solr Standard Query Parser Example

Kevin Yang

A software design and development professional with seventeen years’ experience in the IT industry, especially with Java EE and .NET, I have worked for software companies, scientific research institutes and websites.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button