loading...
Cover image for Get Matching Score With Spring data elastic search in Spring Boot for a Query

Get Matching Score With Spring data elastic search in Spring Boot for a Query

balvinder294 profile image Balvinder Singh ・1 min read

Hi everyone, I am gonna share about the scoring and percentage from es query to order results and show to the user.

Previously, I was using an elastic search rest-client template to have a search query, but that way I have to do extract score, and list of data with mapping to the required format. The configuration is a little more and complex than using spring data elasticsearch directly. But after looking for a solution to fix some other issue I have, I got the solution for getting score directly from spring data elasticsearch with a little modification.


Let me share.

I have done with pagination, but you can do normally too by using stream with a list instead. let me know if you want in the comments below, I will add

  1. We create a normal Query like match query
  2. call the bookpage method with a query and pageable parameters
  3. Create a search query with required options
  4. Call the elastic search template query method to modify and extract the response. it is the main point where you get the hit score and total hits.
  5. call additional bookDOFromMap method to get a score and append to DTO.
  6. Now you have the score calculated from hit score of each record vs all records.

You can modify the query or scoring logic by using a function score query with a scoring function for your needs.
I hope it will help, you let me know in the comments
Also, do check out other blog for more tech posts like this.
https://tekraze.com

Posted on by:

balvinder294 profile

Balvinder Singh

@balvinder294

Full Stack Developer and DevOps working remotely in Dehaze.io. Founder and Blogger at Tekraze.com, here to share my journey of code and experiences to help out the coders to give back to dev communiry

Discussion

pic
Editor guide