如何在Spring Solr Data中获得完整的术语

时间:2017-04-20 05:32:40

标签: spring-mvc solr spring-data solrj spring-data-solr

我正在使用Spring数据solr并参考此链接:https://github.com/christophstrobl/spring-data-solr-showcase

在我的表格中,我希望获得完整的排名: 表格城市:

id: 1
name:    San Francisco 

接口:

interface ProductRepository extends SolrCrudRepository<Product, String> {

@Highlight(prefix = "<b>", postfix = "</b>")
@Query(fields = { SearchableProductDefinition.ID_FIELD_NAME, 
                  SearchableProductDefinition.NAME_FIELD_NAME,
                  SearchableProductDefinition.AVAILABLE_FIELD_NAME }, defaultOperator = Operator.AND)
HighlightPage<Product> findByNameIn(Collection<String> names, Pageable page);

@Facet(fields = { SearchableProductDefinition.NAME_FIELD_NAME})
FacetPage<Product> findByNameStartsWith(Collection<String> nameFragments, Pageable pagebale);
}

方法获取服务中的术语:

public FacetPage<Product> autocompleteNameFragment(String fragment, Pageable pageable) {
    if (StringUtils.isBlank(fragment)) {
        return new SolrResultPage<Product>(Collections.<Product> emptyList());
    }
    return productRepository.findByNameStartsWith(splitSearchTermAndRemoveIgnoredCharacters(fragment), pageable);
}

private Collection<String> splitSearchTermAndRemoveIgnoredCharacters(String searchTerm) {
    String[] searchTerms = StringUtils.split(searchTerm, " ");
    List<String> result = new ArrayList<String>(searchTerms.length);
    for (String term : searchTerms) {
        if (StringUtils.isNotEmpty(term)) {
            result.add(IGNORED_CHARS_PATTERN.matcher(term).replaceAll(" "));
        }
    }
    return result;
}

当我刚收到城市表中的结果时,搜索结果为:

'San'

但预期结果是:

'San Francisco'

Schema.xml的:(的更新

<fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
  <analyzer type="index">
    <tokenizer class="solr.KeywordTokenizerFactory"/>   <!--  StandardTokenizerFactory-->
    <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
    <!-- in this example, we will only use synonyms at query time
    <filter class="solr.SynonymFilterFactory" synonyms="index_synonyms.txt" ignoreCase="true" expand="false"/>
    -->
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
  <analyzer type="query">
    <tokenizer class="solr.KeywordTokenizerFactory"/>   <!--  StandardTokenizerFactory-->
    <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
    <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
</fieldType>

我已经用KeywordTokenizerFactory替换了tokenizer:StandardTokenizerFactory,我可以获得值:'旧金山'在自动完成中但返回null

如何在这种情况下获得满学期?非常感谢!

1 个答案:

答案 0 :(得分:0)

这可能不是由于Spring Data Solr而是由于您的Solr架构。

如果使用标记化分析器(如Standardanalyzer或WhitespaceAnalyzer)分析名称字段,您将从“旧金山”获得两个术语:

  1. 旧金山
  2. 如果那不是你想要的,你需要使用一个没有标记化的类型(或分析器),比如schema.xml中的KeywordTokenizer或'solr.StrField'类字段