From 52ce55b2e9de0eec1cb9ed381d4e3a9e6214de0d Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 3 Aug 2022 07:20:30 +0100 Subject: [PATCH] Implement sentiment search --- core/lib/opensearch.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/core/lib/opensearch.py b/core/lib/opensearch.py index b72c270..db14d7f 100644 --- a/core/lib/opensearch.py +++ b/core/lib/opensearch.py @@ -153,6 +153,7 @@ def query_results(request, size=None): message_class = None add_bool = [] add_top = [] + add_top_negative = [] sort = None if request.user.is_anonymous: sizes = settings.OPENSEARCH_MAIN_SIZES_ANON @@ -209,6 +210,10 @@ def query_results(request, size=None): ] if "check-sentiment" in request.POST: + if "sentiment-method" not in request.POST: + message = "No sentiment method" + message_class = "danger" + return {"message": message, "class": message_class} if "sentiment" in request.POST: sentiment = request.POST["sentiment"] try: @@ -217,6 +222,30 @@ def query_results(request, size=None): message = "Sentiment is not a float" message_class = "danger" return {"message": message, "class": message_class} + sentiment_method = request.POST["sentiment-method"] + range_query_compare = { + "range": { + "sentiment": { + } + } + } + range_query_precise = { + "match": { + "sentiment": None, + } + } + if sentiment_method == "below": + range_query_compare["range"]["sentiment"]["lt"] = sentiment + add_top.append(range_query_compare) + elif sentiment_method == "above": + range_query_compare["range"]["sentiment"]["gt"] = sentiment + add_top.append(range_query_compare) + elif sentiment_method == "exact": + range_query_precise["match"]["sentiment"] = sentiment + add_top.append(range_query_precise) + elif sentiment_method == "nonzero": + range_query_precise["match"]["sentiment"] = 0 + add_top_negative.append(range_query_precise) if "query" in request.POST: query = request.POST["query"] @@ -227,6 +256,12 @@ def query_results(request, size=None): if add_top: for item in add_top: search_query["query"]["bool"]["must"].append(item) + if add_top_negative: + for item in add_top_negative: + if "must_not" in search_query["query"]["bool"]: + search_query["query"]["bool"]["must_not"].append(item) + else: + search_query["query"]["bool"]["must_not"] = [item] if sort: search_query["sort"] = sort results = run_main_query(