Query to return documents with field A gt 10 AND field B gt 5

Hi, can someone help me write the correct query if possible? I want to return documents with field A gt 10 AND field B gt 10. The below query only returns documents with filed A gt 10 and field B (but B is not gt 10). Please help!

{
  "query": {
    "bool": {
      "must": [

        {
          "match_phrase_prefix": {
            "name": "*electric scooter*"
          }
        },
        {
          "range": {
            "characteristics.range": {
              "gte": "10.0"
            }
          }
        },

        {
          "range": {
            "characteristics.length": {
              "gte": "10.0"
            }
          }
        }
      ]
    }
  }
}

What is the mappings of the fields you are applying the range clauses to? If they are mapped as text/keyword the range will be based on string comparison (where "5.0" > "10.0") and not numeric comparison.

Thanks for your reply. They are mapped as text. However, the "characteristics.range" results are > 10, and the characteristics.length results are < 10. In your example string comparison, why is "5.0" >"10.0" ? Just curious as to why this is?

When you compare strings you compare one character at a time from the start based on the ASCII value, so since '5' > '1' you get different results compared to numeric comparison.

If you want numeric comparison you need to map the field as a numeric type.

1 Like

From Elastic Search to Elasticsearch

Ah, ok. Any suggestions on how to achieve what I want? Unfortunately I can't change the mappings. I've had success with regex using e.g.<5-100> but that does not work with values containing decimal points.

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