Sign in

Master Elasticsearch with Expert Guidance

🔐 This chapter is a free Elasticsearch Handbook member exclusive illustrating the usage of nested fields. It was originally published here.

To Nest or Not to Nest?

If you’re coming from a relational database background, you may find it challenging to flatten your nested documents to conform with the NoSQL principles that Elasticsearch enforces.

In order to decide which flattening strategy to choose, I’d recommend to first read this blog post that deals with managing document relations.

From my point of view, there are essentially two options to consolidate related documents:

I prefer nested fields above all else for these three reasons:

  1. One document = one single source of truth.
  2. The…

🔐 This chapter is a free Elasticsearch Handbook member exclusive and was originally published here.

Script Fields in Practice

As mentioned in the chapter Aggregation Data Tables, each individual hit returned from a _search request can contain custom attributes generated on-the-fly through a script.

These custom attributes (script fields) are typically either:

  • modifications of the original values (e.g. price conversions, currency conversions, differently sorted arrays, …)
  • or newly computed values (e.g. sums, boosts, exponentials, distance measurements, etc.)

A _search request can specify more than one script field at once:

POST myindex/_search
{
"script_fields": {
"using_doc_values": {
"script": "doc['price'].value * 42"
},
"using_source": {
"script": "params['_source']['price'] * 42"
}
}
}

Unlike in script queries where you can only access…


🔐 This chapter is a free Elasticsearch Handbook member exclusive and was originally published here.

Use Case

I want to retrieve locations

  • that are within the NYC area, defined by a GeoJSON rectangular viewport
  • and boost those that are in Brooklyn, described by a GeoJSON polygon:

🔐 This chapter is a free Elasticsearch Handbook member exclusive and was originally published here.

Simple Tables

As touched upon in the introduction to 3. Table & Charts, pagination requests are highly useful for building simple tables, lists, grids, and galleries.

In order to remain demand-oriented, you should aim to limit the response hits’ content to only those attributes that are actually needed to populate said components. As such, you can specify, via the includes parameter, which fields should be returned. Analogously, you can control which ones can be skipped via the excludes parameter. Wildcards are supported too:

POST index_name/_search
{
"size": 24,
"from": 24,
"query": { ... …

The Elasticsearch Handbook

Master Elasticsearch with Expert Guidance

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store