Is Solr SuggestComponent able to return shingles instead of whole field values?

后端 未结 1 1853
温柔的废话
温柔的废话 2021-01-03 04:43

I use solr 5.0.0 and want to create an autocomplete functionality generating suggestions from the word-grams (or shingles) of my documents. The problem is that in return of

相关标签:
1条回答
  • 2021-01-03 05:21

    In schema.xml define fieldType as follows:

     <fieldType name="text_autocomplete" class="solr.TextField" positionIncrementGap="100">
            <analyzer type="index">
                <tokenizer class="solr.WhitespaceTokenizerFactory"/>
                <filter class="solr.LowerCaseFilterFactory"/>
                <filter class="solr.ShingleFilterFactory" minShingleSize="2" maxShingleSize="5"/>
            </analyzer>
            <analyzer type="query">
                <tokenizer class="solr.KeywordTokenizerFactory"/>
                <filter class="solr.LowerCaseFilterFactory"/>
            </analyzer>
        </fieldType>
    

    In schema.xml define your field as follows:

    <field name="example_field" type="text_autocomplete" indexed="true" stored="true"/>
    

    Write your query as follows:

    query?q=*&
    rows=0&
    facet=true&
    facet.field=example_field&
    facet.limit=-1&
    wt=json&
    indent=true&
    facet.prefix=so
    

    In the facet.prefix field, specify the term being searched for which you want suggestions ('so', in this example). If you need less than 5 words in the suggestion, reduce maxShingleSize in the fieldType definition accordingly. By default, you will get the results in decreasing order of their frequency of occurrence.

    0 讨论(0)
提交回复
热议问题