Fix some Manticore queries
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
from re import search
|
||||
from django.conf import settings
|
||||
from core.lib.opensearch import annotate_results, filter_blacklisted, parse_results
|
||||
import manticoresearch
|
||||
from core.views.helpers import dedup_list
|
||||
from datetime import datetime
|
||||
from pprint import pprint
|
||||
|
||||
import manticoresearch
|
||||
from django.conf import settings
|
||||
|
||||
from core.lib.processing import annotate_results, filter_blacklisted, parse_results
|
||||
from core.views.helpers import dedup_list
|
||||
|
||||
|
||||
def initialise_manticore():
|
||||
"""
|
||||
Initialise the Manticore client
|
||||
@@ -15,8 +18,10 @@ def initialise_manticore():
|
||||
|
||||
return (api_client, api_instance)
|
||||
|
||||
|
||||
api_client, client = initialise_manticore()
|
||||
|
||||
|
||||
def construct_query(query, size, index, blank=False):
|
||||
"""
|
||||
Accept some query parameters and construct an OpenSearch query.
|
||||
@@ -35,12 +40,14 @@ def construct_query(query, size, index, blank=False):
|
||||
query_base["query"]["bool"]["must"].append(query_string)
|
||||
return query_base
|
||||
|
||||
|
||||
def run_query(client, user, search_query):
|
||||
response = client.search(search_query)
|
||||
response = response.to_dict()
|
||||
filter_blacklisted(user, response)
|
||||
return response
|
||||
|
||||
|
||||
def query_results(
|
||||
request,
|
||||
query_params,
|
||||
@@ -110,6 +117,9 @@ def query_results(
|
||||
query = query_params["query"]
|
||||
search_query = construct_query(query, size, index)
|
||||
query_created = True
|
||||
else:
|
||||
if custom_query:
|
||||
search_query = custom_query
|
||||
|
||||
if tags:
|
||||
# Get a blank search query
|
||||
@@ -159,13 +169,16 @@ def query_results(
|
||||
add_top.append(add_top_tmp)
|
||||
print("AFTER", add_top)
|
||||
|
||||
|
||||
# Date/time range
|
||||
if set({"from_date", "to_date", "from_time", "to_time"}).issubset(
|
||||
query_params.keys()
|
||||
):
|
||||
from_ts = f"{query_params['from_date']}T{query_params['from_time']}Z"
|
||||
to_ts = f"{query_params['to_date']}T{query_params['to_time']}Z"
|
||||
from_ts = datetime.strptime(from_ts, "%Y-%m-%dT%H:%MZ")
|
||||
to_ts = datetime.strptime(to_ts, "%Y-%m-%dT%H:%MZ")
|
||||
from_ts = int(from_ts.timestamp())
|
||||
to_ts = int(to_ts.timestamp())
|
||||
range_query = {
|
||||
"range": {
|
||||
"ts": {
|
||||
@@ -247,7 +260,6 @@ def query_results(
|
||||
if sort:
|
||||
search_query["sort"] = sort
|
||||
|
||||
|
||||
pprint(search_query)
|
||||
results = run_query(
|
||||
client,
|
||||
@@ -256,7 +268,7 @@ def query_results(
|
||||
)
|
||||
if not results:
|
||||
return False
|
||||
#results = results.to_dict()
|
||||
# results = results.to_dict()
|
||||
results_parsed = parse_results(results)
|
||||
if annotate:
|
||||
annotate_results(results_parsed)
|
||||
@@ -280,4 +292,5 @@ def query_results(
|
||||
"card": results["hits"]["total"],
|
||||
"took": results["took"],
|
||||
}
|
||||
return context
|
||||
print("RTRN", context)
|
||||
return context
|
||||
|
||||
Reference in New Issue
Block a user