Sitecore使用SOLR实现短语分面搜索

3

有人有使用SOLR的Sitecore索引配置和关键字分词器的示例吗?我想在一个包含多个单词字符串的字段上进行面向内容的筛选,但是目前返回的聚合值正在将字段中的单词拆分并返回。

例如,我有一些具有州字段的项目,并且我正在尝试在该州字段上进行面向内容的筛选,其中该字段具有类似于新罕布什尔州、南达科他州等值。但是在结果中,我得到了以下聚合值:

名称=新, 聚合=xx
名称=罕布什尔, 聚合=xx
名称=南, 聚合=xx
名称=达科他, 聚合=xx

请问有谁能帮我更改正确的配置呢?

这是我的当前配置:

      <index id="site_search_web_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
        <param desc="name">$(id)</param>
        <param desc="core">site_search_web</param>
        <param desc="propertyStore" ref="contentSearch/databasePropertyStore" param1="$(id)" />
        <strategies hint="list:AddStrategy">
          <strategy ref="contentSearch/indexUpdateStrategies/onPublishEndAsync" />
        </strategies>

        <locations hint="list:AddCrawler">
          <crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
            <Database>web</Database>
            <Root>/sitecore/content/Home</Root>
          </crawler>
        </locations>
      </index>

你应该能够将字段的类型更改为字符串,而不是使用带有分词器的文本类型。分词器会在标记边界(如空格、连字符等)处断开文本,这就是为什么你在外观中看到了单个标记的原因。 - arun
能否在索引级别而不是字段级别上完成这个操作?如果可以的话,我会非常感激提供示例配置!谢谢! - aceanindita
我相信你被锁定在字段级别,但是你总可以使用计算字段或者声明一个别名来代替该字段,并使用不同的数据类型来处理任何特殊情况。 - Jon Upchurch
1个回答

4
你可以通过以下解决方案之一实现这一点: 解决方案1 你可以创建一个计算字段,返回分类值,并将计算字段类型设置为"string",以避免分词。你的计算字段应该像这样:
public class TitleComputedField : IComputedIndexField
{
    public object ComputeFieldValue(IIndexable indexable)
    {
        if (indexable == null) throw new ArgumentNullException("indexable");
        var scIndexable = indexable as SitecoreIndexableItem;

        if (scIndexable == null)
        {
            Log.Warn(
                this + " : unsupported IIndexable type : " + indexable.GetType(), this);
            return false;
        }

        var item = (Item)scIndexable;
        if (item == null)
        {
            Log.Warn(
                this + " : unsupported SitecoreIndexableItem type : " + scIndexable.GetType(), this);
            return false;
        }

        if (String.Compare(item.Database.Name, "core", StringComparison.OrdinalIgnoreCase) == 0)
        {
            return false;
        }

        return = item.Fields["Title"];
    }

    public string FieldName { get; set; }
    public string ReturnType { get; set; }
}

请按照以下步骤在 Sitecore.ContentSearch.Solr.Indexes.config 中配置计算字段:

      <fields hint="raw:AddComputedIndexField">
        ...
        <field fieldName="plaintitle"             returnType="string">YourNamespace.TitleComputedField, YourAssembly</field>
      </fields>

最后,如果您在“plaintitle”字段上进行筛选,应该可以得到预期的结果。

解决方案2

您可以通过更新solr schema.xml来在索引级别上创建字段,具体方法如下:

在solr中创建一个新字段,类型为string。

<fields>
   ...
   <field name="plaintitle" type="string" indexed="true" stored="true" />
</fields>

然后创建一个“copyfield”,将原始字段复制到新字段中

<copyField source="title_t" dest="plaintitle" />

在这两种解决方案中,您可以使用以下代码在新字段上进行分面:

在这两种解决方案中,您可以使用以下代码在新字段上进行分面:

query.FacetOn(i => i["plaintitle"]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接