List of Listings

Chapter 2. Diving into the functionality

Listing 2.1. Indexing documents with populate.sh

Listing 2.2. Search for “elasticsearch” in groups

Listing 2.3. Search reply returning two fields of a single resulting document

Chapter 3. Indexing, updating, and deleting data

Listing 3.1. Getting an automatically generated mapping

Listing 3.2. Trying to change an existing field type from string to long fails

Listing 3.3. Using default and custom time formats

Listing 3.4. Multi-field for a string: once analyzed, once not_analyzed

Listing 3.5. Updating with a script

Listing 3.6. Two concurrent updates managed with versioning: one fails

Chapter 4. Searching your data

Listing 4.1. Limiting the search scope in the URL

Listing 4.2. Paginating results using from and size

Listing 4.3. Changing the order of the results

Listing 4.4. Limiting the fields from source that you want in the response

Listing 4.5. Changing the order of the results

Listing 4.6. Paginating results using from and size

Listing 4.7. Filtering the returned _source

Listing 4.8. Filtering the returned _source showing include and exclude

Listing 4.9. Results sorted by date (ascending), name (descending), and _score

Listing 4.10. Query with all four elements: scope, pagination, fields, and sort order

Listing 4.11. Example search request and response

Listing 4.12. Basic search request using request body

Listing 4.13. match query

Listing 4.14. Query using a filter

Listing 4.15. Example query_string search

Listing 4.16. Specifying a default_field for a query_string search

Listing 4.17. Example term query

Listing 4.18. Example term filter

Listing 4.19. Searching for multiple terms with the terms query

Listing 4.20. Combining queries with a bool query

Listing 4.21. Combining filters with the bool filter

Listing 4.22. Example wildcard query

Listing 4.23. Specify existence and null_value fields as Boolean values Listing 4.24. Caching query filter

Chapter 5. Analyzing your data

Listing 5.1. Setting the number of primary and replica shards

Listing 5.2. Adding a custom analyzer during index creation

Listing 5.3. Example of using the analyze API

Listing 5.4. Example of the reverse token filter

Listing 5.5. Ngram analysis

Listing 5.6. Shingle token filter example

Chapter 6. Searching with relevancy

Listing 6.1. Changing the similarity parameter in a field’s mapping

Listing 6.2. Configuring advanced settings for BM25 similarity

Listing 6.3. Boosting the name field in the group type at index time

Listing 6.4. Query-time boosting using the match query

Listing 6.5. Specify a boost for the entire multi_match query

Listing 6.6. Boosting on the name field only

Listing 6.7. Boosting individual terms in query_string queries

Listing 6.8. Setting the explain flag in the request body

Listing 6.9. Explain API to discover why a document didn’t match a query

Listing 6.10. Using rescore to score a subset of matching documents

Listing 6.11. Function_score query basic structure

Listing 6.12. Using weight function to boost documents containing “hadoop”

Listing 6.13. Specifying two weight functions

Listing 6.14. Using field_value_factor inside a function_score query

Listing 6.15. Scoring using a complex script

Listing 6.16. Using random_score function to sort documents randomly

Listing 6.17. Using Gaussian decay on the geo point location

Listing 6.18. Tying all the function_score functions together

Listing 6.19. Sorting documents with a script

Listing 6.20. Eager loaded field data for the title field

Listing 6.21. Using doc-values in the mapping for the title field

Chapter 7. Exploring your data with aggregations

Listing 7.1. Using the terms aggregation to get top tags

Listing 7.2. Getting top tags for groups in Denver

Listing 7.3. Getting stats for the number of event attendees

Listing 7.4. Getting the average number of event attendees

Listing 7.5. Getting extended statistics on the number of attendees

Listing 7.6. Getting the 80th and the 99th percentiles from the number of attendees

Listing 7.7. Getting the number of unique members through the cardinality aggregation

Listing 7.8. Ordering tag buckets by name

Listing 7.9. Creating buckets only for terms containing “search”

Listing 7.10. Finding attendees attending similar events to Lee

Listing 7.11. Using a range aggregation to divide events by the number of attendees

Listing 7.12. Using a date range aggregation to divide events by scheduled date

Listing 7.13. Histogram showing the number of events for each number of attendees

Listing 7.14. Histogram of events per month

Listing 7.15. Nesting multi-bucket aggregations three times

Listing 7.16. Using the top hits aggregation to get result grouping

Listing 7.17. Global aggregation helps show top tags overall regardless of the query

Listing 7.18. filter aggregation restricts the document set coming from the query

Chapter 8. Relations among documents

