# Relationshilp search

**URL:** <https://discuss.elastic.co/t/relationshilp-search/23396>\
**Category:** Elasticsearch\
**Created:** [April 25, 2015, 2:09am UTC](https://discuss.elastic.co/t/relationshilp-search/23396 "2015-04-25T02:09:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ming\_ming](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@ming\_ming](https://discuss.elastic.co/u/ming_ming)\
**Post date:** [April 25, 2015, 2:09am UTC](https://discuss.elastic.co/t/relationshilp-search/23396/1 "2015-04-25T02:09:31Z")

</div>

hi! i am a newbie to this forum and i've got a problem recently with  
relationship model search(Elasticsearch + Ruby on Rails). I have a document  
model that has\_many geolocations, through georeferences. look like this

_Document_  
has\_many :geolocations, through: :georeferences  
has\_many :georeferences, as: :georeferenceable, dependent: :destroy

_Georeference_  
belongs\_to :georeferenceable, polymorphic: true  
belongs\_to :geolocation

_Geolocation_  
has\_many :georeferences  
has\_many :documents, through: :georeferences

I also have a field that contain polygon data(coordinates) inside  
Geolocation. now i want to search the coordinate to return document. is it  
possible to do that ?

this is my mapping :

mapping \_source: { excludes: ['attachment'] } do  
indexes :title, analyzer: 'english', index\_options: 'offsets'  
indexes :attachment, type: 'attachment'  
indexes :geolocations, type: 'geo\_shape'  
end  
end

this is my as\_index\_json

def as\_indexed\_json(options={})  
as\_json(  
only: 'title',  
include: {geolocations: {only: [:type, :coordinates]}},  
methods: [:attachment]  
)  
end

this is my search function

def search\_lat\_and\_lon(radius, lat, lon)  
**elasticsearch**.search(  
{  
from: 0,  
size: 100,  
query:{  
filtered: {  
query: {  
match\_all: {}  
},  
filter:{  
geo\_shape: {  
\_cache: true,  
geolocations: {  
shape: {  
type: "circle",  
coordinates: [lon,lat],  
radius: "#{radius}m"  
}  
}  
}  
}  
}  
}  
}  
)  
end

thanks in advance ! Help everyone can help me

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/fdeed2d4-6b8a-40d9-9589-1682210ab52e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fdeed2d4-6b8a-40d9-9589-1682210ab52e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 12:17am UTC](https://discuss.elastic.co/t/relationshilp-search/23396/2 "2017-07-06T00:17:46Z")

</div>

Closed on July 6, 2017, 12:17am UTC


