Range Query in Elasticsearch Doesn't Filter on Certain Number Fields


(Tim Schoffelman) #1

I'm running the following query against my ES instance 5.4. My year range and postal code works great, but as soon as I add in the price or millage range, it returns zero results. Here's the cURL statement (I converted it as I'm running the ES PHP library, so my syntax may be off here).

Additionally, I've also moved the price & milage to a filter bool with no tangible results. This query was working once upon a time, but after re-importing my data, it no longer did. So, maybe it's possible I changed the type of those two fields?

In my ES instance I have a Suzuki that match this criterial.

curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query" : {
        "bool" : {
            "must" : {
                {"query_string" : {
                    "query" : "suzuki",
                    "fields" : {"keywords", "description", "title"}
                }},
                {"range" : {
                    "year.keyword" : {
                        "gte" : 2010,
                        "lte" : 1980
                    }
                }},
                {"range" : {
                    "price.keyword" : {
                        "gte" : 0,
                        "lte" : 10000
                    }
                }},
                {"range" : {
                    "mileage.keyword" : {
                        "gte" : 0,
                        "lte" : 100000
                    }
                }},
                {"terms" : {
                    "postal.keyword" : 57013
                }}
            }
        }
    },
    "highlight" : {
        "fields" : {
            "*" : { "force_source" : "true", "fragment_size" : 250 }
        }
    },
    "sort" : {
        "price.keyword" : { "order" : "asc", "mode" : "min" }
    }
}'

Here is the Suzuki ES record (some of this has been scrubbed):

_source: {
index: "",
type: "",
keywords: "2009 Suzuki Sx4 Crossover AWD Crossover 4dr 4A",
description: "2009 Suzuki SX4 Crossover AWD Crossover 4dr 4A",
title: "2009 Suzuki Sx4 Crossover AWD Crossover 4dr 4A,
postal: "57013",
price: "5600",
year: "2009",
make: "Suzuki",
model: "sx4",
mileage: "93012",
engine: "2.0L I4",
transmission: "Automatic",
trim: "AWD Crossover 4dr ",
date_added: "2017-10-01 01:48:36",
date_updated: "2017-10-01 01:48:36"
}

(Christian Dahlqvist) #2

It looks like the numeric fields might be mapped as text instead of numbers. Can you share the mappings for these fields? If this is the case, range will be based on string sorting, where “5600” > “10000”.


(Tim Schoffelman) #3

Sorry about the formatting.

 {
    us-transportation: {
    mappings: {
    transportation: {
    properties: {
    address: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    body: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    date_added: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    date_updated: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    description: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    engine: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    image: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    index: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    keywords: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    make: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    mileage: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    model: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    postal: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    price: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    title: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    transmission: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    trim: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    },
    vin: {
    type: "boolean"
    },
    year: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    }
    }
    }
    }
    }
    }

(Christian Dahlqvist) #4

That seem to be the case. You will need to reindex your data with corrected mappings to resolve this problem.


(Tim Schoffelman) #5

Gotcha - so the items I need to sort off of need to be numeric instead of text. Thank you!


(system) #6

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.