Search code examples
elasticsearchgeolocationhibernate-criteriahibernate-search

Hibernate search : use Elastic Search decay_function in spatial() query?


I have different fields to make a query from, like a title, a text, and a location. I want to put more weight on the location field. I found the decay_function from Elastic Search, which exactly answers my needs according to this example : https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#_detailed_example . However the system i'm on uses Hibernate Search queries. I found the Spatial ( https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#spatial ) filter, but is there a way to replace the "within()" by the decay_function ? Or by a personnalized function or something like that ?

I know I could use the within() but it means I would have to define specific ranges. It could work but it's not the optimal solution in my case.


Solution

  • Not using the DSL itself (for now, it will be possible in 6 to integrate native predicates in the DSL).

    But you can create a native Elasticsearch query for this specific case as explained in our documentation: https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#_queries :

    FullTextEntityManager fullTextEm = Search.getFullTextEntityManager(entityManager);
    QueryDescriptor query = ElasticsearchQueries.fromJson(
        "{ 'query': { 'match' : { 'lastName' : 'Brand' } } }");
    List<?> result = fullTextEm.createFullTextQuery(query, GolfPlayer.class).getResultList();
    

    This way you can use whatever Elasticsearch construct you see fit. And you can still use the DSL for the other supported queries.

    Unrelated to your question: I saw you are a student, are you using Hibernate Search for a school project? It would be nice having Search taught in a French school :).