Listing 8.1. Inner JSON objects mapped as the object type

Listing 8.2. Searching in location.name from events indexed by the code samples

Listing 8.3. Mapping and indexing nested documents

Listing 8.4. Using include_in_parent when there are multiple nested levels

Listing 8.5. Nested query example

Listing 8.6. Indexing and searching multiple levels of nested documents

Listing 8.7. _parent mapping from the code samples

Listing 8.8. has_parent query to find Elasticsearch events in Denver

Listing 8.9. Combining parent and child aggregations

Listing 8.10. Indexing denormalized members

Listing 8.11. Updating denormalized members

Listing 8.12. Querying for denormalized data returns duplicate results

Chapter 9. Scaling out

Listing 9.1. Getting cluster health for a two-node cluster

Listing 9.2. Getting information about nodes in the cluster with curl

Listing 9.3. Decommissioning a node in the cluster

Listing 9.4. Determining shard location from the cluster state

Listing 9.5. Using the _cat API to find cluster health and nodes

Listing 9.6. Using the _cat API to show shard allocation

Listing 9.7. Adding and removing aliases

Listing 9.8. Retrieving the aliases pointing to a specific index

Listing 9.9. Creating a filtered alias

Listing 9.10. Indexing documents with custom routing values

Listing 9.11. Specifying routing when querying

Listing 9.12. Using the _search_shards API with and without routing

Listing 9.13. Defining routing as required in a type’s mapping Listing 9.14. Combining routing with an alias

Chapter 10. Improving performance

Listing 10.1. Indexing two documents in a single bulk

Listing 10.2. Indexing two documents in the same index and type with automatic IDs

Listing 10.3. Bulk with index, create, update, and delete

Listing 10.4. Multisearch request for events and groups about Elasticsearch

Listing 10.5. _mget endpoint and docs array with index, type, and ID of documents

Listing 10.6. Combine bitset filters in a bool filter inside an and/or/not filter

Listing 10.7. Two warmers for upcoming events and popular group tags

Listing 10.8. Register warmer at index creation time

Listing 10.9. Script score that only specifies term frequency Listing 10.10. Use scan search type

Chapter 11. Administering your cluster

Listing 11.1. Configuring multiple templates

Listing 11.2. Adding a dynamic mapping

Listing 11.3. Cluster health API request Listing 11.4. Defining a new repository

Appendix A. Working with geospatial data

Listing A.1. Sorting events by distance

Listing A.2. Taking distance into account when calculating the score

Listing A.3. Indexing a shape

Listing A.4. geo shape filter example

Appendix B. Plugins

Listing B.1. Installing the elasticsearch-head plugin

Listing B.2. Listing installed plugins

Listing B.3. Available options of the plugin script

Listing B.4. Manually installing a plugin from a local ZIP file

Listing B.5. Example output starting Elasticsearch with the head plugin installed

Listing B.6. Making plugins mandatory for the Elasticsearch service Listing B.7. Removing the analysis-icu plugin

Appendix C. Highlighting

Listing C.1. Highlighting terms in two fields

Listing C.2. Forcing the highlighter to return the needed fields with no_match_size

Listing C.3. Highlighting only fields matching the query

Listing C.4. Field-specific fragment_size setting overrides the global setting

Listing C.5. Custom highlighting tags

Listing C.6. Highlight query contains terms from the main and the rescore query

Listing C.7. Analyze API showing offsets

Listing C.8. Using the Postings Highlighter

Listing C.9. Postings Highlighter matches all the terms and discounts phrases Listing C.10. Plain Highlighter doesn’t work well with multi-fields

Appendix E. Turning search upside down with the percolator

Listing E.1. Using percolator with nested attendee names

Listing E.2. Percolating an existing group document

Listing E.3. Using the multi percolate and bulk APIs for automated tagging

Listing E.4. Filtering queries by their content

Listing E.5. Sorting registered queries by a metadata value

Listing E.6. Using aggregations to get matching query metadata and term statistics

Appendix F. Using suggesters for autocomplete and did-you-mean functionality

Listing F.1. Using the term suggester to correct member typos

Listing F.2. Getting the most popular suggestion for a term

Listing F.3. Phrase suggester working with highlighting

Listing F.4. Using the reverse token filter in one of the two generators

Listing F.5. Using a shingles field to get better ranking for suggestions

Listing F.6. Using collate to see which suggestions would return results

Listing F.7. Simple autocomplete based on existing data

Listing F.8. Using weights, inputs, and outputs

Listing F.9. Payload lets you get documents instead of searching for the suggested text