Implement sentiment search

This commit is contained in:
Mark Veidemanis 2022-08-03 07:20:30 +01:00
parent b95aaf8ff2
commit 52ce55b2e9
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 35 additions and 0 deletions

View File

@ -153,6 +153,7 @@ def query_results(request, size=None):
message_class = None message_class = None
add_bool = [] add_bool = []
add_top = [] add_top = []
add_top_negative = []
sort = None sort = None
if request.user.is_anonymous: if request.user.is_anonymous:
sizes = settings.OPENSEARCH_MAIN_SIZES_ANON sizes = settings.OPENSEARCH_MAIN_SIZES_ANON
@ -209,6 +210,10 @@ def query_results(request, size=None):
] ]
if "check-sentiment" in request.POST: 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: if "sentiment" in request.POST:
sentiment = request.POST["sentiment"] sentiment = request.POST["sentiment"]
try: try:
@ -217,6 +222,30 @@ def query_results(request, size=None):
message = "Sentiment is not a float" message = "Sentiment is not a float"
message_class = "danger" message_class = "danger"
return {"message": message, "class": message_class} 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: if "query" in request.POST:
query = request.POST["query"] query = request.POST["query"]
@ -227,6 +256,12 @@ def query_results(request, size=None):
if add_top: if add_top:
for item in add_top: for item in add_top:
search_query["query"]["bool"]["must"].append(item) 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: if sort:
search_query["sort"] = sort search_query["sort"] = sort
results = run_main_query( results = run_main_query(