Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
ElasticSearch Server
ElasticSearch Server

ElasticSearch Server: Whether you're experienced in search servers or a newcomer, this book empowers you to get to grips with the speed and flexibility of ElasticSearch. A reader-friendly approach, including lots of hands-on examples, makes learning a pleasure.

eBook
€19.99 €28.99
Paperback
€37.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

ElasticSearch Server

Chapter 2. Searching Your Data

In the previous chapter we installed and configured our cluster. We also prepared our mappings and indexed our data. We can now do the thing you first had in mind when you chose ElasticSearch, searching! In this chapter you will learn how to query ElasticSearch. Of course, you could say, "Hey, I can just run curl –XGET 'http://localhost:9200/_search?q=first+query' and get all the data I am interested in", and you would be right. However, ElasticSearch supports a wide variety of queries both simple and complicated. In this chapter we will start to get used to some of the search capabilities that ElasticSearch exposes. By the end of this chapter you will have learned:

  • How to query ElasticSearch using its Query DSL

  • How to use basic queries

  • How to use compound queries

  • How to filter your results and why it is important

  • How to change the sorting of your results

  • How to use scripts in ElasticSearch

Understanding the querying and indexing process


Before we see how to search for data, it would be good to understand how the documents and queries sent to ElasticSearch are processed. If you already know that, you can skip this part of the chapter.

In order to understand the querying and indexing process, you should understand the following concepts:

  • Indexing: This is the process of preparing the document sent to ElasticSearch and storing it in the index.

  • Searching: This is the process of matching the documents that satisfy the query requirements.

  • Analysis: This is the process of preparing the content of a field and converting the content to terms that can be written into the Lucene index. During indexing, the data in the fields is divided into a stream of tokens (words) that are written into the index as terms (tokens with additional information such as position in the input text). The analysis process can consist of the following steps:

    • Tokenization: During this stage, the input text...

Mappings


If not stated otherwise, the following mappings will be used for the rest of the chapter:

{
  "book" : {
    "_index" : {
      "enabled" : true
    },
    "_id" : {
      "index": "not_analyzed",
      "store" : "yes"
    },
    "properties" : {
      "author" : {
        "type" : "string"
      },
      "characters" : {
        "type" : "string"
      },
      "copies" : {
        "type" : "long",
        "ignore_malformed" : false
      },
      "otitle" : {
        "type" : "string"
      },
      "tags" : {
        "type" : "string"
      },
      "title" : {
        "type" : "string"
      },
      "year" : {
        "type" : "long",
        "ignore_malformed" : false,
        "index" : "analyzed"
      },
      "available" : {
        "type" : "boolean",
        "index" : "analyzed"
      }
    }
  }
}

Note

Please note that the string-based fields will be analyzed if not stated otherwise.

The preceding mappings were used to create the library index. In order to run that, use...

Querying ElasticSearch


Up to now, most of the times we talked to ElasticSearch with the REST API using an HTTP request, we were using JSON-structured data to do that, regardless of whether it was a mappings change, alias creation, or document indexation. A similar situation arises when we want to send more than a simple query to ElasticSearch—we structure it using JSON objects and send it to ElasticSearch. This is called Query DSL. In a broader view, ElasticSearch supports two kinds of queries, basic ones and compound ones. Basic queries such as the term query are used just for querying. We will cover these in the Basic queries section in this chapter. The second type of query is the compound query, such as the bool query, which can combine multiple queries. We will cover these in the Compound queries section in this chapter.

However, this is not the entirety of the picture. In addition to these two types of queries, your query can have filter queries , which are used to narrow your results...

Basic queries


So, we now know what an ElasticSearch query is, how to construct it, and finally, how to send it using an HTTP request. What we don't know yet is what kind of queries ElasticSearch exposes, and thus, what we can use in order to achieve the desired results. In the next few pages of this chapter, we will try to learn which basic queries ElasticSearch allows us to use and what we can do with them.

The term query

The term query is one of the simplest queries in ElasticSearch and just matches any document that has a term in a given field. You are familiar with this query type because we used it already, but just to have all the query types in one place. The simplest term query is as follows:

{
 "query" : {
  "term" : {
   "title" : "crime"
  }
 }
}

It will match the documents that have the term "crime" in the title field. Please remember that the term query is not analyzed, so you need to provide the exact term that will match the term in the indexed document. However, you can also...

