Reader small image

You're reading from  Elasticsearch 5.x Cookbook - Third Edition

Product typeBook
Published inFeb 2017
Publisher
ISBN-139781786465580
Edition3rd Edition
Right arrow
Author (1)
Alberto Paro
Alberto Paro
author image
Alberto Paro

Alberto Paro is an engineer, manager, and software developer. He currently works as technology architecture delivery associate director of the Accenture Cloud First data and AI team in Italy. He loves to study emerging solutions and applications, mainly related to cloud and big data processing, NoSQL, Natural language processing (NLP), software development, and machine learning. In 2000, he graduated in computer science engineering from Politecnico di Milano. Then, he worked with many companies, mainly using Scala/Java and Python on knowledge management solutions and advanced data mining products, using state-of-the-art big data software. A lot of his time is spent teaching how to effectively use big data solutions, NoSQL data stores, and related technologies.
Read more about Alberto Paro

Right arrow

Introduction


Now we have set the mappings and put the data in the indices, we can search.

In this chapter, we will cover the search using different factors: sorting, highlighting, scrolling, suggesting, counting, and deleting. These actions are the core part of Elasticsearch: ultimately, everything in Elasticsearch is about serving the query and returning good-quality results.

This chapter is divided in two parts: the first part shows how to perform an API call-related search, the last part will look at two special query operators that are the basis for building complex queries in the next chapters.

