Can't ignore punctuation in titles - Solr 5 and Drupal

左心房为你撑大大i 提交于 2019-12-13 19:14:45

问题


The other week I posted a question about removing punctuation from solr search in Drupal. That was using Solr 4. However, since then the development I am doing has changed from solr 4 to solr 5, and now I am having the same problem but the fix at Can't remove punctuation in Solr no longer works. This causes problems when sorting by titles since a lot of content titles have quotes around.

<field name="label" type="text" indexed="true" stored="true" termVectors="true" omitNorms="true"/>
<fieldType name="text" class="solr.TextField" positionIncrementGap="100">
  <analyzer>
    <charFilter class="solr.MappingCharFilterFactory" mapping="mapping-ISOLatin1Accent.txt"/>
    <tokenizer class="solr.WhitespaceTokenizerFactory"/>
    <filter class="solr.StopFilterFactory"
            ignoreCase="true"
            words="stopwords.txt"
            />
    <filter class="solr.WordDelimiterFilterFactory"
            protected="protwords.txt"
            generateWordParts="1"
            generateNumberParts="1"
            catenateWords="1"
            catenateNumbers="1"
            catenateAll="0"
            splitOnCaseChange="0"
            preserveOriginal="1"/>
    <filter class="solr.LengthFilterFactory" min="2" max="100" />
    <filter class="solr.LowerCaseFilterFactory"/>
    <filter class="solr.SnowballPorterFilterFactory" language="English" protected="protwords.txt"/>
    <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
  </analyzer>
</fieldType>

I've tried adding the following rules but apostrophes and quotation marks stay there stubbornly and interfere when sorting by titles, putting anything with quotes at the beginning first on the list.

    <charFilter class="solr.HTMLStripCharFilterFactory" />
    <filter class="solr.ApostropheFilterFactory"/>
    <filter class="solr.PatternReplaceFilterFactory"
        pattern="^\p{Punct}*(.*?)\p{Punct}*$"
        replacement="$1"/>

回答1:


All of the Solr solutions I tried were ineffective unfortunately, so I solved it from the Drupal side which turned out to be a lot simpler. The code below replaces all special characters and numbers, turns the string to lowercase and then adds it to the solr document. The second function adds it to the available sort methods.

function my_module_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type, $env_id) {

      # to keep letters only
      $title = trim($entity->title);
      $title = str_replace(' ', '_', $title);
      $title = preg_replace('/[^a-z]+/i', '', $title);
      $title = strtolower($title);
      $document->addField('ss_new_sort',$title);

}

function my_module_apachesolr_query_prepare(DrupalSolrQueryInterface $query) {
      $query->setAvailableSort('ss_new_sort', array('title' => t('Title'), 'default' => 'asc'));
}


来源:https://stackoverflow.com/questions/36798803/cant-ignore-punctuation-in-titles-solr-5-and-drupal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!