Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Elasticsearch 5.x Cookbook - Third Edition

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

Product type Book
Published in Feb 2017
Publisher
ISBN-13 9781786465580
Pages 696 pages
Edition 3rd Edition
Languages
Author (1):
Alberto Paro Alberto Paro
Profile icon Alberto Paro

Table of Contents (25) Chapters

Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Dedication
Preface
Getting Started Downloading and Setup Managing Mappings Basic Operations Search Text and Numeric Queries Relationships and Geo Queries Aggregations Scripting Managing Clusters and Nodes Backup and Restore User Interfaces Ingest Java Integration Scala Integration Python Integration Plugin Development Big Data Integration

Chapter 7. Relationships and Geo Queries

In this chapter, we will cover the following recipes:

  • Using the has_child query

  • Using the has_parent query

  • Using the nested query

  • Using the geo_bounding_box query

  • Using the geo_polygon query

  • Using the geo_distance query

  • Using the geo_distance_range query

  • Using the geo_hash query

Introduction


In this chapter, we will explore special queries that work on the relationship between Elasticsearch documents and geolocation ones.

When we have a parent-child relation, we can use special queries to query for a similar relation. Elasticsearch doesn't provide the SQL join, but it lets you search child documents starting from a parent or getting a parent starting from the children.

In this chapter, we also see how to query nested objects via a nested query.

The last part of the chapter is related to geolocalization queries that provide queries based on the distance, box, and polygon for matching documents that meet this criterion.

Using the has_child query


Elasticsearch does not only support simple unrelated documents, it also lets you define a hierarchy based on our parent and children. The has_child query allows querying for parent documents of children matching other queries.

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 operating system.

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

How to do it...

To execute the has_child queries, we will perform the following steps:

  1. We want to search parents test-type of children test-type2 , which has a term in the field value as value1. We can create this kind of query:

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

Using the has_parent query


In the previous recipes, we saw the has_child query; Elasticsearch provides a query to search child documents based on the parent query, has_parent.

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 operating system.

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

How to do it...

To execute the has_parent query, we will perform the following steps:

  1. We want to search children test-type2 of parents test-type that has a term joe in the parsedtext field. We can create this kind of query:

            curl -XPOST 'http://127.0.0.1:9200/test-index/test-
            type2/_search?pretty' -d '{
            "query": {
                    "has_parent" : {
                        "type...

Using nested queries


For queries based on nested objects, there is a special nested query.

This kind of query is required because nested objects are indexed in a special way in Elasticsearch.

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 operating system.

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

How to do it...

To execute the nested query, we will perform the following steps:

  1. We want to search the document for nested objects that are blue and whose size is greater than 10. The nested query will be as follows:

            curl -XPOST 'http://127.0.0.1:9200/test-index/test- 
            type/_search?pretty=true' -d '{
            "query": {
                "nested" : {
                    ...

Using the geo_bounding_box query


One of most common operations in geolocalization is searching for a box (square).

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 operating system.

To correctly execute the following commands, you need an index populated with the chapter_07/populate_geo.sh geoscript, available in the online code.

How to do it...

To execute a geobounding box query, we will perform the following steps:

  1. A search to filter documents related to a bounding box 40.03, 72.0 and 40.717, 70.99 can be done with a similar query:

            curl -XPOST http://127.0.0.1:9200/test-mindex/_search?pretty -d 
           '{
                "query": {
                "geo_bounding_box": {
                          "pin.location": {
                              "bottom_right": {
                    ...

Using the geo_polygon query


The Using the geo_bounding_box query recipe shows how to filter on the square section, which is the most common case; Elasticsearch provides a way to filter user-defined polygonal shapes via the geo_polygon filter. This query is useful if the polygon represents a country/region/district shape.

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 operating system.

To correctly execute the following commands, you need an index populated with the chapter_07/populate_geo.sh geoscript available in the online code.

How to do it...

To execute a geopolygon query, we will perform the following steps:

  1. Searching documents in which pin.location is part of a triangle (its shape is madeup of three geopoints) is done with a similar query:

            curl -XGET "http://127.0.0.1:9200/test-mindex...

Using the geo_distance query


When you are working with geolocations, one common task is to filter results based on the distance from a location. This scenario covers very common site requirements such as:

  • Finding the nearest restaurant within a distance of 20 km

  • Finding my nearest friends within a range of 10 km

The geo_distance query is used to achieve this goal.

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 operating system.

To correctly execute the following commands, you need an index populated with the chapter_07/populate_geo.sh geoscript available in the online code.

How to do it...

To execute a geo_distance query, we will perform the following steps:

  1. Searching documents in which pin.location is 200km away from lat  40 and lon as  70 is done with a similar query:

            curl -XGET 'http://127...

Using the geo_distance_range query


It's a common scenario to find documents distant from a single point (epicenter) in a range.

One example, is when you have indexed a list of points of interest (shops, monuments, airports, train stations, and so on) and you need to know how many of them are in the range between 100 and 200 kilometers.

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 operating system.

To correct execute the following commands, you need an index populated with the chapter_07/populate_geo.sh geoscript available in the online code.

How to do it...

To execute a geo_distance_range query, we will perform the following steps:

  1. Searching documents in which pin.location is 100km to 200km away from  lat  40 and lon , is done with a similar query:

            curl -XGET 'http://127.0.0.1:9200/test-mindex...
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 2017 Publisher: 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.
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}