All the recipes in this chapter require us to prepare and populate the required indices: the online code is available on the PacktPub website (https://www.packtpub.com/big-data-and-business-intelligence/elasticsearch-cookbook) or via GitHub (https://github.com/aparo/elasticsearch-cookbook-third-edition). There are scripts to initialize all the required data.

Sorting results


When searching for results, the most common criterion for sorting in Elasticsearch is the relevance to a text query.

Real-world applications often need to control the sorting criteria in scenarios, such as the following:

  • Sorting a user by last name and first name

  • Sorting items by stock symbols, price (ascending, descending)

  • Sorting documents by size, file type, source

Getting ready

You need an up-and-running Elasticsearch installation as used 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 operating system.

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

How to do it...

In order to sort the results, we will perform the following steps:

  1. Add a sort section to your query as follows:

            curl -XGET 'http://127.0.0.1:9200/test-index/test-type/_search?  
            pretty...

Highlighting results


Elasticsearch performs a good job of finding matching results also in big text documents. It's useful for searching text in very large blocks, but to improve user experience, you need to show users the abstract: a small portion of the text that has matched the query. The abstract is a common way to help users to understand how the matched document is relevant to them.

The highlight functionality in Elasticsearch is designed to do this job.

Getting ready

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

To execute curl via a command line you need to install curl for your operating system.

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

How to do it...

For searching and highlighting the results, we will perform the following:

  1. From the command line, we can execute a search...

Executing a scrolling query


Every time a query is executed, the results are calculated and returned to the user. In Elasticsearch, there is not a deterministic order for records: pagination on a big block of values can bring inconsistency between results due to added and deleted documents and also documents with the same score. The scrolling query tries to resolve this kind of problem, giving a special cursor that allows the user to uniquely iterate all the documents.

Getting ready

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

To execute curl via a command line, you need to install curl for your operating system.

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

How to do it...

In order to execute a scrolling query, we will perform the following steps:

  1. From the command line, we can...

Using the search_after functionality


Elasticsearch standard pagination using from and size performs very poorly on large datasets because for every query you need to compute and discard all the results before the from value. The scrolling doesn't have this problem, but it consumes a lot, due to memory search contexts, so it cannot be used for frequent user queries.

To bypass these problems, Elasticsearch 5.x provides the search_after functionality that provides a fast skipping for scrolling results.

Getting ready

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

To execute curl via a command line, you need to install curl for your operating system.

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

How to do it...

In order to execute a scrolling query, we will perform the following steps...

Returning inner hits in results


In Elasticsearch, via nested and child documents, we can have complex data models. Elasticsearch, by default, returns only documents that match the searched type and not the nested/children one that matches the query.

The inner_hits function is introduced in Elasticsearch 5.x to provide this functionality.

Getting ready

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

To execute curl via a command line, you need to install curl for your operating system.

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

How to do it...

To return inner hits during a query, we will perform the following steps:

  1. From the command line, we can execute a call adding inner_hits as follows:

            curl -XPOST 'http://127.0.0.1:9200/test-index/test-
            type/_search?pretty...

Suggesting a correct query


It's very common for users to commit typing errors or to require suggestions for words that they are writing. These issues are solved by Elasticsearch with the suggest functionality.

Getting ready

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

To execute curl via a command line, you need to install curl for your operating system.

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

How to do it...

To suggest relevant terms by query, we will perform the following steps:

  1. From the command line, we can execute a suggest call, as follows:

            curl -XGET 'http://127.0.0.1:9200/test-index/_suggest?
            pretty' -d '{
                "suggest1" : {
                  "text" : "we find tester",
                  "term" : {
                  "field" : "parsedtext...

Counting matched results


It is often required to return only the count of the matched results and not the results themselves.

There are a lot of scenarios involving counting, such as the following:

  • To return the number of something (how many posts for a blog, how many comments for a post)

  • Validating whether some items are available. Are there posts? Are there comments?

Getting ready

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

To execute curl via a command line, you need to install curl for your operating system.

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

How to do it...

In order to execute a counting query, we will perform the following steps:

  1. From the command line, we will execute a count query, as follows:

        curl -XGET 'http://127.0.0.1:9200/test-index/test-type/_count? 
...

Explaining a query


When executing searches, it's very common to have documents that don't match the query as expected. To easily debug these scenarios, Elasticsearch provides the explain query call.

Getting ready

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

To execute curl via a command line, you need to install curl for your operating system.

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

How to do it...

The steps required to execute the explain query call are as follows:

  1. From the command line, we will execute an explain query against a document as follows:

            curl -XGET 'http://127.0.0.1:9200/test-index/test-
            type/1/_explain?pretty' -d '{
                "query": {
                    "term": {
                        "uuid": "11111"
                    }...

Query profiling


A new feature available in Elasticsearch 5.x is the profile API. This allows the user to track the time spent by Elasticsearch in executing a search or an aggregation.

Getting ready

You will need an up-and-running Elasticsearch installation as used in the Downloading and installing Elasticsearch recipe in Chapter 2Downloading and Setup.

To execute curl via a command line, you need to install curl for your operating system.

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

How to do it...

The steps to profile a query are as follows:

  1. From the command line, we will execute a search with the profile set to true, as follows:

            curl -XGET 'http://127.0.0.1:9200/test-index/test-type/_search?
            pretty' -d '{
                "profile": true,
                "query": {
                    "term": {
                        "uuid": "11111"
                    }
...

Deleting by query


We saw how to delete a document in the recipe Deleting a document in Chapter 4, Basic Operations. Deleting a document is very fast but it requires knowing the document ID.

Elasticsearch provides a call to delete all the documents that match a query via an additional module called reindex, which is installed by default.

Getting ready

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

To execute curl via a command-line you need to install curl for your operating system.

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

How to do it...

In order to delete by query, we will perform the following steps:

  1. From the command line, we will execute a query as follows:

        curl -XPOST 'http://127.0.0.1:9200/test-index/test-  
        type/_delete_by_query?pretty' -d '{"query":{"match_all...

Updating by query


In the previous chapter, we saw how to update a document (Chapter 4, Basic Operations,  Update a document recipe).

The update_by_query API call allows the user to execute the update on all the documents that match a query. It is very useful if you need to do the following:

  • Reindex a subset of your records that match a query. It's common if you change your document mapping and need the documents to be reprocessed.

  • Update values of your records the match a query.

This functionality is provided by an additional module called reindex that is installed by default.

Getting ready

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

To execute curl via a command line, you need to install curl for your operating system.

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

How to do it...

Matching all the documents


One of the most common queries, usually in conjunction with a filter, is the match_all query. This kind of query allows the user to return all the documents. It's often used in conjunction with filters.

Getting ready

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

To execute curl via a command line, you need to install curl for your operating system.

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

How to do it...

In order to execute a match_all query, we will perform the following steps:

  1. From the command line, we execute the query, as follows:

            curl -XPOST 'http://127.0.0.1:9200/test-index/test-
            type/_search?pretty' -d '{"query": {"match_all" : {}}}'
    
  2. The result returned by Elasticsearch, if everything works, should be as follows:

      ...

Using a boolean query


Most people using a search engine have at sometime used the syntax with minus (-) and plus (+) to include or exclude query terms. The Boolean query allows the user to programmatically define queries to include, exclude, optionally include (should), or filter in the query.

This kind of query is one of the most important ones because it allows the user to aggregate a lot of simple queries/filters that we will see in this chapter to build a big complex one.

Two main concepts are important in searches: query and filter. The query means that the matched results are scored using an internal Lucene scoring algorithm; for the filter, the results are matched without scoring. Because the filter doesn't need to compute the score, it is generally faster and can be cached.

Getting ready

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

To execute curl via a command line, you need...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Elasticsearch 5.x Cookbook - Third Edition
Published in: Feb 2017Publisher: ISBN-13: 9781786465580
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alberto Paro

Alberto Paro is an engineer, manager, and software developer. He currently works as technology architecture delivery associate director of the Accenture Cloud First data and AI team in Italy. He loves to study emerging solutions and applications, mainly related to cloud and big data processing, NoSQL, Natural language processing (NLP), software development, and machine learning. In 2000, he graduated in computer science engineering from Politecnico di Milano. Then, he worked with many companies, mainly using Scala/Java and Python on knowledge management solutions and advanced data mining products, using state-of-the-art big data software. A lot of his time is spent teaching how to effectively use big data solutions, NoSQL data stores, and related technologies.
Read more about Alberto Paro