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 Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
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
Estimated delivery fee Deliver to Switzerland

Standard delivery 10 - 13 business days

€11.95

Premium delivery 3 - 6 business days

€16.95
(Includes tracking information)

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 Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Switzerland

Standard delivery 10 - 13 business days

€11.95

Premium delivery 3 - 6 business days

€16.95
(Includes tracking information)

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 the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact [email protected] with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at [email protected] using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on [email protected] with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on [email protected] within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on [email protected] who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on [email protected] within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela