Marco
Marco

Reputation: 3651

Combine (independent) indexes in elastic search

I have two indexes in elastic search an I want to combine them in a search.

One index has some articles, e.g.

(
    [id] => 25
    [name] => my test artcile
    [city] => [
           id => 123
    ]
)

The second index has the city names, e.g.

(
    [id] => 123
    [name] => my city
)

So if I do a search query in the articles I want a result like this, where the cities name is used in the result, too, e.g.

(
    [id] => 25
    [name] => my test artcile
    [city] => [
           id => 123
           name => my city
    ]
)

Is there a way to do this? Otherwise I had to extend the import for the articles to add the cities name directly, but the prefered way is a join between the indexes.

Upvotes: 0

Views: 1648

Answers (1)

Daniel Schneiter
Daniel Schneiter

Reputation: 1996

Combining documents from independent indices at search time is not possible/supported by Elasticsearch. It's typically done via application side joins (your application sends 2 requests to Elasticsearch:

  • the first one to retrieve the matching documents
  • the second one to retrieve the countries for the documents you got back from the first request

Elasticsearch's join type only allows to join documents the same index and not across indices. The terms query can be used to look up values in another index to filter out documents in another index based on those values.

As mentioned in the comments section, Elasticsearch is not a relational database, it simply comes with some basic capabilities, to support the handling of relations. With an RDBMS you perform "expensive" joins at query time, RDBMS are optimised for fast writes (smaller entities, single version of the truth, write once, support for transactions etc). Search Engines are optimised for fast reads and search requests, therefore we ask you to execute the "expensive" joins up-front, and get your denormalised documents indexed. This is generally the preferred approach.

Upvotes: 1

Related Questions