Skip to content

Commit

Permalink
made fuzzy search better
Browse files Browse the repository at this point in the history
  • Loading branch information
majimearun committed Dec 1, 2022
1 parent 806f727 commit 55b33d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>me.xdrop</groupId>
<artifactId>fuzzywuzzy</artifactId>
<version>1.3.0</version>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import com.ecommerce.gomart.Product.ProductRepository;
import com.ecommerce.gomart.Stubs.SendCart;
import com.ecommerce.gomart.Stubs.SendOrder;

import me.xdrop.fuzzywuzzy.FuzzySearch;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -142,7 +145,10 @@ public List<Product> getProductsByName(String name) {

@Transactional
public List<Product> getProductsByFuzzyName(String name) {
return productRepository.findByFuzzyName(name);
List<Product> products = getProducts();
return products.stream()
.sorted((p1, p2) -> FuzzySearch.weightedRatio(p2.getName(), name) - FuzzySearch.weightedRatio(p1.getName(), name))
.collect(Collectors.toList());
}

@Transactional
Expand Down

0 comments on commit 55b33d9

Please sign in to comment.