def test_insert_writes_to_elasticsearch(settings, elasticsearch, valid_build): build = valid_build() inserted = Build.insert(build) assert inserted # Because Elasticsearch is async, the content written won't be there # until we wait or flush. elasticsearch.flush() search = BuildDoc.search() response = search.execute() assert response.hits.total == 1 (build_doc, ) = response assert build_doc.id == inserted.id as_dict = build_doc.to_dict() as_dict.pop("id") # Can't easily compare these because elasticseach_dsl will convert # dates to datetime.datetime objects. # But if we convert dates from the Elasticsearch query to a string # we can compare. as_dict["build"]["date"] = as_dict["build"]["date"].isoformat()[:19] as_dict["download"]["date"] = as_dict["download"]["date"].isoformat()[:19] build = inserted.build build["build"]["date"] = build["build"]["date"][:19] build["download"]["date"] = build["download"]["date"][:19] assert as_dict == build
def search(request): """Proxy requests to Elasticsearch""" search = BuildDoc.search() arguments = None if request.method in ("POST", ): try: arguments = json.loads(request.body.decode("utf-8")) except json.JSONDecodeError as exception: return http.JsonResponse({"error": str(exception)}, status=400) if arguments: size = int(arguments.get("size", 0)) if size and size > settings.MAX_SEARCH_SIZE: return http.JsonResponse( {"error": f"Search size too large ({arguments['size']})"}, status=400, ) try: search.update_from_dict(arguments) except (UnknownDslObject, ValueError) as exception: return http.JsonResponse({"error": exception.args[0]}, status=400) metrics.incr("api_search_requests", tags=[f"method:{request.method}"]) try: response = search.execute() except RequestError as exception: return http.JsonResponse(exception.info, status=400) except TransportError as exception: return http.JsonResponse( {"error": exception.info["error"]["root_cause"][0]["reason"]}, status=400) logger.info(f"Finding {format(response.hits.total, ',')} total records.") metrics.gauge("api_search_records", response.hits.total) response_dict = response.to_dict() http_response = http.JsonResponse(response_dict) return http_response
def search(request): """Proxy requests to Elasticsearch""" search = BuildDoc.search() if request.method in ('POST', ): arguments = json.loads(request.body.decode('utf-8')) if arguments: search.update_from_dict(arguments) response = search.execute() logger.info(f"Finding {format(response.hits.total, ',')} total records.") response_dict = response.to_dict() http_response = http.JsonResponse(response_dict) return http_response
def search(request): """Proxy requests to Elasticsearch""" search = BuildDoc.search() arguments = None if request.method in ("POST", ): arguments = json.loads(request.body.decode("utf-8")) if arguments: search.update_from_dict(arguments) metrics.incr("api_search_requests", tags=[f"method:{request.method}"]) response = search.execute() logger.info(f"Finding {format(response.hits.total, ',')} total records.") metrics.gauge("api_search_records", response.hits.total) response_dict = response.to_dict() http_response = http.JsonResponse(response_dict) return http_response
def to_search(self, **kwargs): return BuildDoc.create(self.id, **self.build)