Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

177 enhance search #259

Merged
merged 5 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions series_tiempo_ar_api/apps/metadata/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@
PARAM_LIMIT = 'limit'
PARAM_OFFSET = 'offset'
PARAM_QUERYSTRING = 'q'

FILTER_ARGS = {
# Pares nombre_arg: field del documento en elasticsearch
'dataset_theme': 'dataset_theme',
'units': 'units',
'dataset_publisher_name': 'dataset_publisher_name',
'dataset_source': 'dataset_source_keyword'
}
2 changes: 1 addition & 1 deletion series_tiempo_ar_api/apps/metadata/indexer/doc_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Field(DocType):
id = Keyword()
dataset_title = Text()
dataset_description = Text()
theme_description = Text()
dataset_theme = Keyword()
units = Keyword()
dataset_publisher_name = Keyword()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def scrap_datajson(self):
dataset_source_keyword=dataset.get('source'),
dataset_description=dataset.get('description'),
dataset_publisher_name=dataset.get('publisher', {}).get('name'),
theme_description=themes.get(dataset.get('theme', [None])[0])
dataset_theme=themes.get(dataset.get('theme', [None])[0])
)
actions.append(doc.to_dict(include_meta=True))
return actions
Expand All @@ -72,6 +72,6 @@ def get_dataset(self, identifier):
def get_themes(theme_taxonomy):
themes = {}
for theme in theme_taxonomy:
themes[theme['id']] = theme['description']
themes[theme['id']] = theme['label']

return themes
14 changes: 14 additions & 0 deletions series_tiempo_ar_api/apps/metadata/queries/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def execute(self):
search = Field.search(using=es_client).query('match', _all=querystring)
search = search[offset:limit + offset]

for arg, field in constants.FILTER_ARGS.iteritems():
search = self.add_filters(search, arg, field)

hits = search.execute()
self.response = {
'data': [],
Expand All @@ -80,3 +83,14 @@ def execute(self):

def append_error(self, msg):
self.errors.append({'error': msg})

def add_filters(self, search, arg_name, field_name):
"""Agrega filtro por field_name al objeto search de Elasticsearch,
obtenido desde el campo arg_name de la query.
"""
units = self.args.get(arg_name)
if units:
units = units.split(',')
search = search.filter('terms', **{field_name: units})

return search
17 changes: 17 additions & 0 deletions series_tiempo_ar_api/apps/metadata/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ def test_response_params(self):
self.assertEqual(result['limit'], int(limit))
self.assertEqual(result['offset'], int(offset))

def test_add_filter(self):
q = FieldSearchQuery(args={'units': 'unit_test'})
search = q.add_filters(Search(), 'units', 'units').to_dict()

# Esperado: un filter agregado
filters = search['query']['bool']['filter']
self.assertEqual(len(filters), 1)
self.assertEqual(filters[0]['terms']['units'], ['unit_test'])

def test_add_filter_no_param(self):
q = FieldSearchQuery(args={})
search = Search()
prev_dict = search.to_dict()
search = q.add_filters(search, 'units', 'units').to_dict()
# Esperado: no se modifica la query si no hay parámetros
self.assertEqual(prev_dict, search)


class IndexerTests(TestCase):

Expand Down
3 changes: 2 additions & 1 deletion series_tiempo_ar_api/apps/metadata/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!coding=utf8
from django.conf.urls import url

from .views import search, dataset_source, field_units, dataset_publisher_name
from .views import search, dataset_source, field_units, dataset_publisher_name, dataset_theme

urlpatterns = [
url('^$', search, name='search'),
url('^dataset_source$', dataset_source, name='dataset_source'),
url('^field_units$', field_units, name='field_units'),
url('^dataset_publisher_name', dataset_publisher_name, name='dataset_publisher_name'),
url('^dataset_theme$', dataset_theme, name='dataset_theme'),
]
6 changes: 6 additions & 0 deletions series_tiempo_ar_api/apps/metadata/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ def dataset_publisher_name(request):
response = query_field_terms(field='dataset_publisher_name')

return JsonResponse(response)


def dataset_theme(request):
response = query_field_terms(field='dataset_theme')

return JsonResponse(response)