使用 NGram 自动完成 Sitecore 和 Lucene 搜索

标签 sitecore lucene.net sitecore8

我正在尝试使用 Ngram 为内容搜索设置自动完成功能。 这是我的 lucene 索引:

<autocompleteSearchConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
      <indexAllFields>false</indexAllFields>
      <initializeOnAdd>true</initializeOnAdd>
      <analyzer ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration/analyzer" />
      <fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch">
        <fieldNames hint="raw:AddFieldByFieldName">
          <field
            fieldName="page_title"
            storageType="YES"
            indexType="TOKENIZED"
            vectorType="NO"
            boost="1.5f"
            nullValue="NULL"
            emptyString="EMPTY"
            type="System.String"
            settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
            <analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.NGramAnalyzer, Sitecore.ContentSearch.LuceneProvider" />
          </field>
        </fieldNames>
      </fieldMap>
      <fields hint="raw:AddComputedIndexField">
        <field fieldName="page_title" storageType="yes">Client.Website.Code.Search.AutoCompleteTitle, Client.Website</field>
      </fields>
      <fieldReaders ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration/fieldReaders"/>
      <indexFieldStorageValueFormatter ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration/indexFieldStorageValueFormatter"/>
      <indexDocumentPropertyMapper ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration/indexDocumentPropertyMapper"/>
      <documentBuilderType>Sitecore.ContentSearch.LuceneProvider.LuceneDocumentBuilder, Sitecore.ContentSearch.LuceneProvider</documentBuilderType>
    </autocompleteSearchConfiguration>

请注意,我正在使用 NgramAnalyzer(引用:Sitecore.ContentSearch.LuceneProvider.Analyzers)。

当我在 luke 中查看这个索引时,我可以看到它显示了正确的数据。 但是,以下 iQueryable 不保留任何结果。

var index = ContentSearchManager.GetIndex("INDEX NAME GOES HERE");
using (var context = index.CreateSearchContext())
{
var query = context.GetQueryable<AutocompleteSearchResult>().Where(i => i.PageTitle == term)
var result = query.GetResults();
}

最佳答案

为什么不使用“StartsWith”而不是 ==?

参见this文章。

Sitecore provides an n-gram analyzer for Lucene.net (Sitecore.ContentSearch.LuceneProvider.Analyzers). If you use Solr, you can set this up in the Solr Schema.xml file.

You use the n-gram analyzer to create autocomplete functionality for search input. The analyzer breaks tokens up into unigrams, bigrams, trigrams, and so on. When a user types a word, the n-gram analyzer looks the word up in different positions, using the tokens that it generated.

You add support for autocomplete by adding a new field to the index and mapping this field to use the n-gram analyzer instead of the default. When you run the LINQ query to query that field, use the following code:

using (IProviderSearchContext context = Index.CreateSearchContext())
        {
            result = context.GetQueryable<SearchResultItem>().
                .Where(i => i.Name.StartsWith(“some”))
                .Take(20)
                .ToList();
        }

Sitecore provides an implementation that uses trigrams and a set of English stop words. If you have other requirements, you can build a new analyzer and change these settings.

关于使用 NGram 自动完成 Sitecore 和 Lucene 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36697210/

相关文章:

asp.net-mvc - 在 Sitecore 中使用 razor 作为渲染引擎。有或没有 mvc 的其余部分

c# - 如果正在预览页面,Sitecore API 会在后面检查代码

c# - 为什么所有 Glass.Mapper 字段都应该是虚拟的?

c# - Lucene.Net 大于/小于 TermRangeQuery?

尊重HTML标记的Lucene.NET搜索突出显示

Sitecore FXM 问题 - 添加到占位符对话框中缺少父选择器

c# - 索引文件大小限制

Sitecore 多个自定义用户配置文件

layout - 站点核心 8 : Sharing Layout among different languages

sitecore - 在 Sitecore 8.1 中编辑文本字段时最终布局被删除