Using a simple query string query

Typically, the programmer has the control on building complex query using Boolean query and the other query types. Thus, Elasticsearch provides two kinds of queries that give the user the ability to create string queries with several operators in it.

These kinds of queries are very common on advanced search engine usage such as Google that allows to use + and - operators on terms.

Getting ready

You need an up-and-running Elasticsearch installation as we described in the Downloading and installing Elasticsearch recipe in Chapter 2, Downloading and Setup.

To execute curl via the command line, you need to install curl for your operative system.

To correctly execute the following commands, you need an index populated with the chapter_05/populate_query.sh script available in online code.

How to do it...

For executing a simple query string query, we will perform the following steps:

  1. We want to search for text nice guy, but not excluding the term not. The query will be:
            curl -XPOST 'http://127.0.0.1:9200/test-index/test-
            type/_search?pretty=true' -d '{
              "query": {
                "simple_query_string": {
                  "query": ""nice guy" -not",
                  "fields": [
                    "parsedtext^5",
                    "_all"
                  ],
                  "default_operator": "and"
                }
              }
           }'
    
  2. The result returned by Elasticsearch, if everything is alright, should be:
            { 
                ...truncated..., 
              "hits" : { 
                "total" : 2, 
                "max_score" : 4.90176, 
                "hits" : [ 
                  { 
                     "_index" : "test-index", 
                     "_type" : "test-type", 
                     "_id" : "2", 
                     "_score" : 4.90176, 
                     "_source" : { 
                       "parsedtext" : "Bill Testere nice guy", 
                        ...truncated... 
                    } 
                  }, 
                 { 
                    "_index" : "test-index", 
                    "_type" : "test-type", 
                    "_id" : "1", 
                    "_score" : 4.90176, 
                    "_source" : { 
                      "parsedtext" : "Joe Testere nice guy", 
                        ...truncated... 
                    } 
                 } 
               ] 
             } 
            } 
    

How it works...

The simple query string query takes the query text, tokenizes it and builds a Boolean query applying the rules provided in your text query.

It's a good tool if given to the final user to express simple advanced queries. Its parser is very complex, so it's able to extract fragments for exact match to be interpreted as span queries.

The advantage of simple query string query is that the parser always gives you a valid query. The query string query will give you errors if it's malformed.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.28.133