1313import org .elasticsearch .search .aggregations .Aggregation ;
1414import org .elasticsearch .search .aggregations .AggregationBuilders ;
1515import org .springframework .beans .factory .annotation .Autowired ;
16+ import org .springframework .data .domain .PageRequest ;
1617import org .springframework .data .elasticsearch .core .ElasticsearchOperations ;
1718import org .springframework .data .elasticsearch .core .SearchHit ;
1819import org .springframework .data .elasticsearch .core .SearchHits ;
2728import org .springframework .stereotype .Service ;
2829
2930import io .pratik .elasticsearch .models .Product ;
30- import io .pratik .elasticsearch .models .SearchSuggest ;
31- import io .pratik .elasticsearch .repositories .SearchSuggestRepository ;
3231import lombok .extern .slf4j .Slf4j ;
3332
3433/**
3938public class ProductSearchService {
4039
4140 private static final String PRODUCT_INDEX = "productindex" ;
42- private static final String SEARCH_SUGGEST_INDEX = "searchsuggest" ;
4341
4442 private ElasticsearchOperations elasticsearchOperations ;
45- private SearchSuggestRepository searchSuggestRepository ;
4643
4744 @ Autowired
48- public ProductSearchService (final ElasticsearchOperations elasticsearchOperations , final SearchSuggestRepository searchSuggestRepository ) {
45+ public ProductSearchService (final ElasticsearchOperations elasticsearchOperations ) {
4946 super ();
5047 this .elasticsearchOperations = elasticsearchOperations ;
51- this .searchSuggestRepository = searchSuggestRepository ;
5248 }
5349
5450 public List <String > createProductIndexBulk (final List <Product > products ) {
@@ -70,7 +66,7 @@ public String createProductIndex(Product product) {
7066 return documentId ;
7167 }
7268
73- public void findProductCountByBrand (final String brandName ) {
69+ public void findProductsByBrand (final String brandName ) {
7470 QueryBuilder queryBuilder = QueryBuilders
7571 .matchQuery ("manufacturer" , brandName );
7672 // .fuzziness(0.8)
@@ -79,8 +75,6 @@ public void findProductCountByBrand(final String brandName) {
7975 // .fuzzyTranspositions(true);
8076
8177 Query searchQuery = new NativeSearchQueryBuilder ()
82- //.addAggregation(AggregationBuilders
83- // .cardinality("category"))
8478 .withQuery (queryBuilder )
8579 .build ();
8680
@@ -91,18 +85,18 @@ public void findProductCountByBrand(final String brandName) {
9185
9286 log .info ("productHits {} {}" , productHits .getSearchHits ().size (), productHits .getSearchHits ());
9387
94- List <SearchHit <Product >> srchHits =
88+ List <SearchHit <Product >> searchHits =
9589 productHits .getSearchHits ();
9690 int i = 0 ;
97- for (SearchHit <Product > srchHit : srchHits ) {
98- log .info ("srchHit {}" , srchHit );
91+ for (SearchHit <Product > searchHit : searchHits ) {
92+ log .info ("searchHit {}" , searchHit );
9993 }
10094
10195 }
10296
10397 public void findByProductName (final String productName ) {
10498 Query searchQuery = new StringQuery (
105- "{ \" match\" : { \" name\" : { \" query\" : \" " + productName + "\" } } } \" " );
99+ "{\" match\" :{ \" name\" :{ \" query\" :\" " + productName + "\" }}} \" " );
106100
107101 SearchHits <Product > products = elasticsearchOperations .search (searchQuery , Product .class ,
108102 IndexCoordinates .of (PRODUCT_INDEX ));
@@ -119,10 +113,7 @@ public void findByProductPrice(final String productPrice) {
119113 public List <Product > processSearch (final String query ) {
120114 log .info ("Search with query {}" , query );
121115
122- // 1. Update searchsuggest Index
123- updateSuggestionsIndex (query );
124-
125- // 2. Create query on multiple fields enabling fuzzy search
116+ // 1. Create query on multiple fields enabling fuzzy search
126117 QueryBuilder queryBuilder =
127118 QueryBuilders
128119 .multiMatchQuery (query , "name" , "description" )
@@ -132,13 +123,13 @@ public List<Product> processSearch(final String query) {
132123 .withFilter (queryBuilder )
133124 .build ();
134125
135- // 3 . Execute search
126+ // 2 . Execute search
136127 SearchHits <Product > productHits =
137128 elasticsearchOperations
138129 .search (searchQuery , Product .class ,
139130 IndexCoordinates .of (PRODUCT_INDEX ));
140131
141- // 4 . Map searchHits to product list
132+ // 3 . Map searchHits to product list
142133 List <Product > productMatches = new ArrayList <Product >();
143134 productHits .forEach (srchHit ->{
144135 productMatches .add (srchHit .getContent ());
@@ -147,32 +138,26 @@ public List<Product> processSearch(final String query) {
147138 }
148139
149140
150- public void updateSuggestionsIndex (String query ) {
151- if (query .getBytes ().length < 512 ) {
152- searchSuggestRepository
153- .save (SearchSuggest
154- .builder ()
155- .id (query )
156- .searchText (query )
157- .build ());
158- }
159- }
141+
160142
161- public List <String > fetchRecentSuggestions (String query ) {
143+ public List <String > fetchSuggestions (String query ) {
162144 QueryBuilder queryBuilder = QueryBuilders
163- .wildcardQuery ("searchText " , query +"*" );
145+ .wildcardQuery ("name " , query +"*" );
164146
165147 Query searchQuery = new NativeSearchQueryBuilder ()
166- .withFilter (queryBuilder ).build ();
148+ .withFilter (queryBuilder )
149+ .withPageable (PageRequest .of (0 , 5 ))
150+ .build ();
167151
168- SearchHits <SearchSuggest > searchSuggestions =
152+ SearchHits <Product > searchSuggestions =
169153 elasticsearchOperations .search (searchQuery ,
170- SearchSuggest .class ,
171- IndexCoordinates .of (SEARCH_SUGGEST_INDEX ));
154+ Product .class ,
155+ IndexCoordinates .of (PRODUCT_INDEX ));
172156
173157 List <String > suggestions = new ArrayList <String >();
174- searchSuggestions .getSearchHits ().forEach (srchHit ->{
175- suggestions .add (srchHit .getContent ().getSearchText ());
158+
159+ searchSuggestions .getSearchHits ().forEach (searchHit ->{
160+ suggestions .add (searchHit .getContent ().getName ());
176161 });
177162 return suggestions ;
178163 }
0 commit comments