Index single number as range?


(Sergey Makridenkov) #1

Hello,

I have an object { title: 'milk', weight: '3000' }.
I would like that search 2900 return object above. Is it possible?
I expect that on index creation I can map weight to range [2800..3200], but I can not find any things like that.

Thanks in advance,
Sergey


(Nik Everett) #2

That is a neat idea! There isn't anything for that right now. Ranges are only a thing at query time: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-filter.html

If you wanted a range in the document you could make weight into something like "weight": {"min": 2800, "max": 3200} and then do a bool filter like so `"bool": {"must": [{"range": {"weight.min":{"gte":2900}}}, {"range": {"weight.max":{"lte":2900}}}]}```. I think. I've not tested it but it feels like it'd work. Be careful, though, that won't work with docs like:

{
  "foos": [
    {"weight": {"min": 2800, "max": 3200}},
    {"weight": {"min": 1800, "max": 2200}}
  ]
}

because filters are merged doc by doc, not object by object. You could probably work around that with nested but I'm not super familiar with that.


(system) #3