ElasticSearch App Search API value range filtering

38 views Asked by At

I am using ElasticSearch 8.12 App Search (Enterprise Search). The documentation for range in App Search (not query DSL) is here.

I want to give my e-commerce customers the ability to filter on a price range. After they type in a search term, there will be a filter on the right with the ability to select brand, category, etc. I would also like to include the ability to set a minimum and maximum price.

With the example provided for Date range, I assumed my value range would be something like this:

{
    "query": "query",
    "page": {
        "size": 30,
        "current": 1
    },
    "filters": {
        "price": {
            "from": 50.0,
            "to": 100.0
        }
    }
}

However, the response is:

{
    "errors": [
        "Filters contains invalid value for field: price; must be a string, or an array of strings"
    ]
} 

I'd be shocked if value range is not a valid option.

1

There are 1 answers

0
Sajad Soltanian On

Problem

based on the error:

"Filters contains invalid value for field: price; must be a string, or an array of strings"

it seems that the problem is caused by the price field's type. actually the price field has been indexed as an string (text or keyword) type, so that it does not let you to apply a range query.

How to check

you can simply check the _mapping of your index to see whether the price field has been indexed as integer or not.

run such a below GET query to get mapping of your index:

GET <your_index_name>/_mapping

How to solve the problem

while creating your index, you might have used the dynamic mapping feature implicitly, or you might used a wrong mapping for the price field.

Notice that by default, the string numeric detection is disabled. So a numeric string would not be parsed to an equivalent numeric value.

to solve the problem, you need to recreate your index with the correct mapping values.

  1. If you have already filled the index with a bunch of data, you might need to reindex it (using Reindex API).
  2. If not, you can simply delete the current index and start indexing the documents into the newly created index (with correct mapping).