1

I am trying search with elasticsearch with range query. (PHP)

This is my source code

$searchParams['body']['query']['range']['order_no']['gte'] = "2000";
$searchParams['body']['query']['range']['order_no']['lte'] = "2001";

=

{
    "query": {
        "range": {
             "order_no": {
                 "gte": 2000,
                 "lte": 2001
             }
         }
    }
}

But in result it have order_no:

2000
2001
2000001
200000
....

I want show only

2000
2001

This field have mapping:

"order_no" : {
    "type" : "string",
     "index" : "not_analyzed"
}

How can fix it?

1 Answer 1

3

The best and most effective way is to change your field mapping to

{
  "order_no": {
    "type": "integer",
    "index": "not_analyzed"
  }
}

String range queries are very slow compared to numeric range ones.

If you are constrained from changing the field mapping then another option is to pad the input values with zeros while indexing as well as searching as

{
  "query": {
    "range": {
      "order_no": {
        "gte": 00002000,
        "lte": 00002001
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi. I have another question if you can help me. I can't use range query with string have "-" or "_". Do you know the reason?
What's the explanation for this behavior? is there any source in the docs?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.