0

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?

hoangvu68
  • 845
  • 2
  • 13
  • 28

1 Answers1

2

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
      }
    }
  }
}
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
Prabin Meitei
  • 1,920
  • 1
  • 13
  • 16
  • 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? – hoangvu68 Nov 26 '14 at 07:51
  • http://stackoverflow.com/questions/27144102/range-query-with-elasticsearch-for-string :) – hoangvu68 Nov 26 '14 at 08:21