Skip to content

Commit

Permalink
Add config to control track_total_hits. (#94)
Browse files Browse the repository at this point in the history
* Add config to control track_total_hits.

* fix format

Co-authored-by: Petr Jasek <jasekpetr@gmail.com>
  • Loading branch information
ride90 and petrjasek authored Aug 3, 2020
1 parent ddbf26f commit c009d62
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
16 changes: 13 additions & 3 deletions eve_elastic/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ def init_app(self, app):
app.config.setdefault("ELASTICSEARCH_INDEXES", {})
app.config.setdefault("ELASTICSEARCH_FORCE_REFRESH", True)
app.config.setdefault("ELASTICSEARCH_AUTO_AGGREGATIONS", True)
# https://www.elastic.co/guide/en/elasticsearch/reference/master/search-request-body.html#request-body-search-track-total-hits # NOQA
app.config.setdefault("ELASTICSEARCH_TRACK_TOTAL_HITS", 10000)

self.app = app
self.index = app.config["ELASTICSEARCH_INDEX"]
Expand All @@ -374,10 +376,10 @@ def init_index(self):
if app.config.get("DEBUG"):
raise
else:
raise
logger.error(
"mapping error, updating settings resource=%s", resource
)
raise

def _init_index(self, es, index, settings=None, mapping=None):
if not es.indices.exists(index):
Expand Down Expand Up @@ -983,8 +985,16 @@ def search(self, query, resources, params=None):
They must use all same elastic instance and should be same schema.
"""
if params is None:
params = {}

default_params = {
"track_total_hits": self.app.config["ELASTICSEARCH_TRACK_TOTAL_HITS"]
}

if params is not None:
default_params.update(params)

params = default_params

if isinstance(resources, str):
resources = resources.split(",")
index = [self._resource_index(resource) for resource in resources]
Expand Down
4 changes: 2 additions & 2 deletions eve_elastic/validation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from cerberus import Validator
from cerberus import Validator as _Validator


class Validator(Validator):
class Validator(_Validator):
pass

0 comments on commit c009d62

Please sign in to comment.