[ckan-dev] Custom Search Facets

Renato Guevara guevara.renato at gmail.com
Wed Oct 29 13:38:25 UTC 2014


Adrià,

I have the following method in my plugin that adds a custom field:

def _modify_package_schema(self, schema):

                schema.update({
                        'queryLanguage':
[tk.get_validator('ignore_missing'),

tk.get_converter('convert_to_tags')('queryLanguages')]
                })

                return schema

In another plugin there is an area where some facet key/value pairs are
being added to the facets array (facet_dict), I have the following snippet:

                 facets = {'vocab_queryLanguage': 'Query Language', 'tags':
'Keywords', 'res_format': ('File Format')}

The facet still didn't work.  So, I realized that this
"vocab_queryLanguage" really refers to the vocabulary name which is
"queryLanguages".  I changed "vocab_queryLanguage" to
"vocab_queryLanguages" and I was able to successfully see a faceted search
work.  Thank you for the help!

I have another question which is more of a conceptual question:

We are trying to customize our Common core metadata schema to include
fields that won't necessarily be strings.  They will be more like objects
that would include fields in it (much like the distribution object in
DCAT).  For example, we'd like to add a field called "animal" that has a
value as such:

animal : {
     type: dog,
     name: fido,
     eats: dog_food
}.

Can I add these types of fields which really are objects?  Is CKAN
extendable to do this?

Thank you ahead of time for any tips that you may have.

Renato


On Wed, Oct 29, 2014 at 5:55 AM, Adrià Mercader <adria.mercader at okfn.org>
wrote:

