Search code examples
elasticsearchmodeling

Modelize multiple keywords in one field


I have some products that I want to index in an elasticsearch system. One of the field describing these products is the colour. For instance:

  • Red
  • Blue
  • ...

Some of my products can have two or more colours.

Later on, I want to use facets (aggregation) on this field. And, if I understood correctly, you need to base facets on the type "keyword". So I was hoping to do something like that:

PUT products/product/1
{
  "name": "Trousers",
  "colour": "blue, brown",
}

But, as far as I can tell, what I'm doing here is creating a new keyword "blue, brown" instead of attaching two keywords to my product.

So, how do you input several keywords into one keyword field? Or should I use another approach?


Solution

  • Q: So, how do you input several keywords into one keyword field?

    A: I would store the values in an array that is of type keyword.

    You would index your documents like so:

    PUT products/product/1
    {
      "name": "Trousers",
      "colour": ["blue", "brown"]
    }