Skip to content

Commit

Permalink
Trying filters
Browse files Browse the repository at this point in the history
  • Loading branch information
mboudet committed Nov 23, 2023
1 parent 764b52a commit d134fe4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ES_SEARCH_FIELDS = List of comma-separated fields to search. Default to None, wh
ES_DISPLAY_FIELDS = List of comma-separated fields to return. Default to all
ES_MAX_RESULTS = Number of results returned by the query. Default to 10
ES_HIGHLIGHT_TAG = Html tag (without <>) wrapping the highlights (matched terms). Default to 'em'
ES_ALLOWED_FILTERS = List of comma-separated fields that can be used as direct filters. Default to none
```

Simple send your query to the '/' or '/query' endpoint, with the query itself as the 'q' get parameter.
Expand All @@ -25,3 +26,5 @@ You may pass additional get parameters depending on what you need
'max_results': Integer value, default to the 'ES_MAX_RESULTS' env variable. How many results are returned by the query.
'display_fields': Coma-separated list of fields. Default to the 'ES_DISPLAY_FIELDS' env variable. Which fields will be returned by the query
```

You can pass additional exact filters on some fields as get parameters (using field=value), as long as the field is set in the 'ES_ALLOWED_FILTERS' env variable.
17 changes: 12 additions & 5 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ def index():
display_fields = request.args.get('display_fields').split(',')

search_index = current_app.config['ES_INDEX']
es_query = _generate_query(current_app.config, query)

filters = {}
for key, value in request.args.items():
if key not in ['q', 'highlighting', 'display_fields', 'max_results'] and key in current_app.config['ES_ALLOWED_FILTERS']:
filters[key] = value

es_query = _generate_query(current_app.config, query, filters)
highlight = {}
if show_highlight:
highlight_pre = ["<{}>".format(current_app.config['ES_HIGHLIGHT_TAG'])]
Expand All @@ -40,11 +46,14 @@ def index():
return make_response(jsonify({'error': "", 'data': results['hits']['hits']}), 200)


def _generate_query(config, user_query):
def _generate_query(config, user_query, filters):
query = {}
search_field = config['ES_SEARCH_FIELDS']

if config["RESTRICT_PUBLIC"]:
filters['public'] = True

if filters:
query["bool"] = {
"must": [
{
Expand All @@ -54,9 +63,7 @@ def _generate_query(config, user_query):
}
},
{
"term": {
"public": True
}
"term": {**filters}
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ class Config(object):
display_fields = os.environ.get('ES_DISPLAY_FIELDS', "")
ES_DISPLAY_FIELDS = display_fields.split(",") if display_fields else []
es_url = "http://{}:{}".format(ES_URL, ES_PORT)
allowed_filters = os.environ.get('ES_ALLOWED_FILTERS', "")
ES_ALLOWED_FILTERS = allowed_filters.split(",") if display_fields else []
ES_INSTANCE = Elasticsearch(es_url)
ES_HIGHLIGHT_TAG = os.environ.get('ES_HIGHLIGHT_TAG', 'em')

0 comments on commit d134fe4

Please sign in to comment.