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

Allow index and use as facet new fields #411

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,15 @@ pages.
Fields you exclude will not be shown to the end user, and will not
be accepted when editing or updating this type of dataset.

#### Index dataset Fields

New schema fields that should be indexed by Solr for searching should be marked
with `index_search: true`.

#### Facet this field

New schema fields that should be faceted in the search results should be marked
with `facet_field: true`.

## Group / Organization Schema Keys

Expand Down
60 changes: 60 additions & 0 deletions ckanext/scheming/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ class SchemingDatasetsPlugin(p.SingletonPlugin, DefaultDatasetForm,
p.implements(p.IDatasetForm, inherit=True)
p.implements(p.IActions)
p.implements(p.IValidators)
p.implements(p.IFacets, inherit=True)
p.implements(p.IPackageController, inherit=True)

SCHEMA_OPTION = 'scheming.dataset_schemas'
FALLBACK_OPTION = 'scheming.dataset_fallback'
Expand Down Expand Up @@ -374,6 +376,64 @@ def prepare_dataset_blueprint(self, package_type, bp):
)
return bp

def dataset_facets(self, facets_dict, package_type):
schemas = self._expanded_schemas
dataset_fields = schemas.get(package_type, {}).get('dataset_fields')
for field in dataset_fields:
if not field.get('facet_field', False):
continue
# Add this label to facet
field_name = field['field_name']
facet_field_name = 'extras_{}'.format(field_name)
facets_dict[facet_field_name] = field.get('label', field_name)

return facets_dict

def before_dataset_index(self, data_dict):
schemas = self._expanded_schemas
if data_dict['type'] not in schemas:
return data_dict

package_type = data_dict['type']
dataset_fields = schemas.get(package_type, {}).get('dataset_fields')
for field in dataset_fields:
if field['field_name'] not in data_dict:
continue
if not field.get('index_search', False):
continue
# index the field as extras_*
field_name = field['field_name']
extras_field_name = 'extras_{}'.format(field_name)
value = data_dict.get(field_name)
data_dict[extras_field_name] = self.get_values_to_index(field, value)

return data_dict

def get_values_to_index(self, schema_field, value):
""" Prepare to index a single field value """
if isinstance(value, list):
values = value
elif isinstance(value, str):
# Scheming is not clear on how to handle boolean values
if value.lower() in ['true', 'false']:
values = [p.toolkit.asbool(value)]
else:
try:
values = json.loads(value)
except ValueError:
values = [value]
else:
# TODO check if we need to handle other types
values = [value]
out = []
# Allow fields with choices_helper
choices = helpers.scheming_field_choices(schema_field)
for item in values:
for choice in choices:
if choice['value'] == item:
out.append(choice['label'])
return out


def expand_form_composite(data, fieldnames):
"""
Expand Down
Loading