Modify search to work with article contents
This commit is contained in:
+15
-11
@@ -11,7 +11,7 @@ SEARCH_ENABLED = bool(settings.MEILI_URL)
|
||||
def meili_api(method, route, json=None, params=None, parse_json=True):
|
||||
try:
|
||||
headers = {'Authorization': 'Bearer ' + settings.MEILI_API_KEY}
|
||||
r = method(settings.MEILI_URL + route, json=json, params=params, timeout=4)
|
||||
r = method(settings.MEILI_URL + route, json=json, params=params, headers=headers, timeout=4)
|
||||
if r.status_code > 299:
|
||||
raise Exception('Bad response code ' + str(r.status_code))
|
||||
if parse_json:
|
||||
@@ -25,32 +25,36 @@ def meili_api(method, route, json=None, params=None, parse_json=True):
|
||||
logging.error('Problem with MeiliSearch api route: %s: %s', route, str(e))
|
||||
return False
|
||||
|
||||
def create_index():
|
||||
json = dict(uid='qotnews', primaryKey='id')
|
||||
return meili_api(requests.post, 'indexes', json=json)
|
||||
|
||||
def update_settings():
|
||||
json = {
|
||||
'rankingRules': ['typo', 'words', 'proximity', 'date:desc', 'exactness'],
|
||||
'searchableAttributes': ['title', 'url', 'author'],
|
||||
'displayedAttributes': ['id', 'ref', 'source', 'author', 'author_link', 'score', 'date', 'title', 'link', 'url', 'num_comments'],
|
||||
'rankingRules': ['words', 'typo', 'proximity', 'attribute', 'date:desc', 'exactness'],
|
||||
'searchableAttributes': ['title', 'url', 'author', 'text'],
|
||||
'displayedAttributes': ['id', 'ref', 'source', 'author', 'author_link', 'score', 'date', 'title', 'link', 'url', 'num_comments', 'text'],
|
||||
'stopWords': ['a', 'an', 'the', 'and', 'or', 'but', 'if', 'in', 'on', 'at', 'by', 'for', 'with', 'to', 'from', 'of', 'is', 'it', 'that', 'this'],
|
||||
}
|
||||
return meili_api(requests.post, 'indexes/qotnews/settings', json=json)
|
||||
return meili_api(requests.patch, 'indexes/qotnews/settings', json=json)
|
||||
|
||||
def init():
|
||||
if not SEARCH_ENABLED:
|
||||
logging.info('Search is not enabled, skipping init.')
|
||||
return
|
||||
print(create_index())
|
||||
update_settings()
|
||||
|
||||
def put_story(story):
|
||||
if not SEARCH_ENABLED: return
|
||||
return meili_api(requests.post, 'indexes/qotnews/documents', [story])
|
||||
|
||||
def search(q):
|
||||
def search(q, in_article=False):
|
||||
if not SEARCH_ENABLED: return []
|
||||
|
||||
json = dict(q=q, limit=settings.FEED_LENGTH)
|
||||
|
||||
if True:
|
||||
json['attributesToSearchOn'] = ['text']
|
||||
json['attributesToCrop'] = ['text']
|
||||
json['attributesToRetrieve'] = ['id', 'ref', 'source', 'author', 'author_link', 'score', 'date', 'title', 'link', 'url', 'num_comments']
|
||||
json['cropLength'] = 80
|
||||
|
||||
r = meili_api(requests.post, 'indexes/qotnews/search', json=json, parse_json=False)
|
||||
return r
|
||||
|
||||
|
||||
Reference in New Issue
Block a user