Skip to content

Commit ce24eae

Browse files
authored
Merge pull request #1068 from thicolares/add-new-index-settings-facetSearch-prefixSearch
Add methods for new index settings: `facetSearch` and `prefixSearch`
2 parents 01483a3 + de723fa commit ce24eae

File tree

7 files changed

+210
-3
lines changed

7 files changed

+210
-3
lines changed

.code-samples.meilisearch.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,12 @@ update_pagination_settings_1: |-
626626
client.index('books').update_pagination_settings({'maxTotalHits': 100})
627627
reset_pagination_settings_1: |-
628628
client.index('books').reset_pagination_settings()
629+
get_facet_search_settings_1: |-
630+
client.index('books').get_facet_search_settings()
631+
update_facet_search_settings_1: |-
632+
client.index('books').update_facet_search_settings(False)
633+
reset_facet_search_settings_1: |-
634+
client.index('books').reset_facet_search_settings()
629635
get_faceting_settings_1: |-
630636
client.index('books').get_faceting_settings()
631637
update_faceting_settings_1: |-
@@ -716,6 +722,12 @@ update_search_cutoff_1: |-
716722
client.index('movies').update_search_cutoff_ms(150)
717723
reset_search_cutoff_1: |-
718724
client.index('movies').reset_search_cutoff_ms()
725+
get_prefix_search_settings_1: |-
726+
client.index('books').get_prefix_search()
727+
update_prefix_search_settings_1: |-
728+
client.index('books').update_prefix_search(PrefixSearch.DISABLED)
729+
reset_prefix_search_settings_1: |-
730+
client.index('books').reset_prefix_search()
719731
get_proximity_precision_settings_1: |-
720732
client.index('books').get_proximity_precision()
721733
update_proximity_precision_settings_1: |-

meilisearch/_httprequests.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
MeilisearchCommunicationError,
1313
MeilisearchTimeoutError,
1414
)
15-
from meilisearch.models.index import ProximityPrecision
15+
from meilisearch.models.index import PrefixSearch, ProximityPrecision
1616
from meilisearch.version import qualified_version
1717

1818

@@ -36,6 +36,7 @@ def send_request(
3636
Mapping[str, Any],
3737
Sequence[Mapping[str, Any]],
3838
List[str],
39+
bool,
3940
bytes,
4041
str,
4142
int,
@@ -67,7 +68,7 @@ def send_request(
6768
serialize_body = isinstance(body, dict) or body
6869
data = (
6970
json.dumps(body, cls=serializer)
70-
if serialize_body
71+
if isinstance(body, bool) or serialize_body
7172
else "" if body == "" else "null"
7273
)
7374

@@ -124,9 +125,11 @@ def put(
124125
Mapping[str, Any],
125126
Sequence[Mapping[str, Any]],
126127
List[str],
128+
bool,
127129
bytes,
128130
str,
129131
int,
132+
PrefixSearch,
130133
ProximityPrecision,
131134
]
132135
] = None,

meilisearch/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Paths:
4242
swap = "swap-indexes"
4343
embedders = "embedders"
4444
search_cutoff_ms = "search-cutoff-ms"
45+
prefix_search = "prefix-search"
4546
proximity_precision = "proximity-precision"
4647
localized_attributes = "localized-attributes"
4748
edit = "edit"

meilisearch/index.py

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
IndexStats,
4040
LocalizedAttributes,
4141
Pagination,
42+
PrefixSearch,
4243
ProximityPrecision,
4344
TypoTolerance,
4445
)
@@ -1665,6 +1666,57 @@ def reset_pagination_settings(self) -> TaskInfo:
16651666

16661667
return TaskInfo(**task)
16671668

