0

I am using the search_api and the solr backend. It is possible to index first letter of content so I can use solr to create an A-Z style filter?

2

OK, it's a little late. But yes, you can index a glossary. I've build an A-Z-Filter in SOLR based on custom entity-fields like surname here http://www.autorenwelt.de/verzeichnis/menschen My filter based on the following description: https://drupal.org/node/2069005

I need only this functions to build a field in the SOLR index:

/**
 * Apachesolr index name for this module.
 *
 * Dynamically creates name as expected by Solr.
 */
function _MODULENAME_search_index_key($key) {
  $index = array(
    'name' => $key,
    'multiple' => FALSE,
    'index_type' => 'string',
  );
  return apachesolr_index_key($index);
}


/**
 * Implementation of hook_apachesolr_index_document_build()
 *
 * Alters what is sent to Solr in $document.
 */
function MODULENAME_apachesolr_index_document_build($document, $entity, $entity_type, $env_id) {
    $glossary_key = _MODULENAME_search_index_key('custom_glossary');
    // We want to index the first letter of the title for use in our glossary
    $key = strtoupper($entity->title[0]);
    // Build a custom glossary of profile2 entities
    if ('profile_main' == $entity->type) {
        $key = strtoupper($entity->field_profile_surname['und'][0]['safe_value'][0]);
    }

    $indexed = '0 - 9';
    switch ($key) {
        case 'A':
        case 'B':
        case 'C':
        case 'D':
        case 'E':
            $indexed = 'A - E';
            break;
        case 'F':
        case 'G':
        case 'H':
        case 'I':
        case 'J':
            $indexed = 'F - J';
            break;
        case 'K':
        case 'L':
        case 'M':
        case 'N':
        case 'O':
            $indexed = 'K - O';
            break;
        case 'P':
        case 'Q':
        case 'R':
        case 'S':
        case 'T':
            $indexed = 'P - T';
            break;
        case 'U':
        case 'V':
        case 'W':
        case 'X':
        case 'Y':
        case 'Z':
            $indexed = 'U - Z';
            break;
    }
    $document->{$glossary_key} = $indexed;
}

And I use hook_facetapi_facet_info_alter to register this facet:

/**
 * Implementation of hook_facetapi_facet_info_alter().
 **/
function MODULENAME_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_custom_glossary'] = array(
            'default widget' => 'links',
            'name' => 'ss_custom_glossary',
            'label' => t('A - Z Glossar'),
            'dependency plugins' => array('role'),
            'description' => t('Glossar basierend auf dem ersten Buchstaben des Node-Titles.'),
            'field' => 'ss_custom_glossary',
            'field_alias' => 'ss_custom_glossary',
            'weight' => -50
        );
    }

    return $facets;
}

Ciao Ralf

0

drupal itself provide glossary module by default go to views->search with glossary->enable and you get something like this http://www.dna2life.com/a-z-index

still if you want to index manually and want to show indexing in single page http://drupal.org/node/1895352

2
  • Does this work with solr / search_api? I was trying to get it to work but not having much luck. Feb 18 '13 at 10:51
  • this does work, I believe it was a fault for me. Feb 19 '13 at 16:31
0

I used the code below with search_api/search_api_solr.

<?php
/**
 * @file
 * Index First letter of title to make a Glossary
 * and provides new fields to index.
 */

/**
 * Implements hook_entity_property_info_alter().
 */
function YOUR_MODULE_entity_property_info_alter(&$info) {

    $info['node']['properties']['lexical'] = array(
        'type' => 'string',
        'label' => t('1st Letter of the title'),
        'sanitized' => TRUE,
        'getter callback' => 'YOUR_MODULE_lexical_getter_callback',
    );

}

/**
 * Getter callback for lexical property.
 */
function YOUR_MODULE_lexical_getter_callback($item) {
    return strtoupper($item->title[0]);
}

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.