@@ -22,6 +22,7 @@ enum Modifier {
2222 Pre = 0 ,
2323 Post = 1 ,
2424 Yelp = 2 ,
25+ LocationSign = 3 ,
2526}
2627
2728impl ToSql for Modifier {
@@ -53,11 +54,6 @@ const MAX_QUERY_LENGTH: usize = 150;
5354/// "keyword=:modifier" (please see is_modifier()), define this how many words we should check.
5455const MAX_MODIFIER_WORDS_NUMBER : usize = 2 ;
5556
56- /// The max number of words consisting the location sign. To improve the SQL performance by matching
57- /// with "keyword=:modifier" (please see is_location_sign()), define this how many words we should
58- /// check.
59- const MAX_LOCATION_SIGN_WORDS_NUMBER : usize = 2 ;
60-
6157/// At least this many characters must be typed for a subject to be matched.
6258const SUBJECT_PREFIX_MATCH_THRESHOLD : usize = 2 ;
6359
@@ -115,14 +111,14 @@ impl SuggestDao<'_> {
115111 ) ?;
116112 }
117113
118- for sign in & suggestion. location_signs {
114+ for keyword in & suggestion. location_signs {
119115 self . scope . err_if_interrupted ( ) ?;
120116 self . conn . execute_cached (
121- "INSERT INTO yelp_location_signs (record_id, keyword, need_location ) VALUES(:record_id, :keyword , :need_location )" ,
117+ "INSERT INTO yelp_modifiers (record_id, type, keyword ) VALUES(:record_id, :type , :keyword )" ,
122118 named_params ! {
123119 ":record_id" : record_id. as_str( ) ,
124- ":keyword " : sign . keyword ,
125- ":need_location " : sign . need_location ,
120+ ":type " : Modifier :: LocationSign ,
121+ ":keyword " : keyword ,
126122 } ,
127123 ) ?;
128124 }
@@ -178,7 +174,8 @@ impl SuggestDao<'_> {
178174 query_words = & query_words[ n..] ;
179175 }
180176
181- let location_sign_tuple = self . find_location_sign ( query_words) ?;
177+ let location_sign_tuple =
178+ self . find_modifier ( query_words, Modifier :: LocationSign , FindFrom :: First ) ?;
182179 if let Some ( ( _, n) ) = location_sign_tuple {
183180 query_words = & query_words[ n..] ;
184181 }
@@ -201,7 +198,6 @@ impl SuggestDao<'_> {
201198 subject_exact_match : subject_tuple. 1 ,
202199 pre_modifier : pre_modifier_tuple. map ( |( words, _) | words. to_string ( ) ) ,
203200 post_modifier : post_modifier_tuple. map ( |( words, _) | words. to_string ( ) ) ,
204- need_location : location_sign_tuple. is_some ( ) || location. is_some ( ) ,
205201 location_sign : location_sign_tuple. map ( |( words, _) | words. to_string ( ) ) ,
206202 location,
207203 icon,
@@ -327,52 +323,6 @@ impl SuggestDao<'_> {
327323 Ok ( None )
328324 }
329325
330- /// Find the location sign for given query.
331- /// It returns Option<tuple> as follows:
332- /// (
333- /// String: The keyword in DB (but the case is inherited by query).
334- /// usize: Number of words in query_words that match the keyword.
335- /// Maximum number is MAX_LOCATION_SIGN_WORDS_NUMBER.
336- /// )
337- fn find_location_sign ( & self , query_words : & [ & str ] ) -> Result < Option < ( String , usize ) > > {
338- if query_words. is_empty ( ) {
339- return Ok ( None ) ;
340- }
341-
342- for n in ( 1 ..=std:: cmp:: min ( MAX_LOCATION_SIGN_WORDS_NUMBER , query_words. len ( ) ) ) . rev ( ) {
343- let mut candidate_chunk = query_words. chunks ( n) ;
344- let candidate = candidate_chunk
345- . next ( )
346- . map ( |chunk| chunk. join ( " " ) )
347- . unwrap_or_default ( ) ;
348- if let Some ( keyword_lowercase) = self . conn . try_query_one :: < String , _ > (
349- if n == query_words. len ( ) {
350- "
351- SELECT keyword FROM yelp_location_signs
352- WHERE keyword BETWEEN :word AND :word || x'FFFF'
353- LIMIT 1
354- "
355- } else {
356- "
357- SELECT keyword FROM yelp_location_signs
358- WHERE keyword = :word
359- LIMIT 1
360- "
361- } ,
362- named_params ! {
363- ":word" : candidate. to_lowercase( ) ,
364- } ,
365- true ,
366- ) ? {
367- // Preserve the query as the user typed it including its case.
368- let keyword = format ! ( "{}{}" , candidate, & keyword_lowercase[ candidate. len( ) ..] ) ;
369- return Ok ( Some ( ( keyword, n) ) ) ;
370- }
371- }
372-
373- Ok ( None )
374- }
375-
376326 /// Fetch the custom details for Yelp suggestions.
377327 /// It returns the location tuple as follows:
378328 /// (
@@ -421,25 +371,17 @@ struct SuggestionBuilder<'a> {
421371 post_modifier : Option < String > ,
422372 location_sign : Option < String > ,
423373 location : Option < String > ,
424- need_location : bool ,
425374 icon : Option < Vec < u8 > > ,
426375 icon_mimetype : Option < String > ,
427376 score : f64 ,
428377}
429378
430379impl < ' a > From < SuggestionBuilder < ' a > > for Suggestion {
431380 fn from ( builder : SuggestionBuilder < ' a > ) -> Suggestion {
432- // This location sign such the 'near by' needs to add as a description parameter.
433- let location_modifier = if !builder. need_location {
434- builder. location_sign . as_deref ( )
435- } else {
436- None
437- } ;
438381 let description = [
439382 builder. pre_modifier . as_deref ( ) ,
440383 Some ( builder. subject ) ,
441384 builder. post_modifier . as_deref ( ) ,
442- location_modifier,
443385 ]
444386 . iter ( )
445387 . flatten ( )
@@ -451,7 +393,7 @@ impl<'a> From<SuggestionBuilder<'a>> for Suggestion {
451393 let mut url = String :: from ( "https://www.yelp.com/search?" ) ;
452394 let mut parameters = form_urlencoded:: Serializer :: new ( String :: new ( ) ) ;
453395 parameters. append_pair ( "find_desc" , & description) ;
454- if let ( Some ( location) , true ) = ( & builder. location , builder . need_location ) {
396+ if let Some ( location) = & builder. location {
455397 parameters. append_pair ( "find_loc" , location) ;
456398 }
457399 url. push_str ( & parameters. finish ( ) ) ;
@@ -475,7 +417,7 @@ impl<'a> From<SuggestionBuilder<'a>> for Suggestion {
475417 icon : builder. icon ,
476418 icon_mimetype : builder. icon_mimetype ,
477419 score : builder. score ,
478- has_location_sign : location_modifier . is_none ( ) && builder. location_sign . is_some ( ) ,
420+ has_location_sign : builder. location_sign . is_some ( ) ,
479421 subject_exact_match : builder. subject_exact_match ,
480422 location_param : "find_loc" . to_string ( ) ,
481423 }
0 commit comments