1669+
def get_facet_search_settings(self) -> bool:
1670+
"""Get the facet search settings of an index.
1671+
1672+
Returns
1673+
-------
1674+
bool:
1675+
True if facet search is enabled, False if disabled.
1676+
Raises
1677+
------
1678+
MeilisearchApiError
1679+
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
1680+
"""
1681+
1682+
return self.http.get(self.__settings_url_for(self.config.paths.facet_search))
1683+
1684+
def update_facet_search_settings(self, body: Union[bool, None]) -> TaskInfo:
1685+
"""Update the facet search settings of the index.
1686+
1687+
Parameters
1688+
----------
1689+
body: bool
1690+
True to enable facet search, False to disable it.
1691+
1692+
Returns
1693+
-------
1694+
task_info:
1695+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
1696+
https://www.meilisearch.com/docs/reference/api/tasks#get-one-task
1697+
1698+
Raises
1699+
------
1700+
MeilisearchApiError
1701+
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
1702+
"""
1703+
task = self.http.put(self.__settings_url_for(self.config.paths.facet_search), body=body)
1704+
1705+
return TaskInfo(**task)
1706+
1707+
def reset_facet_search_settings(self) -> TaskInfo:
1708+
"""Reset facet search settings of the index to default values.
1709+
1710+
Returns
1711+
-------
1712+
task_info:
1713+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
1714+
https://www.meilisearch.com/docs/reference/api/tasks
1715+
"""
1716+
task = self.http.delete(self.__settings_url_for(self.config.paths.facet_search))
1717+
1718+
return TaskInfo(**task)
1719+
16681720
def get_faceting_settings(self) -> Faceting:
16691721
"""Get the faceting settings of an index.
16701722
@@ -1678,7 +1730,6 @@ def get_faceting_settings(self) -> Faceting:
16781730
MeilisearchApiError
16791731
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
16801732
"""
1681-
16821733
faceting = self.http.get(self.__settings_url_for(self.config.paths.faceting))
16831734

16841735
return Faceting(**faceting)
@@ -2078,6 +2129,58 @@ def reset_search_cutoff_ms(self) -> TaskInfo:
20782129

20792130
return TaskInfo(**task)
20802131

2132+
# PREFIX SEARCH
2133+
2134+
def get_prefix_search(self) -> PrefixSearch:
2135+
"""Get the prefix search settings of an index.
2136+
2137+
Returns
2138+
-------
2139+
settings:
2140+
The prefix search settings of the index.
2141+
2142+
Raises
2143+
------
2144+
MeilisearchApiError
2145+
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
2146+
"""
2147+
prefix_search = self.http.get(self.__settings_url_for(self.config.paths.prefix_search))
2148+
2149+
return PrefixSearch[to_snake(prefix_search).upper()]
2150+
2151+
def update_prefix_search(self, body: Union[PrefixSearch, None]) -> TaskInfo:
2152+
"""Update the prefix search settings of the index.
2153+
2154+
Parameters
2155+
----------
2156+
body:
2157+
Prefix search settings
2158+
2159+
Returns
2160+
-------
2161+
task_info:
2162+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
2163+
https://www.meilisearch.com/docs/reference/api/tasks
2164+
"""
2165+
task = self.http.put(self.__settings_url_for(self.config.paths.prefix_search), body)
2166+
2167+
return TaskInfo(**task)
2168+
2169+
def reset_prefix_search(self) -> TaskInfo:
2170+
"""Reset the prefix search settings of the index
2171+
2172+
Returns
2173+
-------
2174+
task_info:
2175+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
2176+
https://www.meilisearch.com/docs/reference/api/tasks
2177+
"""
2178+
task = self.http.delete(
2179+
self.__settings_url_for(self.config.paths.prefix_search),
2180+
)
2181+
2182+
return TaskInfo(**task)
2183+
20812184
# PROXIMITY PRECISION SETTINGS
20822185

20832186
def get_proximity_precision(self) -> ProximityPrecision:

meilisearch/models/index.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ class TypoTolerance(CamelBase):
4949
min_word_size_for_typos: Optional[MinWordSizeForTypos] = None
5050

5151

