Home > database >  ElasticSearch: update by query from a different index
ElasticSearch: update by query from a different index

Time:07-02

I have the following problem with ElasticSearch. Let's say I have one index called "products". In general, its documents have the following fields:

  1. productId
  2. productPackId
  3. productName
  4. price

And then (for reason that I cannot explain here, but let's say weren't my decision) I have another index called "productPacks" with:

  1. productPackId
  2. name
  3. imageUrl

Now, I need to get the imageUrl field of the index "productPacks" in the "products" index according to which *productPackId" each document on the "products" index has. To clarify: let's say that in "productPacks" the document with

"productPackId" = 1

has as

imageUrl: "https://mywebsite.com/image1.jpg",

what I need is that all documents on the "products" index that have "productPackId" === 1 get then

imageUrl: "https://mywebsite.com/image1.jpg"

I can't find a way of doing it.

Thanks in advance!

(This, of course, would be super easy on a SQL database.)

CodePudding user response:

What you basically want to do is join the two indices, on the "productPackId".

This is not possible in elasticsearch over two different indices.

There is a simple solution:

Iterate over each and every document in the index with the image url's(Index 2) and update by query into index 1, use the productPackId to make the query. That way you will be able to add the image_urls into index1.

Elasticsearch does not have any concept of Join's across indexes.

HTH.

CodePudding user response:

The result you expect, you can only do it with a SQL request

https://www.elastic.co/guide/en/elasticsearch/reference/master/xpack-sql.html

  • Related