# ElasticSearch - Geo Spatial search on location array

**URL:** <https://discuss.elastic.co/t/elasticsearch-geo-spatial-search-on-location-array/108210>\
**Category:** Elasticsearch\
**Created:** [November 17, 2017, 11:12pm UTC](https://discuss.elastic.co/t/elasticsearch-geo-spatial-search-on-location-array/108210 "2017-11-17T23:12:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![athir.gillani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/athir.gillani/32/47708_2.png) [@athir.gillani](https://discuss.elastic.co/u/athir.gillani)\
**Post date:** [November 17, 2017, 11:12pm UTC](https://discuss.elastic.co/t/elasticsearch-geo-spatial-search-on-location-array/108210/1 "2017-11-17T23:12:40Z")

</div>

Dear All, I am creating an index with two types Restaurant and Branch with Spring Data. Restaurant has many branches as nested type. When I create this mapping, Spring Data / ElasticSearch automatically converts the location field inside the Branch type as latitude , longitude array, hence geo spatial query is not possible. Here it is:

I want to perform a search on branch.location for geo spatial search, but elastic search is not treating location as geopoint, rather a string array, hence it is not possible, please suggest what I need to do or where I am getting wrong.

```
class Restaurant {
    @Field( type = FieldType.Nested)
    private List<Branch> branches = new ArrayList<Branch>();
}

class Branch {
   private GeoPoint location;
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 18, 2017, 3:51am UTC](https://discuss.elastic.co/t/elasticsearch-geo-spatial-search-on-location-array/108210/2 "2017-11-18T03:51:28Z")

</div>

Please format your code using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

What does the mapping look like?  
What does a typical Json document look like?

---

<div class="post-metadata">

**Author:** ![athir.gillani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/athir.gillani/32/47708_2.png) [@athir.gillani](https://discuss.elastic.co/u/athir.gillani)\
**Post date:** [November 18, 2017, 11:46am UTC](https://discuss.elastic.co/t/elasticsearch-geo-spatial-search-on-location-array/108210/3 "2017-11-18T11:46:16Z")

</div>

Thanks for your response David, I have formated the code. I am attaching the full class of Spring Data Mapping classes:

```
@Document(indexName = "index", type = "restaurant")
public class Restaurant {

	@Id
	private String id;
	private String name;
	private String name_ar;
	private String description;
	private String description_ar;
	
//	private GeoPoint location;
	
	@Field( type = FieldType.Nested)
	private List<BranchLocation> locations = new ArrayList<BranchLocation>();
//	private Map<Integer, GeoPoint> locations = new HashMap<Integer, GeoPoint>();
	
//	@Field( type = FieldType.Nested)
//	private List<Product> products = new ArrayList<Product>();
	
}

```

Branches class/mapping in Spring Data:

```
@Document(indexName = "index", type = "branch")
public class BranchLocation {
	@Id
	private String id;
	private String name;	
	private GeoPoint location	
//	@Field(type = FieldType.Nested, ignoreFields = {"restaurant"})
	private Restaurant restaurant;
}

```

This correctly creates the mapping as below:

`{"index4":{"aliases":{},"mappings":{"branch":{"properties":{"location":{"type":"geo_point"}}},"restaurant":{"properties":{"locations":{"type":"nested","properties":{"location":{"type":"geo_point"}}}}}},"settings":{"index":{"refresh_interval":"1s","number_of_shards":"5","creation_date":"1511004142432","store":{"type":"fs"},"number_of_replicas":"1","uuid":"s8Pa8Ok2Tc2SNUJK-jEC1w","version":{"created":"2040099"}}},"warmers":{}}}`

But then I use following code to insert values:

```
BranchLocation location = new BranchLocation("1001", "", 24.7007637D, 46.6463977D, null);
	locationRepository.save(location);
	BranchLocation location2 = new BranchLocation("1002", "", 24.7034429D,46.6801231D, null);
	locationRepository.save(location2);
	BranchLocation location3 = new BranchLocation("1003", "", 24.7035265D,46.6494806D, null);
	locationRepository.save(location3);
	BranchLocation location4 = new BranchLocation("1004", "", 24.7044622D,46.6869027D, null);
	locationRepository.save(location4);
	
	Restaurant rest = new Restaurant("001", "Bangkok Thai Restaurant", "مطعم بانكوك التايلاندي", "Bangkok Thai Restaurant Description", "مطعم بانكوك التايلاندي");
	rest.getLocations().add(location);
	rest.getLocations().add(location2);
	rest.getLocations().add(location3);
	rest.getLocations().add(location4);
	restaurantService.save(rest);

```

It changes the mapping to following, when I add data, which is correct now, but I am not able to execute the following query, my index contain 1 restaurant and 4 branches which has geo locations inside it, I want to search a restaurant based on the branch location with the following query, please tell me what I have done wrong here:

Geo Spatial Query:

```
{
  "from" : 0,
  "size" : 20,
  "query" : {
    "match_all" : { }
  },
  "post_filter" : {
    "geo_distance" : {
      "distance" : "25km",
      "locations.location" : { 
        "lat" : 24.7007637, 
        "lon" : 46.6463977 
      }      
    }
  }
}

```

After adding data in the index (restaurant and braches):  
`{"index5":{"aliases":{},"mappings":{"branch":{"properties":{"id":{"type":"string"},"location":{"properties":{"lat":{"type":"double"},"lon":{"type":"double"}}},"name":{"type":"string"}}},"restaurant":{"properties":{"description":{"type":"string"},"description_ar":{"type":"string"},"id":{"type":"string"},"locations":{"type":"nested","properties":{"id":{"type":"string"},"location":{"type":"geo_point"},"name":{"type":"string"}}},"name":{"type":"string"},"name_ar":{"type":"string"}}}},"settings":{"index":{"refresh_interval":"1s","number_of_shards":"5","creation_date":"1511005201064","store":{"type":"fs"},"number_of_replicas":"1","uuid":"5x9T44cKT8uJylT9HDZY_A","version":{"created":"2040099"}}},"warmers":{}}}`

Please tell me where I am wrong, why the query is returning empty.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 18, 2017, 12:34pm UTC](https://discuss.elastic.co/t/elasticsearch-geo-spatial-search-on-location-array/108210/4 "2017-11-18T12:34:08Z")

</div>

I can see this in your mapping.

```auto
"type":"nested"

```

Which means that when you query you need to use nested queries as well as internally nested documents are specific Lucene documents.

---

<div class="post-metadata">

**Author:** ![athir.gillani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/athir.gillani/32/47708_2.png) [@athir.gillani](https://discuss.elastic.co/u/athir.gillani)\
**Post date:** [November 18, 2017, 1:13pm UTC](https://discuss.elastic.co/t/elasticsearch-geo-spatial-search-on-location-array/108210/5 "2017-11-18T13:13:37Z")

</div>

can you please transform the following query to suit my case, looking forward to your support.

```
{
  "from" : 0,
  "size" : 20,
  "query" : {
    "match_all" : { }
  },
  "post_filter" : {
    "geo_distance" : {
      "distance" : "25km",
      "locations.location" : { 
        "lat" : 24.7007637, 
        "lon" : 46.6463977 
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 18, 2017, 3:23pm UTC](https://discuss.elastic.co/t/elasticsearch-geo-spatial-search-on-location-array/108210/6 "2017-11-18T15:23:33Z")

</div>

Have a look at [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 16, 2017, 3:23pm UTC](https://discuss.elastic.co/t/elasticsearch-geo-spatial-search-on-location-array/108210/7 "2017-12-16T15:23:34Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
