ruby-on-railselasticsearchsearchkick

How to add multiple range filters for the same field with searchkick and elastic search?


On my rails app, I have a product model which is indexed with searchkick and elastic-search with an esp field. While it is fairly easy to search prosucts within a range of esp:

Product.search('*', where{ esp: 100..200 })

I am struggling to work out how to add multiple ranges to the query.

I know elastic search can support multiple ranges on the same field as discussed here: Elasticsearch multiple range filters on same field and https://discuss.elastic.co/t/multiple-range-filters-for-same-field/73898

I have tried

Product.search('*', where{ esp: [100..200, 500..600] })

which gives me this error:

Searchkick::InvalidQueryError ([400] {"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: multiple points","index_uuid":"bMBebzhDSB2QKnaynoOHWQ","index":"public_products_development_20240202154554635"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"public_products_development_20240202154554635","node":"8-khPyjBS5SmR3Pea20QRg","reason":{"type":"query_shard_exception","reason":"failed to create query: multiple points","index_uuid":"bMBebzhDSB2QKnaynoOHWQ","index":"public_products_development_20240202154554635","caused_by":{"type":"number_format_exception","reason":"multiple points"}}}]},"status":400})

Also tried with

Product.search('*', where: { esp: [{gte: 0, lte: 100}, {gte: 500, lte: 1000}] })

which results in

Searchkick::InvalidQueryError ([400] {"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: For input string: "{"","index_uuid":"bMBebzhDSB2QKnaynoOHWQ","index":"public_products_development_20240202154554635"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"public_products_development_20240202154554635","node":"8-khPyjBS5SmR3Pea20QRg","reason":{"type":"query_shard_exception","reason":"failed to create query: For input string: "{"","index_uuid":"bMBebzhDSB2QKnaynoOHWQ","index":"public_products_development_20240202154554635","caused_by":{"type":"number_format_exception","reason":"For input string: "{""}}}]},"status":400})


Solution

  • Okay dug into the docs and found the answer:

    Product.search("*", where: { _or: [{esp: 100..200}, {esp: 500..600}] } )