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

search: Faceted filters #29

Merged
merged 1 commit into from
Jun 19, 2019
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
41 changes: 39 additions & 2 deletions sonar/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def _(x):
BABEL_DEFAULT_TIMEZONE = 'Europe/Zurich'
#: Other supported languages (do not include the default language in list).
I18N_LANGUAGES = [
('fr', _('French'))
('fr', _('French')),
('de', _('German'))
]

# Base templates
Expand Down Expand Up @@ -179,6 +180,7 @@ def _(x):
SEARCH_UI_JSTEMPLATE_SELECT_BOX = 'templates/documents/search_ui/'\
'select_box.html'
SEARCH_UI_JSTEMPLATE_LOADING = 'templates/documents/search_ui/loading.html'
SEARCH_UI_JSTEMPLATE_FACETS = 'templates/documents/search_ui/facets.html'

SECURITY_LOGIN_USER_TEMPLATE = 'sonar/accounts/login.html'
SECURITY_FORGOT_PASSWORD_TEMPLATE = 'sonar/accounts/forgot_password.html'
Expand Down Expand Up @@ -265,13 +267,48 @@ def _(x):

RECORDS_REST_FACETS = {
'documents': dict(
aggs=dict(
institution=dict(terms=dict(field='institution.pid')),
language=dict(terms=dict(field='languages.language')),
author=dict(terms=dict(field='facet_authors')),
subject=dict(terms=dict(field='facet_subjects'))
),
filters={
_('institution'): terms_filter('institution.pid')
_('institution'): terms_filter('institution.pid'),
_('language'): terms_filter('languages.language'),
_('author'): terms_filter('facet_authors'),
_('subject'): terms_filter('facet_subjects'),
}
)
}
"""REST search facets."""

RECORDS_REST_SORT_OPTIONS = dict(
documents=dict(
bestmatch=dict(
title=_('Best match'),
fields=['_score'],
default_order='desc',
order=2,
),
mostrecent=dict(
title=_('Most recent'),
fields=['-_created'],
default_order='asc',
order=1,
),
)
)
"""Setup sorting options."""

RECORDS_REST_DEFAULT_SORT = dict(
documents=dict(
query='bestmatch',
noquery='mostrecent',
),
)
"""Set default sorting options."""

SONAR_ENDPOINTS_ENABLED = True
"""Enable/disable automatic endpoint registration."""

Expand Down
3 changes: 1 addition & 2 deletions sonar/modules/documents/dojson/contrib/marc21tojson/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,11 @@ def marc21_to_is_part_of(self, key, value):


