search is completely working in frontend now

This commit is contained in:
2017-12-01 16:32:57 +01:00
parent b0d6f724ef
commit 1cba014948
6 changed files with 105 additions and 58 deletions

View File

@@ -2,33 +2,57 @@ import logging
import json
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q
from elasticsearch_dsl.connections import connections
from pillar import current_app
#elk_hosts = current_app.config['ELASTIC_SEARCH_HOSTS']
#
#connections.create_connection(
# hosts=elk_hosts,
# sniff_on_start=True,
# timeout=20)
#
client = Elasticsearch()
log = logging.getLogger(__name__)
node_agg_terms = ['node_type', 'media', 'tags', 'is_free']
user_agg_terms = ['roles', ]
def add_aggs_to_search(search):
def add_aggs_to_search(search, agg_terms):
"""
Add facets / aggregations to the search result
"""
agg_terms = ['node_type', 'media', 'tags', 'is_free']
for term in agg_terms:
search.aggs.bucket(term, 'terms', field=term)
#search.aggs.bucket('project', 'terms', field='project.name')
def make_must(terms):
"""
Given some term parameters
we must match those
"""
must = []
for field, value in terms.items():
print(field, value)
if value:
must.append({'match': {field: value}})
return must
def nested_bool(should, terms):
"""
"""
must = []
must = make_must(terms)
bool_query = Q('bool', should=should)
must.append(bool_query)
bool_query = Q('bool', must=must)
search = Search(using=client)
search.query = bool_query
return search
def do_search(query: str, terms: dict) -> dict:
@@ -46,16 +70,14 @@ def do_search(query: str, terms: dict) -> dict:
Q('term', tags=query),
]
#must = []
if query:
search = nested_bool(should, terms)
else:
# do a match all for the aggregations
search = Search(using=client)
search.query = Q('term', _type='node')
#for field, value in terms.items():
# must.append(
bool_query = Q('bool', should=should)
search = Search(using=client)
search.query = bool_query
add_aggs_to_search(search)
add_aggs_to_search(search, node_agg_terms)
if current_app.config['DEBUG']:
print(json.dumps(search.to_dict(), indent=4))
@@ -68,7 +90,7 @@ def do_search(query: str, terms: dict) -> dict:
return response.to_dict()
def do_user_search(query: str) -> dict:
def do_user_search(query: str, terms: dict) -> dict:
"""
return user objects
"""
@@ -76,17 +98,23 @@ def do_user_search(query: str) -> dict:
Q('match', username=query),
Q('match', full_name=query),
]
bool_query = Q('bool', should=should)
search = Search(using=client)
search.query = bool_query
if query:
search = nested_bool(should, terms)
else:
# do a match all for the aggregations
search = Search(using=client)
search.query = Q('term', _type='user')
add_aggs_to_search(search, user_agg_terms)
if current_app.config['DEBUG']:
log.debug(json.dumps(search.to_dict(), indent=4))
print(json.dumps(search.to_dict(), indent=4))
response = search.execute()
if current_app.config['DEBUG']:
log.debug('%s', json.dumps(response.to_dict(), indent=4))
print(json.dumps(response.to_dict(), indent=4))
return response.to_dict()

View File

@@ -1,22 +1,15 @@
import json
import logging
from bson import ObjectId
from flask import Blueprint, request, current_app, make_response, url_for
from flask import Response
from flask import Blueprint, request
from werkzeug import exceptions as wz_exceptions
from pillar.api.utils import authorization, jsonify
from pillar.api.utils import authorization, jsonify, str2id
from pillar.api.utils import mongo
from pillar.api.utils.authorization import require_login, check_permissions
from pillar.auth import current_user
from . import queries
log = logging.getLogger(__name__)
blueprint_search = Blueprint('elksearch', __name__)
from . import queries
def _valid_search() -> str:
@@ -25,19 +18,23 @@ def _valid_search() -> str:
"""
searchword = request.args.get('q', '')
if not searchword:
raise wz_exceptions.BadRequest('You are forgetting a "?q=whatareyoulookingfor"')
# if not searchword:
# raise wz_exceptions.BadRequest(
# 'You are forgetting a "?q=whatareyoulookingfor"')
return searchword
def _term_filters() -> dict:
"""
Check if frontent want to filter stuff
Check if frontent wants to filter stuff
on specific fields AKA facets
"""
terms = [
'node_type', 'media',
'tags', 'is_free', 'projectname']
'tags', 'is_free', 'projectname',
'roles',
]
parsed_terms = {}
@@ -60,7 +57,8 @@ def search_user():
searchword = _valid_search()
data = queries.do_user_search(searchword)
terms = _term_filters()
data = queries.do_user_search(searchword, terms)
return jsonify(data)