0

I am using a client for elasticsearch client for elasticsearch, i am not getting the right result for a multiple range query.

The body that i use for querying:

{  
   "index":"locations",
   "type":"portugal",
   "body":{  
      "aggs":{  
         "unique":{  
            "aggs":{  
               "documents":{  
                  "top_hits":{  
                     "size":50
                  }
               }
            },
            "terms":{  
               "field":"cp3"
            }
         }
      },
      "query":{  
         "filtered":{  
            "filter":{  
               "bool":{  
                  "must":{  
                     "range":{  
                        "latitude":{  
                           "gte":41.1373667,
                           "lte":41.1373767
                        },
                        "longitude":{  
                           "gte":-8.631723,
                           "lte":-8.631713
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

the result:

{  
   "took":2,
   "timed_out":false,
   "_shards":{  
      "total":5,
      "successful":5,
      "failed":0
   },
   "hits":{  
      "total":1,
      "max_score":1,
      "hits":[  
         {  
            "_index":"locations",
            "_type":"portugal",
            "_id":"AVTJ4I0g_vwSgXBDKO-W",
            "_score":1,
            "_source":{  
               "id":222956,
               "latitude":41.0383217,
               "longitude":-8.6317147
            }
         }
      ]
   },
   "aggregations":{  
      "unique":{  
         "doc_count_error_upper_bound":0,
         "sum_other_doc_count":0,
         "buckets":[  
            {  
               "key":199,
               "doc_count":1,
               "documents":{  
                  "hits":{  
                     "total":1,
                     "max_score":1,
                     "hits":[  
                        {  
                           "_index":"locations",
                           "_type":"portugal",
                           "_id":"AVTJ4I0g_vwSgXBDKO-W",
                           "_score":1,
                           "_source":{  
                              "id":222956,
                              "latitude":41.0383217,
                              "longitude":-8.6317147
                           }
                        }
                     ]
                  }
               }
            }
         ]
      }
   }
}

as you can see the result's latitude is not inside the latitude range i gave it.

Using the Head plugin from elasticsearch to test:

my query:

{  
   "query":{  
      "bool":{  
         "must":[  
            {  
               "range":{  
                  "latitude":{  
                     "gte":"41.1363671",
                     "lte":"41.1363771"
                  }
               }
            },
            {  
               "range":{  
                  "longitude":{  
                     "gt":"-8.6318828",
                     "lte":"-8.6318728"
                  }
               }
            }
         ],
         "must_not":[  

         ],
         "should":[  

         ]
      }
   },
   "from":0,
   "size":10,
   "sort":[  

   ],
   "aggs":{  

   }
}

The result: (empty), there is no records in this range.

Edit: added the mapping for my index:

{     
      "locations":{  
      "aliases":{  

      },
      "mappings":{  
         "portugal":{  
            "properties":{  
               "cp3":{  
                  "type":"long"
               },
               "cp4":{  
                  "type":"long"
               },
               "cpalf":{  
                  "type":"string"
               },
               "id":{  
                  "type":"long"
               },
               "latitude":{  
                  "type":"double"
               },
               "localidade":{  
                  "type":"string"
               },
               "longitude":{  
                  "type":"double"
               }
            }
         }
      },
      "settings":{  
         "index":{  
            "creation_date":"1463675755006",
            "number_of_shards":"5",
            "number_of_replicas":"1",
            "uuid":"C9OO0ig_QyeigqSufK8_dA",
            "version":{  
               "created":"2030199"
            }
         }
      },
      "warmers":{  

      }    
    } 
}
5
  • 1
    You can show the mapping and settings? Commented May 24, 2016 at 9:36
  • Check this : github.com/elastic/elasticsearch/issues/14161 . This should be help Commented May 24, 2016 at 9:40
  • { "locations": { "aliases": {}, "mappings": { "portugal": { "properties": { "cp3": { "type": "long" }, "cp4": { "type": "long" }, "cpalf": { "type": "string" }, "id": { "type": "long" }, "latitude": { "type": "double" }, "localidade": { "type": "string" }, "longitude": { "type": "double" } } } }, "settings": { "index": { "creation_date": "1463675755006", "number_of_shards": "5", "number_of_replicas": "1", "uuid": "C9OO0ig_QyeigqSufK8_dA", "version": { "created": "2030199" } } }, "warmers": {} } } Commented May 24, 2016 at 9:43
  • check the below answer. Did it help you, @GustavoSilva ? Commented May 26, 2016 at 8:46
  • On PHP-elasticsearch, i was not sending a JSON as body, i was sending an array, and the elasticsearch client was converting it. When i tried to send one like the one you posted, there was a response error by elasticsearch. My query was right after i removed the "term" from it. I still don´t know why it was not working like it should. Thanks for the help. Commented May 30, 2016 at 8:42

1 Answer 1

1

Test with the following query :

{  
   "query":{  
      "bool":{  
         "must":[  
            {  
               "range":{  
                  "latitude":{  
                     "gte": 41.1363671,
                     "lte": 41.1363771
                  }
               }
            },
            {  
               "range":{  
                  "longitude":{  
                     "gt": -8.6318828,
                     "lte": -8.6318728
                  }
               }
            }
         ],
         "must_not":[  

         ],
         "should":[  

         ]
      }
   },
   "from":0,
   "size":10,
   "sort":[  

   ],
   "aggs":{  

   }
}

As longitude and latitude fields are double type the should be compare with double, but you have compared with string.

Sign up to request clarification or add additional context in comments.

Comments

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.