SolrNet: How can I perform Fuzzy search in SolrNet

2019-07-31 18:58发布

I am searching a "text" field in solr and I looking for a way to match (for e.g.) "anamal" with "animal". My schema for the "text" field looks like the following:

            <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
            <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="1" catenateNumbers="1" catenateAll="0" splitOnCaseChange="1" />
            <filter class="solr.LowerCaseFilterFactory" />
            <filter class="solr.SnowballPorterFilterFactory" language="English" protected="protwords.txt" />
            <filter class="solr.PorterStemFilterFactory"/>
        </analyzer>
        <analyzer type="query">
            <tokenizer class="solr.WhitespaceTokenizerFactory" />

            <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true" />
            <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
            <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="0" catenateNumbers="0" catenateAll="0" splitOnCaseChange="1" />
            <filter class="solr.LowerCaseFilterFactory" />
            <filter class="solr.SnowballPorterFilterFactory" language="English" protected="protwords.txt" />
            <filter class="solr.PorterStemFilterFactory"/>
        </analyzer>
    </fieldType>     

Using SolrNet how can I perform a Fuzzy search to match "anamal" with "animal"?

2条回答
forever°为你锁心
2楼-- · 2019-07-31 19:06

You can do this with index time synonyms, please see the Solr Wiki - SynonymFilterFactory for details on how to set this up.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-31 19:22

It seems that you can use the Lucene "~" fuzzy search, but there is a trick! If left unabated SolrNet will escape the tilde character for you by default, but it can be turned off... so this should do the trick for you:

var query = new SolrQueryByField("myField", "some value~");

query.Quoted = false;

The caveat seems to be that (for now at least) you have to use a query by field, the same parameter doesn't exist on a SolrQuery... but that makes sense I guess.

查看更多
登录 后发表回答