1
1
from meilisearch ._httprequests import HttpRequests
2
2
from meilisearch .document import Document
3
- from meilisearch .search import Search
3
+ # from meilisearch.search import Search
4
4
# from meilisearch.stat import Stat
5
5
from meilisearch .setting import Setting
6
+ import urllib
6
7
7
8
# pylint: disable=too-many-ancestors
8
- class Index (Document , Search , Setting ):
9
+ class Index (Document , Setting ):
9
10
"""
10
11
Indexes routes wrapper
11
12
@@ -20,6 +21,7 @@ class Index(Document, Search, Setting):
20
21
index_path = 'indexes'
21
22
update_path = 'updates'
22
23
stat_path = 'stats'
24
+ search_path = 'search'
23
25
uid = ""
24
26
25
27
class Update :
@@ -86,6 +88,38 @@ def __init__(self, parent_path, config, uid=None, name=None):
86
88
self .uid = uid
87
89
self .index_path = parent_path
88
90
91
+ class Search :
92
+ """
93
+ Search routes wrapper
94
+
95
+ Index's parent that gives access to all the search methods of meilisearch.
96
+ https://docs.meilisearch.com/references/search.html#search-in-an-index
97
+
98
+ Attributes
99
+ ----------
100
+ search_path:
101
+ Search url path
102
+ """
103
+ search_path = 'search'
104
+
105
+ def __init__ (self , parent_path , config , uid = None , name = None ):
106
+ """
107
+ Parameters
108
+ ----------
109
+ config : Config
110
+ Config object containing permission and location of meilisearch
111
+ name: str
112
+ Name of the index on which to perform the index actions.
113
+ uid: str
114
+ Uid of the index on which to perform the index actions.
115
+ index_path: str
116
+ Index url path
117
+ """
118
+ self .config = config
119
+ self .name = name
120
+ self .uid = uid
121
+ self .index_path = parent_path
122
+
89
123
def __init__ (self , config , uid ):
90
124
"""
91
125
Parameters
@@ -97,7 +131,7 @@ def __init__(self, config, uid):
97
131
index_path: str
98
132
Index url path
99
133
"""
100
- Search .__init__ (self , Index .index_path , config , uid )
134
+ # Search.__init__(self, Index.index_path, config, uid)
101
135
Document .__init__ (self , Index .index_path , config , uid )
102
136
# Stat.__init__(self, Index.index_path, config, uid)
103
137
Setting .__init__ (self , Index .index_path , config , uid )
@@ -276,3 +310,33 @@ def get_stats(self):
276
310
self .stat_path ,
277
311
)
278
312
)
313
+
314
+ # pylint: disable=dangerous-default-value
315
+ # Not dangerous because opt_params is not modified in the method
316
+ # See: https://stackoverflow.com/questions/26320899/why-is-the-empty-dictionary-a-dangerous-default-value-in-python
317
+ def search (self , query , opt_params = {}):
318
+ """Search in meilisearch
319
+
320
+ Parameters
321
+ ----------
322
+ query: str
323
+ String containing the searched word(s)
324
+ opt_params: dict
325
+ Dictionnary containing optional query parameters
326
+ https://docs.meilisearch.com/references/search.html#search-in-an-index
327
+ Returns
328
+ ----------
329
+ results: `dict`
330
+ Dictionnary with hits, offset, limit, processingTime and initial query
331
+ """
332
+ search_param = {'q' : query }
333
+ params = {** search_param , ** opt_params }
334
+ return HttpRequests .get (
335
+ self .config ,
336
+ '{}/{}/{}?{}' .format (
337
+ self .index_path ,
338
+ self .uid ,
339
+ self .search_path ,
340
+ urllib .parse .urlencode (params ))
341
+ )
342
+
0 commit comments