0
votes

In elasticsearch, I want to query multiple indices with a geo_distance filter.

The issue is that one index has geo_point mapping and the other has no geo_point mapping.

If I query using latitude and longitude, I am getting results only for the index which has geo_point mapping. For the other index I am getting failed to find geo_point field exception.

How can I get the results from both indices?

Following is my query.

{  
"query":{  
    "filtered":{  
        "query":{  
            "bool":{  
                "should":{  
                    "match_all":{  

                    }
                }
            }
        },
        "filter":{  
            "bool":{  
                "should":[  
                    {  
                        "bool":{  
                            "must":[  
                                {  
                                    "geo_distance":{  
                                        "distance":"50km",
                                        "location":{  
                                            "lat":22.5705741,
                                            "lon":88.4355427
                                        }
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
},
"sort":[  
    {  
        "_geo_distance":{  
            "location":{  
                "lat":22.5705741,
                "lon":88.4355427
            },
            "order":"asc",
            "unit":"km"
        }
    }
],
"size":30,
"from":0
}

I am using elasticsearch verion 0.90.12

1
Well obviously if your second index doesn't have a geo_point field, it's going to be difficult to query it with a geo_distance filter, isn't it? One thing you can try is to use a type filter and combine the geo_distance filter with the type one on the type that has the geo_point. But if the geo_distance is the only filter you have, I don't see the point of retrieving results from another index that doesn't have any geo-ish information.Val
@Val There is a match query as well along with the geo_distance filter. I will try the type filter and let you know how it goes.tungri
@Val Can you please give an example of using type filter in the scenario explained? i.e, geo_distance search on indexes with one index having geo_point field and missing in the other one. Thankstungri

1 Answers

0
votes

Refer to your last comment i think you can try this :

{
"query": {
  "filtered": {
     "filter": {
        "bool": {
           "should": [
              {
                 "bool": {
                    "must": [
                       {
                          "term": {
                             "_index": "index_name_with_geo_point"
                          },
                          "geo_distance": {
                             "from": 100,
                             "to": 200,
                             "distance_unit": "km",
                             "location": {
                                "lat": 40.73,
                                "lon": -74.1
                             }
                          }
                       }
                    ]
                 }
              },
              {
                 "term": {
                    "_index": "Index_name_without_geo_point"
                 }
              }
           ]
        }
     }
  }
 }
}

now or

  • the ducument must be from the index with the geo point and in the geo range
  • the document isnot from the index with the geo point.

ofcorse you can expand the second element of the should query to bool : {must : {terms : {}}} as the first one is , but do it only if it's necessary