Skip to content

Commit acd94eb

Browse files
committed
Allow multiple string algos
1 parent 946f922 commit acd94eb

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

pyxivapi/client.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import logging
22
from typing import List
33

4-
from .exceptions import XIVAPIBadRequest, XIVAPIForbidden, XIVAPINotFound, XIVAPIServiceUnavailable, XIVAPIInvalidLanguage, XIVAPIError, XIVAPIInvalidIndex, XIVAPIInvalidColumns
4+
from .exceptions import XIVAPIBadRequest, XIVAPIForbidden, XIVAPINotFound, XIVAPIServiceUnavailable, \
5+
XIVAPIInvalidLanguage, XIVAPIError, XIVAPIInvalidIndex, XIVAPIInvalidColumns, XIVAPIInvalidAlgo
56
from .decorators import timed
67
from .models import Filter, Sort
78

@@ -25,7 +26,10 @@ def __init__(self, session, api_key):
2526

2627
self.base_url = "https://xivapi.com"
2728
self.languages = ["en", "fr", "de", "ja"]
28-
29+
self.string_algos = [
30+
"custom", "wildcard", "wildcard_plus", "fuzzy", "term", "prefix", "match", "match_phrase",
31+
"match_phrase_prefix", "multi_match", "query_string"
32+
]
2933

3034
@timed
3135
async def character_search(self, world, forename, surname, page=1):
@@ -242,7 +246,7 @@ async def pvpteam_by_id(self, lodestone_id):
242246

243247

244248
@timed
245-
async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]=(), sort: Sort=None, page=1, language="en"):
249+
async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]=(), sort: Sort=None, page=1, language="en", string_algo="match"):
246250
"""|coro|
247251
Search for data from on specific indexes.
248252
Parameters
@@ -265,6 +269,10 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
265269
Optional[language: str]
266270
The two character length language code that indicates the language to return the response in. Defaults to English (en).
267271
Valid values are "en", "fr", "de" & "ja"
272+
Optional[string_algo: str]
273+
The search algorithm to use for string matching (default = "match")
274+
Valid values are "custom", "wildcard", "wildcard_plus", "fuzzy", "term", "prefix", "match", "match_phrase",
275+
"match_phrase_prefix", "multi_match", "query_string"
268276
"""
269277

270278
if len(indexes) == 0:
@@ -276,14 +284,17 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
276284
if len(columns) == 0:
277285
raise XIVAPIInvalidColumns("Please specify at least one column to return in the resulting data.")
278286

287+
if string_algo not in self.string_algos:
288+
raise XIVAPIInvalidAlgo(f'"{string_algo}" is not a supported string_algo for XIVAPI')
289+
279290
body = {
280291
"indexes": ",".join(list(set(indexes))),
281292
"columns": "ID",
282-
"body" : {
293+
"body": {
283294
"query": {
284295
"bool": {
285296
"should": [{
286-
"match": {
297+
string_algo: {
287298
"NameCombined_en": {
288299
"query": name,
289300
"fuzziness": "AUTO",
@@ -292,7 +303,7 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
292303
}
293304
}
294305
}, {
295-
"match": {
306+
string_algo: {
296307
"NameCombined_de": {
297308
"query": name,
298309
"fuzziness": "AUTO",
@@ -301,7 +312,7 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
301312
}
302313
}
303314
}, {
304-
"match": {
315+
string_algo: {
305316
"NameCombined_fr": {
306317
"query": name,
307318
"fuzziness": "AUTO",
@@ -310,7 +321,7 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
310321
}
311322
}
312323
}, {
313-
"match": {
324+
string_algo: {
314325
"NameCombined_ja": {
315326
"query": name,
316327
"fuzziness": "AUTO",
@@ -385,7 +396,6 @@ async def index_by_id(self, index, content_id: int, columns=(), language="en"):
385396
async with self.session.get(url, params=params) as response:
386397
return await self.process_response(response)
387398

388-
389399
@timed
390400
async def lore_search(self, query, language="en"):
391401
"""|coro|

pyxivapi/exceptions.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,11 @@ class XIVAPIError(Exception):
7272
"""
7373
XIVAPI error
7474
"""
75-
pass
75+
pass
76+
77+
78+
class XIVAPIInvalidAlgo(Exception):
79+
"""
80+
Invalid String Algo
81+
"""
82+
pass

0 commit comments

Comments
 (0)