Filtering your results


We already know how to build queries and searches by using different criteria. We know how scoring works, which document is more important for a given query, and how input text can affect ordering. But sometimes, we want to choose only a subset of our index, and the chosen criterion should not have an influence on scoring. This is the place where filters should be used.

Frankly enough, we should use filters whenever possible. If a given part of the query does not affect scoring, it is a good candidate to turn into a filter. Score calculation complicates things, and filtering is a relatively simple operation like a simple match-don't match calculation. Due to the fact that filtering is done on all index contents, the result of filtering is independent of the found documents and relationship between them. Filters can easily be cached, further increasing the overall performance of filtered queries.

Using filters

To use a filter in any search, just add filter to the query...

Compound queries


As we have already discussed, in addition to simple queries, ElasticSearch exposes a few compound queries that can be used to connect multiple queries together or are used to control the behavior of another query. You may wonder whether you need such functionality. In fact, if you are interested in making your search better, you'll use the following queries somewhere in your journey with ElasticSearch. A simple example is combining a simple term query with a phrase query in order to get better search results. But for now, let's stick to the query description.

The bool query

A bool query allows us to wrap a virtually unbounded number of queries and connect them with a logical value by using one of the following sections:

  • should: The query wrapped into this section may or may not have a match (the number of the queries in the should section that need to match is controlled by the minimum_should_match parameter).

  • must: The query wrapped into this section must match in order...

Sorting data


Now we can build quite complex queries for the whole index, or to part of it, by using filters. We can send these queries to ElasticSearch and analyze the returned data. Until now, this data was organized in the order determined by scoring. This is exactly what we want in most cases. Search should give us the most appropriate documents first. But what can we do if we want to use our search more like a database or set a more sophisticated algorithm for data ordering? Let's check what ElasticSearch can do with sorting.

Default sorting

Let's look at the following query, which returns all the books with at least one of the specified words:

{
 "query" : {
    "terms" : {
       "title" : [ "crime", "front", "punishment" ],
       "minimum_match" : 1
    }
  }
}

Under the hood, ElasticSearch sees this as follows:

{
 "query" : {
    "terms" : {
       "title" : [ "crime", "front", "punishment" ],
       "minimum_match" : 1
    }
  },
  "sort" : [
    { "_score" : "desc" }
  ]
}

Note the...

Using scripts


ElasticSearch has a few functionalities where scripts can be used. You've already seen examples such as updating documents, filtering, and searching. Regardless of the fact that this seems to be advanced, we will take a look at the possibilities given by ElasticSearch. Looking into any request that use scripts, we can spot several fields:

  • script: This field contains the script code.

  • lang: This field informs the engine which language is used. If it is omitted, ElasticSearch assumes mvel.

  • params: This is an object containing parameters. Every defined parameter is available for script by its name. By using parameters, we can write cleaner code. Due to caching, code with parameters performs better than code with embedded constant values.

Available objects

During the execution of the script, ElasticSearch exposes several objects. The ones available for operations connected with searching are as follows:

  • doc (also available as _doc): This is an instance of the org.elasticsearch.search...

Summary


In this chapter we've looked at simple and compound query types that are available in ElasticSearch. In addition to that, we've learned how to filter query results and alter the way documents are sorted. We've also learned how to use scripts in ElasticSearch. In the next chapter, we will look at some of the ways to extend our index structure and search results, such as highlighting, using geographical queries or implementing autocomplete functionality with ElasticSearch.

Left arrow icon Right arrow icon

Key benefits

  • Learn the basics of ElasticSearch like data indexing, analysis, and dynamic mapping
  • Query and filter ElasticSearch for more accurate and precise search results
  • Learn how to monitor and manage ElasticSearch clusters and troubleshoot any problems that arise

Description

