Elasticsearch Nested Query

Tram Ho

What is a nested query in elasticsearch?

Perform a query to search for nested objects. Nested queries look for nested objects as if they were indexed as separate documents. If an object matches the search criteria, the nested query will return the original object.

Create the index

To use nested queries, your index must include nested mappings, for example:

In rails we use the gem “chewy” to declare and manage the chewy index on the home page of this chewy gem will have instructions for config and installation, so we will skip this step. Example: There are 2 models, User and Post.

  • Users have fields (name, email),
  • Post has fields (title, content) We will perform a post title search to retrieve the user who created that post

User has has_many relationship with Post. Then using chewy we will declare the nested index like this

  • field -> declare the field name to be indexed
  • type -> declare the data type of that index We have now completed the setup of the index to use for nested queries

Perform query search

  • path (require, string): path to the nested object you want to search
  • query (require, object query): The query you want to run on nested objects in path. If an object satisfies the condition, the nested query will return the original object User

Multi-level nested objects

How to declare index similar to above

Or

For multiple levels of nested, query search is the same

If the object satisfies the search criteria, it also returns the original User object.

Refer

1: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html

2: https://github.com/toptal/chewy/

Share the news now

Source : Viblo