Recently, I’ve been playing around with a search in Elasticsearch and got stuck with development when attempting to work with an array of objects. Indexing went fine, the query results, however, did not look as expected.

Elasticsearch is a really powerful search and analytics engine which comes in very handy when you need to perform a text-based search on data collections. But you will only get along with this particular tool as long as you understand some of its specific behaviors—at least that’s what I learned when I found out the power of Elasticsearch’s nested objects.

The examples below are written in Ruby with assistance from the elasticsearch gem.

First There’s an Idea

Let’s suppose you’re running a recruitment agency helping software houses hire developers perfectly matching the requirements for their open positions. In order to simplify the example, the personal details of developers will be limited to their names and skills, including the languages they know along with the level of their proficiency therein. So far, only two developers have registered with your agency. The documents representing developer data can be found below:

[
  {
    name: "John Doe",
    skills: [
      {
        language: "ruby",
        level: "expert",
      },
      {
        language: "javascript",
        level: "beginner",
      },
    ]
  },
  {
    name: "Mark Smith",
    skills: [
      {
        language: "ruby",
        level: "beginner",
      },
    ]
  },
]

#elasticsearch #ruby/rails #development

How To Index Array of Objects in Elasticsearch (Code Provided)
4.45 GEEK