# Pg\_trgm similarity ( text, text ) → real (postgresql Function) on ElasticSearch

**URL:** <https://discuss.elastic.co/t/pg-trgm-similarity-text-text-real-postgresql-function-on-elasticsearch/263920>\
**Category:** Elasticsearch\
**Created:** [February 10, 2021, 6:34pm UTC](https://discuss.elastic.co/t/pg-trgm-similarity-text-text-real-postgresql-function-on-elasticsearch/263920 "2021-02-10T18:34:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zied\_marouani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zied_marouani/32/83505_2.png) [@zied\_marouani](https://discuss.elastic.co/u/zied_marouani)\
**Post date:** [February 10, 2021, 6:34pm UTC](https://discuss.elastic.co/t/pg-trgm-similarity-text-text-real-postgresql-function-on-elasticsearch/263920/1 "2021-02-10T18:34:47Z")

</div>

Hello , i'm trying to imrpove the search time for a text from a postgresql database using Elasticsearch , the problem is the search returns a nameof product with best score using "similarity" function in Postgresql example :

````
```
select p.name , similarity(p.name , 'HORTIFRUTIGRANJEIROS/PITAYA') as sml
from products p 
order by sml desc limit 10
 ```

````

when changed to Elasticsearch , i used the java search API :

````
```
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("name", "HORTIFRUTIGRANJEIROS/PITAYA");
matchQueryBuilder.fuzziness(Fuzziness.AUTO);
searchSourceBuilder.query(matchQueryBuilder);
searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
SearchRequest searchRequest = new SearchRequest();
searchRequest.source(searchSourceBuilder);
```

````

but the order of products with score in the response is very different ,  
Is there any way to find something similaire to similarity() function on Elasticsearch ? knowing that it uses the trigram for search , i found some resources about n-gram on Elasticsearch but nothing for the java search API .

---

<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:** [March 10, 2021, 6:35pm UTC](https://discuss.elastic.co/t/pg-trgm-similarity-text-text-real-postgresql-function-on-elasticsearch/263920/2 "2021-03-10T18:35:35Z")

</div>

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