mercredi 26 juillet 2023

I'm working on implementing a search feature in my Ruby on Rails with Diacritic Search Matching Exact Word

 if params[:name]
      name_normalized = params.unicode_normalize(:nfkd).gsub("_", "\\_")
      brands = brands.where("name LIKE ?", "%#{name_normalized}%")
 end

When I search for the diacritic character "š" in the database, the search query does not return any results, even though there is a record with the name "Tešt" containing that character.

If I modify the query to make it diacritic, it returns results like "testing," "Test," "Tešt," and "demo_Test" when searching for "š." However, my desired result is only the exact match "Tešt" because I was specifically searching for this particular word.

Despite all this, the search still doesn't fetch the desired results. I suspect there might be an issue with the query or how I'm handling the search term.

I have used .gsub("_", "\\_") to handle cases involving underscores in my query, and it is working as expected. If I use it in the query alone, it functions correctly.

Can anyone please guide me on how to implement a diacritic search properly using Ruby on Rails and SQL? Any insights or suggestions would be greatly appreciated. Thank you!

Aucun commentaire:

Enregistrer un commentaire