hoangvu68
hoangvu68

Reputation: 865

Range with elasticsearch

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?

Upvotes: 1

Views: 154

Answers (1)

Prabin Meitei
Prabin Meitei

Reputation: 2000

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
      }
    }
  }
}

Upvotes: 3

Related Questions