1212key = environment_vars ["search_api_key" ]
1313
1414# Your index name
15- index_name = ' good-books'
15+ index_name = " good-books"
1616
1717# Create Azure SDK client
1818search_client = SearchClient (endpoint , index_name , AzureKeyCredential (key ))
@@ -22,12 +22,12 @@ def read_facets(facetsString):
2222 facets = facetsString .split ("," )
2323 output = {}
2424 for x in facets :
25- if ( x .find ('*' ) != - 1 ) :
26- newVal = x .replace ('*' , '' )
27- output [newVal ]= ' array'
28- else :
29- output [x ]= ' string'
30-
25+ if x .find ("*" ) != - 1 :
26+ newVal = x .replace ("*" , "" )
27+ output [newVal ] = " array"
28+ else :
29+ output [x ] = " string"
30+
3131 return output
3232
3333
@@ -36,107 +36,112 @@ def create_filter_expression(filter_list, facets):
3636 i = 0
3737 filter_expressions = []
3838 return_string = ""
39- separator = ' and '
39+ separator = " and "
4040
41- while ( i < len (filter_list )) :
41+ while i < len (filter_list ):
4242 field = filter_list [i ]["field" ]
4343 value = filter_list [i ]["value" ]
44-
45- if ( facets [field ] == ' array' ):
46- print (' array' )
47- filter_expressions .append (f' { field } /any(t: search.in(t, \ '{ value } \ ' , \' , \ ' ))' )
48- else :
49- print (' value' )
50- filter_expressions .append (f' { field } eq \ '{ value } \' ' )
51-
44+
45+ if facets [field ] == " array" :
46+ print (" array" )
47+ filter_expressions .append (f" { field } /any(t: search.in(t, '{ value } ', ', '))" )
48+ else :
49+ print (" value" )
50+ filter_expressions .append (f" { field } eq '{ value } '" )
51+
5252 i += 1
53-
54-
53+
5554 return_string = separator .join (filter_expressions )
5655
5756 return return_string
5857
58+
5959def new_shape (docs ):
60-
60+
6161 old_api_shape = list (docs )
62-
63- count = 0
62+
63+ count = 0
6464 client_side_expected_shape = []
65-
65+
6666 for item in old_api_shape :
67-
67+
6868 new_document = {}
69- new_document ["score" ]= item ["@search.score" ]
70- new_document ["highlights" ]= item ["@search.highlights" ]
69+ new_document ["score" ] = item ["@search.score" ]
70+ new_document ["highlights" ] = item ["@search.highlights" ]
7171
7272 new_shape = {}
73- new_shape ["id" ]= item ["id" ]
74- new_shape ["goodreads_book_id" ]= item ["goodreads_book_id" ]
75- new_shape ["best_book_id" ]= item ["best_book_id" ]
76- new_shape ["work_id" ]= item ["work_id" ]
77- new_shape ["books_count" ]= item ["books_count" ]
78- new_shape ["isbn" ]= item ["isbn" ]
79- new_shape ["isbn13" ]= item ["isbn13" ]
80- new_shape ["authors" ]= item ["authors" ]
81- new_shape ["original_publication_year" ]= item ["original_publication_year" ]
82- new_shape ["original_title" ]= item ["original_title" ]
83- new_shape ["title" ]= item ["title" ]
84- new_shape ["language_code" ]= item ["language_code" ]
85- new_shape ["average_rating" ]= item ["average_rating" ]
86- new_shape ["ratings_count" ]= item ["ratings_count" ]
87- new_shape ["work_ratings_count" ]= item ["work_ratings_count" ]
88- new_shape ["work_text_reviews_count" ]= item ["work_text_reviews_count" ]
89- new_shape ["ratings_1" ]= item ["ratings_1" ]
90- new_shape ["ratings_2" ]= item ["ratings_2" ]
91- new_shape ["ratings_3" ]= item ["ratings_3" ]
92- new_shape ["ratings_4" ]= item ["ratings_4" ]
93- new_shape ["ratings_5" ]= item ["ratings_5" ]
94- new_shape ["image_url" ]= item ["image_url" ]
95- new_shape ["small_image_url" ]= item ["small_image_url" ]
96-
97- new_document ["document" ]= new_shape
98-
73+ new_shape ["id" ] = item ["id" ]
74+ new_shape ["goodreads_book_id" ] = item ["goodreads_book_id" ]
75+ new_shape ["best_book_id" ] = item ["best_book_id" ]
76+ new_shape ["work_id" ] = item ["work_id" ]
77+ new_shape ["books_count" ] = item ["books_count" ]
78+ new_shape ["isbn" ] = item ["isbn" ]
79+ new_shape ["isbn13" ] = item ["isbn13" ]
80+ new_shape ["authors" ] = item ["authors" ]
81+ new_shape ["original_publication_year" ] = item ["original_publication_year" ]
82+ new_shape ["original_title" ] = item ["original_title" ]
83+ new_shape ["title" ] = item ["title" ]
84+ new_shape ["language_code" ] = item ["language_code" ]
85+ new_shape ["average_rating" ] = item ["average_rating" ]
86+ new_shape ["ratings_count" ] = item ["ratings_count" ]
87+ new_shape ["work_ratings_count" ] = item ["work_ratings_count" ]
88+ new_shape ["work_text_reviews_count" ] = item ["work_text_reviews_count" ]
89+ new_shape ["ratings_1" ] = item ["ratings_1" ]
90+ new_shape ["ratings_2" ] = item ["ratings_2" ]
91+ new_shape ["ratings_3" ] = item ["ratings_3" ]
92+ new_shape ["ratings_4" ] = item ["ratings_4" ]
93+ new_shape ["ratings_5" ] = item ["ratings_5" ]
94+ new_shape ["image_url" ] = item ["image_url" ]
95+ new_shape ["small_image_url" ] = item ["small_image_url" ]
96+
97+ new_document ["document" ] = new_shape
98+
9999 client_side_expected_shape .append (new_document )
100-
100+
101101 return list (client_side_expected_shape )
102102
103+
103104def main (req : func .HttpRequest ) -> func .HttpResponse :
104105
105106 # variables sent in body
106107 req_body = req .get_json ()
107- q = req_body .get ('q' )
108- top = req_body .get (' top' ) or 8
109- skip = req_body .get (' skip' ) or 0
110- filters = req_body .get (' filters' ) or []
108+ q = req_body .get ("q" )
109+ top = req_body .get (" top" ) or 8
110+ skip = req_body .get (" skip" ) or 0
111+ filters = req_body .get (" filters" ) or []
111112
112113 facets = environment_vars ["search_facets" ]
113114 facetKeys = read_facets (facets )
114-
115- filter = ""
116- if ( len (filters )):
115+
116+ filter = ""
117+ if len (filters ):
117118 filter = create_filter_expression (filters , facetKeys )
118119
119120 if q :
120121 logging .info (f"/Search q = { q } " )
121-
122- search_results = search_client .search (search_text = q , top = top ,skip = skip , facets = facetKeys , filter = filter , include_total_count = True )
123-
122+
123+ search_results = search_client .search (
124+ search_text = q ,
125+ top = top ,
126+ skip = skip ,
127+ facets = facetKeys ,
128+ filter = filter ,
129+ include_total_count = True ,
130+ )
131+
124132 returned_docs = new_shape (search_results )
125133 returned_count = search_results .get_count ()
126134 returned_facets = search_results .get_facets ()
127-
135+
128136 # format the React app expects
129137 full_response = {}
130-
131- full_response ["count" ]= search_results .get_count ()
132- full_response ["facets" ]= search_results .get_facets ()
133- full_response ["results" ]= returned_docs
134-
135-
136- return func .HttpResponse (body = json .dumps (full_response ), mimetype = "application/json" ,status_code = 200 )
137- else :
138+
139+ full_response ["count" ] = search_results .get_count ()
140+ full_response ["facets" ] = search_results .get_facets ()
141+ full_response ["results" ] = returned_docs
142+
138143 return func .HttpResponse (
139- "No query param found." ,
140- status_code = 200
144+ body = json .dumps (full_response ), mimetype = "application/json" , status_code = 200
141145 )
142-
146+ else :
147+ return func .HttpResponse ("No query param found." , status_code = 200 )
0 commit comments