1

I'm using the Apache Solr Search Integration and Facet API modules. I'm trying to implement facets for custom fields publication_date, publication_author, and journal, which are part of the content type publication.

These 3 fields are indexed successfully as ss_date, ss_author, and ss_journal. Furthermore, I can search against these fields on the default apachesolr search page. However, the facets for these fields show up as "_empty_". For example, when the search page loads, under the "publication_author" facet header, one bullet item shows up as "_empty_ (10)", which to me means that the author field is being indexed successfully with value "NULL" or possibly literally "_empty_" for all 10 publication nodes. Code is below for just the author facet.

define custom facets

function HOOK_facetapi_facet_info(array $searcher_info) {
    $facets = array();
    // Facets are usually associated with the type of content stored in the index.
    if (isset($searcher_info['types']['node'])) {
        $facets['ss_author'] = array(
            'name' => 'ss_author',
            'label' => t('custom: author'),
            'description' => t('Shows the authors'),
            'field' => 'ss_author',
            'dependency plugins' => array('role'),
            'default widget' => 'links',
            'allowed operators' => array(FACETAPI_OPERATOR_AND => TRUE, FACETAPI_OPERATOR_OR => TRUE),
            'facet missing allowed' => FALSE,
            'facet mincount allowed' => FALSE,
            //'map callback' => 'not used for now',
            'max callback' => FALSE,
            'default sorts' => array(
                array('active', SORT_DESC),
                array('count', SORT_DESC),
                array('display', SORT_ASC),
            ),
        );
    }
    return $facets;
}

"ss_author" is the name of the field added to the ApacheSolr document during indexing. If you'd like the code used to actually index these fields, let me know. Thanks very much!

  • Hey, if you go to the facets configuration page on your site, do you see your new fields as facet options? i.e. do your fields appear on the list with a check mark to enable as facets? The url to this page should be something like /admin/config/search/apachesolr/settings/localhost/facets – Boriana Ditcheva Jul 17 '13 at 15:37
  • @Boriana yep, my fields appear in the facets list, they are enabled, and the appropriate blocks are also placed in the left sidebar region; I can see those facet blocks where they're supposed to be, but it seems like the values of the custom fields are lost along the way – Don Jul 17 '13 at 18:55
1

Facets, by their very nature, work best when you use them for 'select' type fields, not pure text. Facets work very well - and automatically - with taxonomy terms for example, because terms are re-used and it's easy to group based on select type fields.

Whenever I've wanted to use custom fields as facets, I've implemented them as terms (tagging) or select fields, and then they work properly.

So, I'm wondering - is that something you could do? Rather than setting up your fields as 'text', could you set them up as select-type fields (whether it's tags, which don't have to be predetermined or select options which you have to enter in advance)? That type of field is easier to GROUP by - which is what facets are really doing.

It may not be an option, but I have a feeling that would resolve your issue.

Let us know how this gets resolved!

  • I think I've solved my problem for now (see my answer), but I'll consider this approach, and if I use it, I'll be sure to mark your answer as correct. Thanks for the feedback! – Don Jul 17 '13 at 20:41
1

I wasn't properly storing the values of my custom fields. Specifically, in hook_apachesolr_index_document_build, I was trying to get the value of "publication_author" as

$author = $entity->field_publication_author;

while the actual value should be retrieved with

$author = $entity->field_publication_author[$entity->language][0]['value'];

The full hook follows:

function hook_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type, $env_id) {
    $author = $entity->field_publication_author[$entity->language][0]['value'];
    // get other fields
    $document->addField('ss_author', $author);
    // add other fields to document
}

Also remember you need hook_apachesolr_query_alter as well:

function hook_apachesolr_query_alter($query){
    $query->addParams(array('fl' => array('ss_author')));
}
  • Aaah - that makes sense. :-) Thanks for sharing your solution. – Boriana Ditcheva Jul 17 '13 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.