Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch - Enum datatype

Let's say I want to store documents like the one below:

{
  "item_id": 1,
  "item_price": 500,
  "currency": "USD"
}

I want the currency field to be like ENUM, so I can predefine the set of values, like: "USD", "GBP", "EUR" and so on...

I also would like that each value will be related to an integer, like hash map, so the set of values will look like this:

{ "USD":1, "GBP":2, "EUR":3 }

how shell I map this field?

like image 411
TVC Avatar asked Apr 24 '17 22:04

TVC


1 Answers

You need to declare your enum in your indexing code and your document should be denormalized like this:

{
  "item_id": 1,
  "item_price": 500,
  "currency": "USD",
  "currency_id": 1
}

As for the data types, I suggest to declare the currency field as keyword and the currency_id field as byte or short depending on the number of currencies you need to track.

like image 164
Val Avatar answered Oct 29 '22 01:10

Val