@marc21tojson.over('subjects', '^6....')
@utils.for_each_value
@utils.ignore_value
def marc21_to_subjects(self, key, value):
"""Get subjects.

subjects: 6xx [duplicates could exist between several vocabularies,
if possible deduplicate]
"""
return value.get('a')
return value.get('a').split(' ; ')
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"type": "text",
"copy_to": "facet_subjects"
},
"facet_subjects": {
"type": "keyword"
},
"identifiers": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -50,10 +53,14 @@
"type": "object",
"properties": {
"name": {
"type": "text"
"type": "text",
"copy_to": "facet_authors"
}
}
},
"facet_authors": {
"type": "keyword"
},
"extent": {
"type": "keyword"
},
Expand All @@ -67,7 +74,7 @@
"type": "keyword"
},
"name": {
"type": "text"
"type": "keyword"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<div ng-init="tree=[];tree_more=[]" ng-repeat="aggr in orderedAggs track by $index">
<div ng-if="aggr.value.buckets.length > 0 || getValues(aggr.key)" class="card bg-light mb-2">
<div class="card-body">
<h5 class="card-title">{{ aggr.key }}</h5>
<ul class="list-unstyled my-1" ng-init="values = getValues(aggr.key)" ng-repeat="item in aggr.value.buckets">
<li>
<div class="form-check">
<input type="checkbox" class="form-check-input" ng-checked="values.indexOf(item.key) > -1" ng-click="handleClick(aggr.key, item.key)" />
<label class="form-check-label">
{{ item.key }} ({{ item.doc_count }})
<small>
<a
ng-init="tree_more[item.key] = false; tree[item.key] = (values.indexOf(item.key) > -1) ? true : false"
ng-model="tree[item.key]"
ng-show="tree_more[item.key]"
ng-click="tree[item.key]=!tree[item.key]"
>{{ (tree[item.key]) ? '-' : '+' }}
</a>
</small>
</label>
</div>
<div ng-show="tree[item.key] === true" ng-repeat="(subKey, subValue) in item">
<div ng-if="['doc_count', 'key'].indexOf(subKey) === -1">
<ul ng-init="tree_more[item.key] = true; subValues = getValues(subKey)" ng-repeat="subFacets in subValue.buckets">
<li>
<input
ng-init="tree[item.key] = (subValues.indexOf(subFacets.key) > -1 || tree[item.key])"
type="checkbox"
ng-checked="subValues.indexOf(subFacets.key) > -1"
ng-click="handleClick(subKey, subFacets.key)" /> {{ subFacets.key }} ({{ subFacets.doc_count }})
</li>
</ul>
</div>
</div>
</li>
</ul>
<!-- Show previously selected facets with zero results at the bottom -->
<ul class="list-unstyled" ng-repeat="selectedValue in getValues(aggr.key)">
<li ng-if="(aggr.value.buckets | filter:{key:selectedValue}:true).length == 0">
<input type="checkbox" checked ng-click="handleClick(aggr.key, selectedValue)" /> {{ selectedValue }} (0)
</li>
</ul>
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion sonar/theme/templates/sonar/partial/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<img src="{{ url_for('static', filename='images/' ~ g.ir|default('sonar') ~ '-logo.svg')}}" height="50" class="d-inline-block align-top mr-3 my-4" alt="">
</a>
<form action="{{ url_for('documents.search', ir=g.ir|default('sonar')) }}" class="form-inline my-2 my-lg-0 ml-lg-5">
<input name="q" class="form-control mr-sm-2" type="search" placeholder="{{_('Search')}}" aria-label="{{_('Search')}}">
<input name="q" class="form-control mr-sm-2" type="search" placeholder="{{_('Search')}}" aria-label="{{_('Search')}}" value="{{ request.args.get('q', '') }}">
<button class="btn btn-outline-light my-2 my-sm-0" type="submit">
<i class="fa fa-search"></i>
</button>
Expand Down
52 changes: 28 additions & 24 deletions sonar/theme/templates/sonar/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
{%- if sort_options %}
<div class="row align-items-center">
<div class="col d-none d-sm-block text-right">
Sort by:
{{ _('Sort by') }}
</div>
<div class="col">
{%- block search_sort_select scoped %}
Expand All @@ -68,28 +68,32 @@
{%- endblock search_sort %}
</div>
</div>
<div class="mt-5">
<invenio-search-loading
template="{{ url_for('static', filename=config.SEARCH_UI_JSTEMPLATE_LOADING) }}"
message="{{ _('Loading...') }}">
</invenio-search-loading>
<invenio-search-results
template="{{ url_for('static', filename=config.SEARCH_UI_JSTEMPLATE_RESULTS) }}">
</invenio-search-results>
<invenio-search-error
template="{{ url_for('static', filename=config.SEARCH_UI_JSTEMPLATE_ERROR) }}"
message="{{ _('Search failed.') }}">
</invenio-search-error>
<invenio-search-range
options='{{ config.SEARCH_UI_JSTEMPLATE_RANGE_OPTIONS | tojson }}'
template="{{ url_for('static', filename=config.SEARCH_UI_JSTEMPLATE_RANGE) }}">
</invenio-search-range>
<invenio-search-facets
template="{{ url_for('static', filename=config.SEARCH_UI_JSTEMPLATE_FACETS) }}">
</invenio-search-facets>
<invenio-search-pagination
template="{{ url_for('static', filename=config.SEARCH_UI_JSTEMPLATE_PAGINATION) }}"
>
</invenio-search-pagination>
<div class="row mt-5">
<div class="col-md-3">
<invenio-search-facets
template="{{ url_for('static', filename=config.SEARCH_UI_JSTEMPLATE_FACETS) }}">
</invenio-search-facets>
</div>
<div class="col-md-9">
<invenio-search-loading
template="{{ url_for('static', filename=config.SEARCH_UI_JSTEMPLATE_LOADING) }}"
message="{{ _('Loading...') }}">
</invenio-search-loading>
<invenio-search-results
template="{{ url_for('static', filename=config.SEARCH_UI_JSTEMPLATE_RESULTS) }}">
</invenio-search-results>
<invenio-search-error
template="{{ url_for('static', filename=config.SEARCH_UI_JSTEMPLATE_ERROR) }}"
message="{{ _('Search failed.') }}">
</invenio-search-error>
<invenio-search-range
options='{{ config.SEARCH_UI_JSTEMPLATE_RANGE_OPTIONS | tojson }}'
template="{{ url_for('static', filename=config.SEARCH_UI_JSTEMPLATE_RANGE) }}">
</invenio-search-range>
<invenio-search-pagination
template="{{ url_for('static', filename=config.SEARCH_UI_JSTEMPLATE_PAGINATION) }}"
>
</invenio-search-pagination>
</div>
</div>
{%- endblock page_body -%}
Loading