0

I'm using Spring data solr and refer this link: https://github.com/christophstrobl/spring-data-solr-showcase

In my table, I want to get full term of row: example, table city:

id: 1
name:    San Francisco 

interface:

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);
}

Method get term in Service:

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;
}

When search i just received a result in city table is:

'San'

But expected result is:

'San Francisco'

Schema.xml:(updated)

<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>

I have replaced tokenizer: StandardTokenizerFactory by KeywordTokenizerFactory and I can get value: 'San Francisco' in autocomplete but return null

How to get full term in this case? thank so much !

Khanh Luong Van
  • 476
  • 3
  • 14
  • 31

1 Answers1

0

this is probably not due to Spring Data Solr but to your Solr schema.

If the name field is analyzed with a tokenizing analyzer (like the Standardanalyzer or the WhitespaceAnalyzer) you will get two terms from 'San Francisco':

  1. San
  2. Francisco

If that is not what you want, you need to use a type (or analyzer) that does not tokenize, like the KeywordTokenizer or a 'solr.StrField' class field in schema.xml

Persimmonium
  • 15,593
  • 11
  • 47
  • 78