heywhy / ex_elasticlunr

Elasticlunr is a small, full-text search library for use in the Elixir environment. It indexes JSON documents and provides a friendly search interface to retrieve documents.
https://hexdocs.pm/elasticlunr
MIT License
189 stars 10 forks source link

Support Nested Document Attributes #5

Closed heywhy closed 3 years ago

heywhy commented 3 years ago

Overview

These changes allow one to index documents with nested attributes and search using dot notation. Example:

# user structure coming from a data source
%{
  "id" => 1,
  "name" => "peter simons",
  "address" => %{
    "line1" => "some random street",
    "city" => "booming city",
    "country" => "somewhere on earth"
  }
}

# the user object is flattened to this structure before it is indexed
%{
  "id" => 1,
  "name" => "peter simons",
  "address.line1" => "some random street",
  "address.city" => "booming city",
  "address.country" => "somewhere on earth",
}

Considering the transformation that happened before indexing the document, you can now query the index for certain criteria on the nested attributes.

query = %{
  "bool" => %{
    "should" => %{
      "match" => %{"address.city" => "random"}
    }
  }
}

Index.search(index, query)

It's important to note that you don't need to specify the nested fields at the point of creating the index as long as the top-level attribute, address, is specified. The library handles this internal mapping so you don't have to worry.

Related Issues

4

TODO

What to test: