0

I'm trying to insert documents into Elasticsearch, they come as a format like:

  {
  total: 1,
  subtotal: 1.2,
  totalDiscount: 0}

The issue I'm having is with the zeroes, in JavaScript you can't force '0' to be represented as '0.0' or '0.00'.

I can't use text in the mappings in ES, as I want to obviously do mathematical operations on these fields. So I'm using a 'float' mapping for all of the above.

So, for each of those fields I have something like:

"subtotal": {
   "type": "float"
   },

I've tried all sort of different combinations, storing them as 'text' doesn't let me query them as I want, if I don't define the mapping I get a 'long' type for the fields, which truncates them, If I use float I get an exception mapper [totalDiscount] cannot be changed from type [float] to [long], if I remove them complitely, so skipping the save I get an error too

Rejecting mapping update to [...] as the final mapping would have more than 1 type

Any help much appreciated, thanks.

Or Assayag
  • 5,662
  • 13
  • 57
  • 93
Antonio Terreno
  • 2,835
  • 1
  • 14
  • 9

3 Answers3

1

Update:

the scaled_float didn't work well for me, so I ended up doing this "the stripe way"

i.e. representing all monetary amounts in cents, safe, less space on disk, just works without having to define a mapping.

also used this https://currency.js.org/ to make sure the multiplication and output wouldn't suffer from the 'well known' issues with floats in JS.

Antonio Terreno
  • 2,835
  • 1
  • 14
  • 9
0

as this might be useful to someone reading, I think the answer might be using this sort of mapping:

"price": {
"type": "scaled_float",
"scaling_factor": 100
}

not only is more disk-efficient, but it won't have the above issues.

I'll keep this thread updated, to see if that works.

Antonio Terreno
  • 2,835
  • 1
  • 14
  • 9
0

I am not familiar with the node client library, but in elasticsearch, the errors signify that -

mapper [totalDiscount] cannot be changed from type [float] to [long]

From the above error, it seems as if when you created the index, totalDiscount field, was defined with the float field data type and now you are changing it to the long data type. This is not possible, that is why the above error is thrown.

Rejecting mapping update to [...] as the final mapping would have more than 1 type

This error occurs because types are deprecated in APIs in 7.0, with breaking changes to the index creation, put mapping, get mapping, put template, get template and get field mappings APIs. Refer to this to know more about the removal of mapping types.

ESCoder
  • 15,431
  • 2
  • 19
  • 42