Skip to content

Commit f70551f

Browse files
committed
Integrate Search into Index
1 parent 158a5fd commit f70551f

File tree

2 files changed

+67
-66
lines changed

2 files changed

+67
-66
lines changed

meilisearch/index.py

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from meilisearch._httprequests import HttpRequests
22
from meilisearch.document import Document
3-
from meilisearch.search import Search
3+
# from meilisearch.search import Search
44
# from meilisearch.stat import Stat
55
from meilisearch.setting import Setting
6+
import urllib
67

78
# pylint: disable=too-many-ancestors
8-
class Index(Document, Search, Setting):
9+
class Index(Document, Setting):
910
"""
1011
Indexes routes wrapper
1112
@@ -20,6 +21,7 @@ class Index(Document, Search, Setting):
2021
index_path = 'indexes'
2122
update_path = 'updates'
2223
stat_path = 'stats'
24+
search_path = 'search'
2325
uid = ""
2426

2527
class Update:
@@ -86,6 +88,38 @@ def __init__(self, parent_path, config, uid=None, name=None):
8688
self.uid = uid
8789
self.index_path = parent_path
8890

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+
89123
def __init__(self, config, uid):
90124
"""
91125
Parameters
@@ -97,7 +131,7 @@ def __init__(self, config, uid):
97131
index_path: str
98132
Index url path
99133
"""
100-
Search.__init__(self, Index.index_path, config, uid)
134+
# Search.__init__(self, Index.index_path, config, uid)
101135
Document.__init__(self, Index.index_path, config, uid)
102136
# Stat.__init__(self, Index.index_path, config, uid)
103137
Setting.__init__(self, Index.index_path, config, uid)
@@ -276,3 +310,33 @@ def get_stats(self):
276310
self.stat_path,
277311
)
278312
)
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+

meilisearch/search.py

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)