52+
class PrefixSearch(str, Enum):
53+
INDEXING_TIME = "indexingTime"
54+
"""
55+
Calculate prefix search during indexing. This is the default behavior.
56+
"""
57+
58+
DISABLED = "disabled"
59+
"""
60+
Do not calculate prefix search. May speed up indexing, but will severely impact search result relevancy.
61+
"""
62+
63+
5264
class ProximityPrecision(str, Enum):
5365
BY_WORD = "byWord"
5466
BY_ATTRIBUTE = "byAttribute"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
DEFAULT_FACET_SEARCH_SETTINGS_STATUS = True
2+
ENABLED_FACET_SEARCH_SETTINGS_STATUS = True
3+
DISABLED_FACET_SEARCH_SETTINGS_STATUS = False
4+
5+
6+
def test_get_facet_search_settings(empty_index):
7+
response = empty_index().get_facet_search_settings()
8+
9+
assert DEFAULT_FACET_SEARCH_SETTINGS_STATUS == response
10+
11+
12+
def test_update_facet_search_settings(empty_index):
13+
index = empty_index()
14+
15+
response = index.update_facet_search_settings(DISABLED_FACET_SEARCH_SETTINGS_STATUS)
16+
index.wait_for_task(response.task_uid)
17+
response = index.get_facet_search_settings()
18+
assert DISABLED_FACET_SEARCH_SETTINGS_STATUS == response
19+
20+
response = index.update_facet_search_settings(ENABLED_FACET_SEARCH_SETTINGS_STATUS)
21+
index.wait_for_task(response.task_uid)
22+
response = index.get_facet_search_settings()
23+
assert ENABLED_FACET_SEARCH_SETTINGS_STATUS == response
24+
25+
26+
def test_reset_facet_search_settings(empty_index):
27+
index = empty_index()
28+
29+
response = index.update_facet_search_settings(DISABLED_FACET_SEARCH_SETTINGS_STATUS)
30+
index.wait_for_task(response.task_uid)
31+
response = index.get_facet_search_settings()
32+
assert DISABLED_FACET_SEARCH_SETTINGS_STATUS == response
33+
assert DEFAULT_FACET_SEARCH_SETTINGS_STATUS != response
34+
35+
response = index.reset_facet_search_settings()
36+
index.wait_for_task(response.task_uid)
37+
response = index.get_facet_search_settings()
38+
assert DEFAULT_FACET_SEARCH_SETTINGS_STATUS == response
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from meilisearch.models.index import PrefixSearch
2+
3+
DEFAULT_PREFIX_SEARCH_SETTINGS = PrefixSearch.INDEXING_TIME
4+
5+
6+
def test_get_prefix_search(empty_index):
7+
response = empty_index().get_prefix_search()
8+
9+
assert DEFAULT_PREFIX_SEARCH_SETTINGS == response
10+
11+
12+
def test_update_prefix_search(empty_index):
13+
index = empty_index()
14+
15+
response = index.update_prefix_search(PrefixSearch.DISABLED)
16+
index.wait_for_task(response.task_uid)
17+
response = index.get_prefix_search()
18+
assert PrefixSearch.DISABLED == response
19+
20+
response = index.update_prefix_search(PrefixSearch.INDEXING_TIME)
21+
index.wait_for_task(response.task_uid)
22+
response = index.get_prefix_search()
23+
assert PrefixSearch.INDEXING_TIME == response
24+
25+
26+
def test_reset_prefix_search(empty_index):
27+
index = empty_index()
28+
29+
response = index.update_prefix_search(PrefixSearch.DISABLED)
30+
index.wait_for_task(response.task_uid)
31+
response = index.get_prefix_search()
32+
assert PrefixSearch.DISABLED == response
33+
assert DEFAULT_PREFIX_SEARCH_SETTINGS != response
34+
35+
response = index.reset_prefix_search()
36+
index.wait_for_task(response.task_uid)
37+
response = index.get_prefix_search()
38+
assert DEFAULT_PREFIX_SEARCH_SETTINGS == response

0 commit comments

Comments
 (0)