Search: include explicit indices to search
This makes it possible to search for 'everything' without explicitly stating the document type.
This commit is contained in:
parent
b6f7958dfe
commit
33c051bf28
@ -4,6 +4,8 @@ import logging
|
|||||||
from elasticsearch import Elasticsearch
|
from elasticsearch import Elasticsearch
|
||||||
from elasticsearch_dsl import Search, Q
|
from elasticsearch_dsl import Search, Q
|
||||||
|
|
||||||
|
from pillar import current_app
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
NODE_AGG_TERMS = ['node_type', 'media', 'tags', 'is_free']
|
NODE_AGG_TERMS = ['node_type', 'media', 'tags', 'is_free']
|
||||||
@ -32,17 +34,19 @@ def make_must(must: list, terms: dict) -> list:
|
|||||||
return must
|
return must
|
||||||
|
|
||||||
|
|
||||||
def nested_bool(must: list, should: list, terms: dict) -> Search:
|
def nested_bool(must: list, should: list, terms: dict, *, index_alias: str) -> Search:
|
||||||
"""
|
"""
|
||||||
Create a nested bool, where the aggregation
|
Create a nested bool, where the aggregation selection is a must.
|
||||||
selection is a must
|
|
||||||
|
:param index_alias: 'USER' or 'NODE', see ELASTIC_INDICES config.
|
||||||
"""
|
"""
|
||||||
must = make_must(must, terms)
|
must = make_must(must, terms)
|
||||||
bool_query = Q('bool', should=should)
|
bool_query = Q('bool', should=should)
|
||||||
must.append(bool_query)
|
must.append(bool_query)
|
||||||
bool_query = Q('bool', must=must)
|
bool_query = Q('bool', must=must)
|
||||||
|
|
||||||
search = Search(using=client)
|
index = current_app.config['ELASTIC_INDICES'][index_alias]
|
||||||
|
search = Search(using=client, index=index)
|
||||||
search.query = bool_query
|
search.query = bool_query
|
||||||
|
|
||||||
return search
|
return search
|
||||||
@ -72,7 +76,7 @@ def do_node_search(query: str, terms: dict) -> dict:
|
|||||||
if not query:
|
if not query:
|
||||||
should = []
|
should = []
|
||||||
|
|
||||||
search = nested_bool(must, should, terms)
|
search = nested_bool(must, should, terms, index_alias='NODE')
|
||||||
add_aggs_to_search(search, NODE_AGG_TERMS)
|
add_aggs_to_search(search, NODE_AGG_TERMS)
|
||||||
|
|
||||||
if log.isEnabledFor(logging.DEBUG):
|
if log.isEnabledFor(logging.DEBUG):
|
||||||
@ -89,20 +93,21 @@ def do_node_search(query: str, terms: dict) -> dict:
|
|||||||
|
|
||||||
def do_user_search(query: str, terms: dict) -> dict:
|
def do_user_search(query: str, terms: dict) -> dict:
|
||||||
""" return user objects represented in elasicsearch result dict"""
|
""" return user objects represented in elasicsearch result dict"""
|
||||||
|
|
||||||
|
if query:
|
||||||
should = [
|
should = [
|
||||||
Q('match', username=query),
|
Q('match', username=query),
|
||||||
Q('match', full_name=query),
|
Q('match', full_name=query),
|
||||||
Q('match', email=query),
|
Q('match', email=query),
|
||||||
]
|
]
|
||||||
|
else:
|
||||||
|
should = []
|
||||||
|
|
||||||
must = [
|
must = [
|
||||||
Q('term', _type='user')
|
Q('term', _type='user')
|
||||||
]
|
]
|
||||||
|
|
||||||
if not query:
|
search = nested_bool(must, should, terms, index_alias='USER')
|
||||||
should = []
|
|
||||||
|
|
||||||
search = nested_bool(must, should, terms)
|
|
||||||
add_aggs_to_search(search, USER_AGG_TERMS)
|
add_aggs_to_search(search, USER_AGG_TERMS)
|
||||||
|
|
||||||
if log.isEnabledFor(logging.DEBUG):
|
if log.isEnabledFor(logging.DEBUG):
|
||||||
@ -121,6 +126,8 @@ def do_user_search_admin(query: str) -> dict:
|
|||||||
return users seach result dict object
|
return users seach result dict object
|
||||||
search all user fields and provide aggregation information
|
search all user fields and provide aggregation information
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if query:
|
||||||
should = [
|
should = [
|
||||||
Q('match', username=query),
|
Q('match', username=query),
|
||||||
Q('match', email=query),
|
Q('match', email=query),
|
||||||
@ -135,8 +142,10 @@ def do_user_search_admin(query: str) -> dict:
|
|||||||
'boost': 100, # how much more it counts for the score
|
'boost': 100, # how much more it counts for the score
|
||||||
}
|
}
|
||||||
}})
|
}})
|
||||||
|
else:
|
||||||
|
should = []
|
||||||
|
|
||||||
search = Search(using=client)
|
search = Search(using=client, index=current_app.config['ELASTIC_INDICES']['USER'])
|
||||||
search.query = Q('bool', should=should)
|
search.query = Q('bool', should=should)
|
||||||
add_aggs_to_search(search, USER_AGG_TERMS)
|
add_aggs_to_search(search, USER_AGG_TERMS)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user