T52710 search on id works

This commit is contained in:
2017-12-08 13:12:39 +01:00
parent b6a93452cd
commit 533544117b
4 changed files with 67 additions and 41 deletions

View File

@@ -115,7 +115,11 @@ def create_doc_from_user_data(user_to_index):
def create_doc_from_node_data(node_to_index):
# node stuff
doc_id = str(node_to_index['objectID'])
doc_id = str(node_to_index.get('objectID', ''))
if not doc_id:
log.error('ID missing %s', node_to_index)
return
doc = Node(_id=doc_id)

View File

@@ -21,32 +21,23 @@ def push_updated_user(user_to_index: dict):
Push an update to the Elastic index when
a user item is updated.
"""
log.warning(
'WIP USER ELK INDEXING %s %s',
user_to_index.get('username'),
user_to_index.get('objectID'))
doc = documents.create_doc_from_user_data(user_to_index)
log.debug('UPDATE USER %s', doc._id)
doc.save()
log.warning('CREATED ELK USER DOC')
def index_node_save(node_to_index: dict):
log.warning(
'ELK NODE INDEXING %s',
node_to_index.get('objectID'))
log.warning(node_to_index)
if not node_to_index:
return
doc = documents.create_doc_from_node_data(node_to_index)
log.warning('CREATED ELK NODE DOC')
log.debug('CREATED ELK NODE DOC %s', doc._id)
doc.save()
def index_node_delete(delete_id: str):
log.warning('NODE DELETE INDEXING %s', delete_id)
log.debug('NODE DELETE INDEXING %s', delete_id)
documents.Node(id=delete_id).delete()

View File

@@ -22,29 +22,26 @@ def add_aggs_to_search(search, agg_terms):
search.aggs.bucket(term, 'terms', field=term)
def make_must(terms):
def make_must(must: list, terms: dict) -> list:
"""
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):
def nested_bool(must: list, should: list, terms: dict) -> Search:
"""
Create a nested bool, where the aggregation
selection is a must
"""
must = []
must = make_must(terms)
must = make_must(must, terms)
bool_query = Q('bool', should=should)
must.append(bool_query)
bool_query = Q('bool', must=must)
@@ -70,13 +67,14 @@ def do_search(query: str, terms: dict) -> dict:
Q('term', tags=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='node')
must = [
Q('term', _type='node')
]
if not query:
should = []
search = nested_bool(must, should, terms)
add_aggs_to_search(search, node_agg_terms)
if current_app.config['DEBUG']:
@@ -99,13 +97,18 @@ def do_user_search(query: str, terms: dict) -> dict:
Q('match', full_name=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')
must = [
Q('term', _type='user')
]
# We got an id field. we MUST find it.
if len(query) == len('563aca02c379cf0005e8e17d'):
must.append(Q('term', _id=query))
if not query:
should = []
search = nested_bool(must, should, terms)
add_aggs_to_search(search, user_agg_terms)
if current_app.config['DEBUG']: