Implement a better Drilldown form

This commit is contained in:
Mark Veidemanis 2022-07-21 13:49:11 +01:00
parent 67ea926c96
commit 424f8381d3
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
4 changed files with 145 additions and 139 deletions

View File

@ -21,11 +21,13 @@ def initialise_opensearch():
return client return client
def construct_query(query, fields): def construct_query(query, fields, results):
if not fields: if not fields:
fields = settings.OPENSEARCH_MAIN_SEARCH_FIELDS fields = settings.OPENSEARCH_MAIN_SEARCH_FIELDS
if not results:
results = 5
query = { query = {
"size": 5, "size": results,
"query": { "query": {
"multi_match": { "multi_match": {
"query": query, "query": query,
@ -36,8 +38,8 @@ def construct_query(query, fields):
return query return query
def run_main_query(client, query, fields=None): def run_main_query(client, query, fields=None, results=None):
search_query = construct_query(query, fields) search_query = construct_query(query, fields, results)
# fmt: off # fmt: off
response = client.search(body=search_query, response = client.search(body=search_query,
index=settings.OPENSEARCH_INDEX_MAIN) index=settings.OPENSEARCH_INDEX_MAIN)

View File

@ -2,12 +2,8 @@
{% for plan in plans %} {% for plan in plans %}
{% if plan in user_plans %}
<div class="box has-background-info-light">
{% else %}
<div class="box">
{% endif %}
<div class="box">
<article class="media"> <article class="media">
<div class="media-left"> <div class="media-left">
<figure class="image is-64x64"> <figure class="image is-64x64">

View File

@ -1,93 +1,96 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load static %} {% load static %}
{% block content %} {% block content %}
<div class="block"> <div class="block">
<form method="POST"> <form method="POST">
{% csrf_token %}
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">Search</label>
</div>
<div class="field-body"> <div class="field-body">
<div class="field"> <div class="field">
<p class="control is-expanded has-icons-left"> <div class="control is-expanded has-icons-left">
{% csrf_token %}
<input name="query" class="input" type="text" placeholder="Query"> <input name="query" class="input" type="text" placeholder="Query">
<span class="icon is-small is-left"> <span class="icon is-small is-left">
<i class="fas fa-user"></i> <i class="fas fa-magnifying-glass"></i>
</span> </span>
</p> </div>
</div>
</div>
</div> </div>
<div class="field is-horizontal"> <div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">Timescale</label>
</div>
<div class="field-body"> <div class="field-body">
<div class="field is-narrow"> <div class="field">
<div class="control"> <div class="control is-expanded has-icons-left">
<div class="select is-fullwidth"> <div class="select is-fullwidth">
<select name="fields"> <select name="timescale">
<option value="minute">Minute</option>
<option value="hour">Hour</option>
<option value="day">Day</option>
<option value="week">Week</option>
<option value="month">Month</option>
<option value="6months">6 months</option>
</select>
<span class="icon is-small is-left">
<i class="fas fa-magnifying-glass"></i>
</span>
</div>
</div>
</div>
<div class="field-label is-normal">
<label class="label">Fields</label>
</div>
<div class="field">
<div class="control is-expanded has-icons-left">
<div class="select is-fullwidth is-multiple">
<select multiple name="fields">
{% for field in fields %} {% for field in fields %}
<option>{{ field }}</option> <option value="{{ field }}">{{ field }}</option>
{% endfor %} {% endfor %}
</select> </select>
<span class="icon is-small is-left">
<i class="fas fa-magnifying-glass"></i>
</span>
</div> </div>
</div> </div>
</div> </div>
</div>
</div>
<div class="field-body">
<div class="field is-narrow">
<div class="control">
<div class="select is-fullwidth">
<select>
<option>day</option>
<option>week</option>
<option>month</option>
<option>year</option>
</select>
</div>
</div>
</div>
</div>
</div>
<div class="field is-horizontal">
<div class="field-body">
<div class="field is-horizontal">
<div class="field-label">
<label class="label">Search all fields</label>
</div>
<div class="field-body">
<div class="field is-narrow">
<div class="control">
<label class="radio">
<input type="radio" name="member">
Yes
</label>
<label class="radio">
<input type="radio" name="member">
No
</label>
</div>
</div>
</div>
</div>
<div class="field is-horizontal">
<div class="field-label is-normal"> <div class="field-label is-normal">
<label class="label">Results</label> <label class="label">Results</label>
</div> </div>
<div class="field-body">
<div class="field is-narrow"> <div class="field">
<div class="control"> <div class="control is-expanded has-icons-left">
<div class="select is-fullwidth"> <div class="select is-fullwidth">
<select name="results"> <select name="results">
<option>5</option> <option value="5">5</option>
<option>10</option> <option value="10">10</option>
<option>25</option> <option value="15">15</option>
<option value="20">20</option>
</select> </select>
<span class="icon is-small is-left">
<i class="fas fa-magnifying-glass"></i>
</span>
</div> </div>
</div> </div>
</div> </div>
</div>
</div>
</div>
</div>
<div class="field-body"> </div>
</div>
<div class="field is-horizontal"> <div class="field is-horizontal">
<div class="field-label"> <div class="field-label">
<!-- Left empty for spacing --> <!-- Left empty for spacing -->
@ -95,11 +98,11 @@
<div class="field-body"> <div class="field-body">
<div class="field"> <div class="field">
<div class="control"> <div class="control">
<button class="button is-primary"> <button class="button is-primary">
Send message Search
</button> </button>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>
@ -137,6 +140,7 @@
</tbody> </tbody>
</table> </table>
</div> </div>
{% if results is not None %}
<p>{{ card }} hits</p> <p>{{ card }} hits</p>
{% endif %}
{% endblock %} {% endblock %}

View File

@ -18,20 +18,24 @@ class Drilldown(LoginRequiredMixin, View):
def get(self, request): def get(self, request):
if not request.user.has_plan(self.plan_name): if not request.user.has_plan(self.plan_name):
return render(request, "denied.html") return render(request, "denied.html")
return render(request, self.template_name) context = {"fields": settings.OPENSEARCH_MAIN_SEARCH_FIELDS}
return render(request, self.template_name, context)
def post(self, request): def post(self, request):
if not request.user.has_plan(self.plan_name): if not request.user.has_plan(self.plan_name):
return render(request, "denied.html") return render(request, "denied.html")
fields = None fields = None
if "fields" in request.POST: if "fields" in request.POST:
fields = request.POST["fields"] fields = request.POST.getlist("fields")
print("FIELD", fields) print("FIELD", fields)
if "results" in request.POST:
results = request.POST["results"]
print("RESULTS", results)
if "query" in request.POST: if "query" in request.POST:
query = request.POST["query"] query = request.POST["query"]
# field = results.POST["field"] # field = results.POST["field"]
# print("FIELD ", field) # print("FIELD ", field)
results = run_main_query(client, query, fields) results = run_main_query(client, query, fields, results)
pp.pprint(results) pp.pprint(results)
results_parsed = [] results_parsed = []
if "hits" in results.keys(): if "hits" in results.keys():