Implement choosing source in search form

This commit is contained in:
Mark Veidemanis 2022-08-03 23:26:22 +01:00
parent fc86aae119
commit 75e9831d1c
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
6 changed files with 82 additions and 33 deletions

View File

@ -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": [

View File

@ -0,0 +1,5 @@
{% if message is not None %}
<div class="notification is-{{ class }}" hx-ext="remove-me" remove-me="3s">
{{ message }}
</div>
{% endif %}

View File

@ -65,6 +65,7 @@
<div class="control has-icons-left">
<span class="select">
<select name="source">
<option selected value="all">All</option>
<option value="irc">IRC</option>
<option value="dis">Discord</option>
</select>
@ -81,9 +82,9 @@
</div>
</div>
<div class="column">
<div class="field has-addons">
<div class="field has-addons" id="sentiment">
<div class="control">
<input id="sliderWithValue" class="slider has-output-tooltip is-fullwidth" min="-1" max="1" value="0" step="0.05" type="range">
<input disabled="undefined" name="sentiment" id="sliderWithValue" class="slider has-output-tooltip is-fullwidth" min="-1" max="1" value="0" step="0.05" type="range">
<output for="sliderWithValue" class="slider-output">0</output>
<script>bulmaSlider.attach();</script>
</div>
@ -93,15 +94,20 @@
</a>
</p>
</div>
<label class="checkbox">
<input type="checkbox" name="check-sentiment"
_="on click toggle @disabled on #sliderWithValue">
Check sentiment
</label>
</div>
</div>
<div class="is-hidden"></div>
</form>
</div>
<div class="block">
<div id="results">
<div id="results">
</div>
</div>
</div>
<div id="modals-here">
</div>
{% endblock %}

View File

@ -1,6 +1,6 @@
{% load static %}
{% load index %}
{% include 'partials/notify.html' %}
{% if results is not None %}
<div style="display: none" id="jsonData" data-json="{{ data }}">
</div>

View File

@ -43,8 +43,9 @@ class DrilldownSearch(View):
# return HttpResponseForbidden()
context = query_results(request)
if not context:
return HttpResponseForbidden()
if "message" in context:
return render(request, self.template_name, context)
context["data"] = json.dumps(
[
{

View File

@ -79,6 +79,8 @@ class InsightsNicks(LoginRequiredMixin, APIView):
return HttpResponse("No nick")
net = request.data["net"]
nick = request.data["nick"]
if not nick:
return HttpResponseForbidden()
nicks = get_nicks(request, net, [nick])
# Filter Guest
nicks = [x for x in nicks if not x.startswith("Guest")]