ElasticSearch is an open source search server built on Apache Lucene. It was built to provide a scalable search solution with built-in support for near real-time search and multi-tenancy.Jumping into the world of ElasticSearch by setting up your own custom cluster, this book will show you how to create a fast, scalable, and flexible search solution. By learning the ins-and-outs of data indexing and analysis, "ElasticSearch Server" will start you on your journey to mastering the powerful capabilities of ElasticSearch. With practical chapters covering how to search data, extend your search, and go deep into cluster administration and search analysis, this book is perfect for those new and experienced with search servers.In "ElasticSearch Server" you will learn how to revolutionize your website or application with faster, more accurate, and flexible search functionality. Starting with chapters on setting up your own ElasticSearch cluster and searching and extending your search parameters you will quickly be able to create a fast, scalable, and completely custom search solution.Building on your knowledge further you will learn about ElasticSearch's query API and become confident using powerful filtering and faceting capabilities. You will develop practical knowledge on how to make use of ElasticSearch's near real-time capabilities and support for multi-tenancy.Your journey then concludes with chapters that help you monitor and tune your ElasticSearch cluster as well as advanced topics such as shard allocation, gateway configuration, and the discovery module.

Who is this book for?

This book is written for developers who wish to leverage ElasticSearch to create a fast and flexible search solution. If you are looking to learn ElasticSearch or become more proficient then this book is for you. You do not need know anything about ElasticSeach, Java, or Apache Lucene in order to use this book, though basic knowledge about databases and queries is required.

What you will learn

  • Configuration and creation of an ElasticSearch Index
  • Using ElasticSearch query DSL to make all kinds of queries
  • Efficient and precise use of filters without loss of performance
  • Implementing the autocomplete functionality
  • Highlight data and geographical search information for better results
  • Understand how ElasticSearch returns results and how to validate those results
  • Faceting and ‚Äúmore like this‚Äù functionalities to get more from your search and improve your client s search experience
  • Monitor your cluster state and health by using ElasticSearch API as well as third party monitoring solutions

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 21, 2013
Length: 318 pages
Edition : 1st
Language : English
ISBN-13 : 9781849518444
Vendor :
Apache
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Feb 21, 2013
Length: 318 pages
Edition : 1st
Language : English
ISBN-13 : 9781849518444
Vendor :
Apache
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 83.98
ElasticSearch Server
€37.99
ElasticSearch Cookbook
€45.99
Total 83.98 Stars icon
Banner background image

Table of Contents

8 Chapters
Getting Started with ElasticSearch Cluster Chevron down icon Chevron up icon
Searching Your Data Chevron down icon Chevron up icon
Extending Your Structure and Search Chevron down icon Chevron up icon
Make Your Search Better Chevron down icon Chevron up icon
Combining Indexing, Analysis, and Search Chevron down icon Chevron up icon
Beyond Searching Chevron down icon Chevron up icon
Administrating Your Cluster Chevron down icon Chevron up icon
Dealing with Problems Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3
(22 Ratings)
5 star 50%
4 star 31.8%
3 star 13.6%
2 star 4.5%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Piotr Sep 01, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
You can read through the whole book or skim through. It shows nicely the main features with understandable examples how to use them in real live.
Amazon Verified review Amazon
Sarah Sep 15, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book had much more information then is available on the web. Highly recommend for anyone doing serious elastic search work.
Amazon Verified review Amazon
Daniel Bryant Jul 14, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
In a nutshell: If you are experimenting with or considering using ElasticSearch in production then I would strongly recommend that you buy this book. As mentioned by another reviewer, there are plenty of bad (and out of date) ElasticSearch tutorials floating around the web, but this single book presents a well-paced and logical introduction to ElasticSearch, and also an excellent guide of how to configure it and use it within a production deployment.The book begins by discussing the basic search concepts and how ElasticSearch implements them. As the book progresses so does the level of complexity, but it is easy to pick and choose the information required. The chapters on the advanced topics are worth the price of the book alone, and it is hard to find the same quality of coverage on the web in relation to topics such as percolation, troubleshooting or administrating a cluster.One of the authors, Rafal Kuc, is an authority in full-text searching and indexing, and he has written several successful books on Solr (the other popular open source search engine), which have been very useful to me as I have used search engines more and more over the past year in my daily work as a software developer. Rafal has a gift for providing exactly what a developer requires in order to understand the key concepts of search, and also provides excellent instruction in how to implement this effectively in the real world.In summary, this book comes highly recommended for learning about the up and coming ElasticSearch!
Amazon Verified review Amazon
Oleg Dec 11, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Good for start in ES
Amazon Verified review Amazon
Allen Levin May 14, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Elastic is cool. Elastic works. Elastic documentation leaves a lot to be desired.It seems that Elastic is too new. You need to code and test to really figure it out.This book really helps.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.