diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2019-09-03 19:35:42 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2019-09-03 19:35:44 -0700 |
commit | ea3b8c33cf53b7e50cb353eac2c6f73f5cb75979 (patch) | |
tree | d441edde2ada1a7019f07bef9cfd20a9345b984a /python/fatcat_web/search.py | |
parent | 56e30786ccf326ff69deee9a0dcf86b032d3b1ae (diff) | |
download | fatcat-ea3b8c33cf53b7e50cb353eac2c6f73f5cb75979.tar.gz fatcat-ea3b8c33cf53b7e50cb353eac2c6f73f5cb75979.zip |
include example releases on container landing page
Included making release search results an entity macro (in webface
templates).
Diffstat (limited to 'python/fatcat_web/search.py')
-rw-r--r-- | python/fatcat_web/search.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/python/fatcat_web/search.py b/python/fatcat_web/search.py index 446a851a..94246329 100644 --- a/python/fatcat_web/search.py +++ b/python/fatcat_web/search.py @@ -6,6 +6,7 @@ the formal API) TODO: ELASTICSEARCH_*_INDEX should probably be factored out and just hard-coded """ +import datetime import requests from flask import abort, flash from fatcat_web import app @@ -242,3 +243,45 @@ def get_elastic_container_stats(ident, issnl=None): } return stats + +def get_elastic_container_random_releases(ident, limit=5): + """ + Returns a list of releases from the container. + """ + + assert limit > 0 and limit <= 100 + + query = { + "size": int(limit), + "sort": [ + { "in_web": {"order": "desc"} }, + { "release_date": {"order": "desc"} }, + ], + "query": { + "bool": { + "must": [ + { "term": { "container_id": ident } }, + { "range": { "release_year": { "lte": datetime.datetime.today().year } } }, + ], + }, + }, + } + resp = requests.get( + "{}/fatcat_release/_search".format(app.config['ELASTICSEARCH_BACKEND']), + json=query, + params=dict(request_cache="true")) + # TODO: abort() + #print(resp.json()) + resp.raise_for_status() + resp = resp.json() + print(resp) + hits = [h['_source'] for h in resp['hits']['hits']] + for h in hits: + # Handle surrogate strings that elasticsearch returns sometimes, + # probably due to mangled data processing in some pipeline. + # "Crimes against Unicode"; production workaround + for key in h: + if type(h[key]) is str: + h[key] = h[key].encode('utf8', 'ignore').decode('utf8') + + return hits |