> Hi Renato and Rachel,
>
> I think there is some confusion about the fields that you should use
> for faceting
>
> > facets_dict['extras_level'] = p.toolkit._('Level')
>
> > It's enough to use the key 'extras_level' to tell CKAN to get the facet
> from the extras. The right-hand side is the name of the facet.
>
> When a dataset dict is indexed in Solr, all extra fields are indexed
> to allow searching and faceting. For each extra, two fields are
> indexed in Solr. Taking your extra 'level' as example:
>
> * The field 'level' will be indexed as 'string'. This basically
> indexes the value "as is" and is the one that should be used for
> faceting and sorting
> * The field 'extra_level' will be indexed as 'text'. This is a more
> complex field type that is basically optimized for searching (eg
> different words or parts will be tokenized, which might be what is
> happening to 'sparql-language')
>
> @Renato, if you are storing 'queryLanguage' as an extra, try using
> 'queryLanguage' when defining your facets instead of
> 'extra_queryLanguage' to see if you get the proper values.
>
> If you are storing the values as tags with a vocabulary, try using
> 'vocab_queryLanguage'. Apparently these are also indexed, but I'm not
> familiar with how they work.
>
> It's pretty clear that we could use some docs in this front...
>
> Hope this helps,
>
> Adrià
>
>
> On 28 October 2014 19:27, Renato Guevara <guevara.renato at gmail.com> wrote:
> > One of the issues I have is the following:
> >
> > I have a plugin that is adding new custom fields.
> >
> > class DescribeFieldsPlugin(p.SingletonPlugin, tk.DefaultDatasetForm):
> >         p.implements(p.IDatasetForm)
> >         p.implements(p.IConfigurer)
> >         p.implements(p.ITemplateHelpers)
> >
> >         def get_helpers(self):
> >                 return {'queryLanguages': queryLanguages}
> >
> >         def _modify_package_schema(self, schema):
> >                 '''
> >                 schema.update({
> >                         'queryLanguage':
> > [tk.get_validator('ignore_missing'),
> >
> > tk.get_converter('convert_to_extras')]
> >                 })
> >                 '''
> >                 schema.update({
> >                         'queryLanguage':
> > [tk.get_validator('ignore_missing'),
> >
> > tk.get_converter('convert_to_tags')('queryLanguages')]
> >                 })
> >                 return schema
> >
> >         def create_package_schema(self):
> >                 schema = super(DescribeFieldsPlugin,
> > self).create_package_schema()
> >                 schema = self._modify_package_schema(schema)
> >                 return schema
> >
> >         def update_package_schema(self):
> >                 schema = super(DescribeFieldsPlugin,
> > self).update_package_schema()
> >                 schema = self._modify_package_schema(schema)
> >                 return schema
> >
> >         def show_package_schema(self):
> >                 schema = super(DescribeFieldsPlugin,
> > self).show_package_schema()
> >                 '''
> >                 schema.update({
> >                         'queryLanguage':
> > [tk.get_converter('convert_from_extras'),
> >
> > tk.get_validator('ignore_missing')]
> >                 })
> >                 '''
> >
> > schema['tags']['__extras'].append(tk.get_converter('free_tags_only'))
> >                 schema.update({
> >                         'queryLanguage': [
> >
> > tk.get_converter('convert_from_tags')('queryLanguages'),
> >                         tk.get_validator('ignore_missing')]
> >                 })
> >                 return schema
> >
> > def create_queryLanguages():
> >         user = tk.get_action('get_site_user')({'ignore_auth': True}, {})
> >         context = {'user': user['name']}
> >         try:
> >                 data = {'id': 'queryLanguages'}
> >                 tk.get_action('vocabulary_show')(context, data)
> >         except tk.ObjectNotFound:
> >                 data = {'name': 'queryLanguages'}
> >                 vocab = tk.get_action('vocabulary_create')(context, data)
> >                 for tag in (u'sparql-language',u'sql'):
> >                         data = {'name': tag, 'vocabulary_id':
> vocab['id']}
> >                         tk.get_action('tag_create')(context, data)
> >
> > def queryLanguages():
> >         create_queryLanguages()
> >         try:
> >                 tag_list = tk.get_action('tag_list')
> >                 queryLanguages = tag_list(data_dict={'vocabulary_id':
> > 'queryLanguages'})
> >                 return queryLanguages
> >         except tk.ObjectNotFound:
> >                 return None
> >
> >
> > However, when I add a facet for queryLanguage, should I add it as an
> extra?
> > Because what is happening is that the term "sparql-language' is getting
> > broken up into two different facets.  Do you know why this may be
> happening?
> >
> > On Tue, Oct 28, 2014 at 4:19 AM, Rachel Knowler <rachel.knowler at liip.ch>
> > wrote:
> >>
> >> Sorry, I should have mentioned this in my reply because it's not covered
> >> well in the docs. The new facet that I added is called 'Level'. It's
> added
> >> to all my datasets as an extra field, during harvesting. In this line,
> I add
> >> it to the facets_dict:
> >>
> >> facets_dict['extras_level'] = p.toolkit._('Level')
> >>
> >> It's enough to use the key 'extras_level' to tell CKAN to get the facet
> >> from the extras. The right-hand side is the name of the facet.
> >>
> >> Best,
> >> Rachel
> >>
> >>
> >>
> >>
> >> On 28.10.2014 09:12, Rachel Knowler wrote:
> >>
> >> Hi Renato,
> >>
> >> To add a custom facet search you have to implement the iFacets interface
> >> in your plugin. This is described in the documentation here:
> >>
> http://docs.ckan.org/en/latest/extensions/plugin-interfaces.html#ckan.plugins.interfaces.IFacets
> .
> >> I recently did this and you can see my code for it here:
> >>
> https://github.com/openresearchdata/ckanext-ord-hierarchy/blob/master/ckanext/ord_hierarchy/plugin.py#L210
> .
> >>
> >> Hope that helps!
> >>
> >> - Rachel
> >>
> >> On 27.10.2014 16:02, Renato Guevara wrote:
> >>
> >> I was able to successfully add a custom field to my dataset.  In fact,
> it
> >> was a vocabulary with its corresponding tags.  However, now, I'm looking
> >> into adding a custom facet search for this specific custom field.  In
> this
> >> case, the name of my custom field is queryLanguage.  I looked through
> the
> >> forum and documentation to see if there were any tutorials but I
> couldn't
> >> find any.  Would someone be able to point me in the right direction.
> Again,
> >> I'm trying to add a custom facet search like there is on the default
> Dataset
> >> search page on the left side.  Right now, the default ones are
> "Publishers",
> >> "Subjects", "Organizations", "Groups", "Tags", "Formats", and
> "Licenses".
> >> I'd like to add "Query Languages".
> >>
> >> Thanks,
> >>
> >> Renato Guevara
> >>
> >>
> >> _______________________________________________
> >> ckan-dev mailing list
> >> ckan-dev at lists.okfn.org
> >> https://lists.okfn.org/mailman/listinfo/ckan-dev
> >> Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
> >>
> >>
> >> --
> >> Liip AG // Limmatstrasse 183 // CH-8005 Zürich
> >> Tel +41 43 500 39 80 // Mob +41 78 658 26 12 // www.liip.ch
> >>
> >>
> >> --
> >> Liip AG // Limmatstrasse 183 // CH-8005 Zürich
> >> Tel +41 43 500 39 80 // Mob +41 78 658 26 12 // www.liip.ch
> >>
> >>
> >> _______________________________________________
> >> ckan-dev mailing list
> >> ckan-dev at lists.okfn.org
> >> https://lists.okfn.org/mailman/listinfo/ckan-dev
> >> Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
> >>
> >
> >
> > _______________________________________________
> > ckan-dev mailing list
> > ckan-dev at lists.okfn.org
> > https://lists.okfn.org/mailman/listinfo/ckan-dev
> > Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
> >
> _______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> https://lists.okfn.org/mailman/listinfo/ckan-dev
> Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20141029/2c6f001d/attachment-0003.html>


More information about the ckan-dev mailing list