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

**URL:** <https://discuss.elastic.co/t/query-to-return-documents-with-field-a-gt-10-and-field-b-gt-5/362226>\
**Category:** Elasticsearch\
**Created:** [June 28, 2024, 6:56pm UTC](https://discuss.elastic.co/t/query-to-return-documents-with-field-a-gt-10-and-field-b-gt-5/362226 "2024-06-28T18:56:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![trs80](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@trs80](https://discuss.elastic.co/u/trs80)\
**Post date:** [June 28, 2024, 6:56pm UTC](https://discuss.elastic.co/t/query-to-return-documents-with-field-a-gt-10-and-field-b-gt-5/362226/1 "2024-06-28T18:56:15Z")

</div>

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!

```auto
{
  "query": {
    "bool": {
      "must": [

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

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

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [June 28, 2024, 7:05pm UTC](https://discuss.elastic.co/t/query-to-return-documents-with-field-a-gt-10-and-field-b-gt-5/362226/2 "2024-06-28T19:05:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![trs80](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@trs80](https://discuss.elastic.co/u/trs80)\
**Post date:** [June 28, 2024, 7:13pm UTC](https://discuss.elastic.co/t/query-to-return-documents-with-field-a-gt-10-and-field-b-gt-5/362226/3 "2024-06-28T19:13:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [June 28, 2024, 7:36pm UTC](https://discuss.elastic.co/t/query-to-return-documents-with-field-a-gt-10-and-field-b-gt-5/362226/4 "2024-06-28T19:36:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 28, 2024, 7:38pm UTC](https://discuss.elastic.co/t/query-to-return-documents-with-field-a-gt-10-and-field-b-gt-5/362226/5 "2024-06-28T19:38:27Z")

</div>

Category updated on June 28, 2024, 7:38pm UTC

From #Elastic Search to #Elasticsearch

---

<div class="post-metadata">

**Author:** ![trs80](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@trs80](https://discuss.elastic.co/u/trs80)\
**Post date:** [June 28, 2024, 7:40pm UTC](https://discuss.elastic.co/t/query-to-return-documents-with-field-a-gt-10-and-field-b-gt-5/362226/6 "2024-06-28T19:40:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 26, 2024, 7:40pm UTC](https://discuss.elastic.co/t/query-to-return-documents-with-field-a-gt-10-and-field-b-gt-5/362226/7 "2024-07-26T19:40:38Z")

</div>

Closed on July 26, 2024, 7:40pm UTC

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