diff --git a/core/lib/opensearch.py b/core/lib/opensearch.py index d054bdc..3b4402d 100644 --- a/core/lib/opensearch.py +++ b/core/lib/opensearch.py @@ -127,7 +127,8 @@ def run_main_query(client, user, query, custom_query=False, index=None, size=Non search_query = construct_query(query, size) try: response = client.search(body=search_query, index=index) - except RequestError: + except RequestError as err: + print("OpenSearch error", err) return False filter_blacklisted(user, response) return response @@ -141,6 +142,9 @@ def query_results(request, size=None): results with the other data we have. """ # is_anonymous = isinstance(request.user, AnonymousUser) + message = None + message_class = None + add_bool = [] if request.user.is_anonymous: sizes = settings.OPENSEARCH_MAIN_SIZES_ANON else: @@ -149,14 +153,39 @@ def query_results(request, size=None): if "size" in request.POST: size = request.POST["size"] if size not in sizes: - return False + message = "Size is not permitted" + message_class = "danger" + return {"message": message, "class": message_class} + if "source" in request.POST: + source = request.POST["source"] + if source not in settings.OPENSEARCH_MAIN_SOURCES: + message = "Invalid source" + message_class = "danger" + return {"message": message, "class": message_class} + if source != "all": + add_bool.append({"src": source}) + + if "check-sentiment" in request.POST: + if "sentiment" in request.POST: + sentiment = request.POST["sentiment"] + try: + sentiment = float(sentiment) + except ValueError: + message = "Sentiment is not a float" + message_class = "danger" + return {"message": message, "class": message_class} + if "query" in request.POST: query = request.POST["query"] - + search_query = construct_query(query, size) + if add_bool: + for item in add_bool: + search_query["query"]["bool"]["must"].append({"match": item}) results = run_main_query( client, request.user, # passed through run_main_query to filter_blacklisted - query, + search_query, + custom_query=True, size=size, ) if not results: @@ -208,28 +237,34 @@ def construct_query(query, size): query = { "size": size, "query": { - "query_string": { - "query": query, - # "fields": fields, - # "default_field": "msg", - # "type": "best_fields", - "fuzziness": "AUTO", - "fuzzy_transpositions": True, - "fuzzy_max_expansions": 50, - "fuzzy_prefix_length": 0, - # "minimum_should_match": 1, - "default_operator": "or", - "analyzer": "standard", - "lenient": True, - "boost": 1, - "allow_leading_wildcard": True, - # "enable_position_increments": False, - "phrase_slop": 3, - # "max_determinized_states": 10000, - "quote_field_suffix": "", - "quote_analyzer": "standard", - "analyze_wildcard": False, - "auto_generate_synonyms_phrase_query": True, + "bool": { + "must": [ + { + "query_string": { + "query": query, + # "fields": fields, + # "default_field": "msg", + # "type": "best_fields", + "fuzziness": "AUTO", + "fuzzy_transpositions": True, + "fuzzy_max_expansions": 50, + "fuzzy_prefix_length": 0, + # "minimum_should_match": 1, + "default_operator": "or", + "analyzer": "standard", + "lenient": True, + "boost": 1, + "allow_leading_wildcard": True, + # "enable_position_increments": False, + "phrase_slop": 3, + # "max_determinized_states": 10000, + "quote_field_suffix": "", + "quote_analyzer": "standard", + "analyze_wildcard": False, + "auto_generate_synonyms_phrase_query": True, + } + } + ] } }, "sort": [ diff --git a/core/templates/partials/notify.html b/core/templates/partials/notify.html new file mode 100644 index 0000000..80a3852 --- /dev/null +++ b/core/templates/partials/notify.html @@ -0,0 +1,5 @@ +{% if message is not None %} +