# Elasticsearch custom function

**URL:** <https://discuss.elastic.co/t/elasticsearch-custom-function/2266>\
**Category:** Elasticsearch\
**Created:** [June 10, 2015, 1:15am UTC](https://discuss.elastic.co/t/elasticsearch-custom-function/2266 "2015-06-10T01:15:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fhratul](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fhratul/32/586_2.png) [@fhratul](https://discuss.elastic.co/u/fhratul)\
**Post date:** [June 10, 2015, 1:15am UTC](https://discuss.elastic.co/t/elasticsearch-custom-function/2266/1 "2015-06-10T01:15:39Z")

</div>

Lets say I have a the following tables:

```
products: id, name, latitude and longitude
users: id, name, latitude, longitude
interests: id, name
user_interests: user_id, interest_id

```

These gets inserted/updated into elasticsearch whenever they are created/edited using model observers.

Now I want to make a custom script/scoring function in elastic search which will return me a matching score when a user is searching for a product. The scoring will be based on distance between users location and product, user interests and product name match etc etc.

Being new to elastic search, whats the right approach of implementing this? Any tutorial, online resources or examples are highly appreciated.

PS: I am using PostgreSQL as a database. I can create a function there called get\_match(product\_id, user\_id) which returns a number (0-100) based on matching criteria and do something like:

```
//psuedo sql 
select p.name, get_match(p.id, u.id) as match
from products p, users u
order by match desc

```

I want to achieve a similar functionality in elasticsearch if possible.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [June 10, 2015, 1:46am UTC](https://discuss.elastic.co/t/elasticsearch-custom-function/2266/2 "2015-06-10T01:46:42Z")

</div>

You cannot do joins like that in ES, you have to structure your data to take the relations into account or grab the data from each index and then mash them up externally.

---

<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:08am UTC](https://discuss.elastic.co/t/elasticsearch-custom-function/2266/3 "2017-07-06T00:08:44Z")

</div>

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


