How to exclude fields form being indexed with Sitecore search (new method)

社会主义新天地 提交于 2019-12-06 11:04:19

Are you using the Advanced Database Crawler? If so, there are sections you can add to include specific fields by their GUIDs and exclude specific fields by their GUIDs. Below I've provided a snippet where the hint attribute of the <include> node defines whether the fields should be included or excluded

<master type="Sitecore.SharedSource.Search.Crawlers.AdvancedDatabaseCrawler,Sitecore.SharedSource.Search">
  <Database>master</Database>
  <Root>/sitecore/content</Root>
  <IndexAllFields>false</IndexAllFields>

  <include hint="list:IncludeField">
    <!-- some field you'd want to include -->
    <fieldId>{8CDC337E-A112-42FB-BBB4-4143751E123F}</fieldId>
  </include>

  <include hint="list:ExcludeField">
    <!-- __revision field -->
    <fieldId>{8CDC337E-A112-42FB-BBB4-4143751E123F}</fieldId>
    <!-- __context menu field -->
    <fieldId>{D3AE7222-425D-4B77-95D8-EE33AC2B6730}</fieldId>
    <!-- __security field -->
    <fieldId>{DEC8D2D5-E3CF-48B6-A653-8E69E2716641}</fieldId>
    <!-- __renderings field -->
    <fieldId>{F1A1FE9E-A60C-4DDB-A3A0-BB5B29FE732E}</fieldId>
  </include>

You can see a sample search config for the Advanced Database Crawler on SVN.

If you are using the Standard Sitecore DatabaseCrawler I would suggest you create a custom crawler that inherits from the Sitecore Database crawler and then override the AddAllFieldsMethod. Then just configure your index to use your custom crawler

You can look at the source code for the Advanced Database Crawler for an example of how that can be done. Something like this: (NOTE THIS HAS NOT BEEN TESTED)

public class DatabaseCrawler : Sitecore.Search.Crawlers.DatabaseCrawler
{
    protected override void AddAllFields(Lucene.Net.Documents.Document document, Sitecore.Data.Items.Item item, bool versionSpecific)
    {
        if(IndexAllFields)
        {
            base.AddAllFields(document, item, versionSpecific);
        }
        else
        {
            var fieldsToIndex = new List<string>() {"title", "Text"};
            foreach (var field in fieldsToIndex)
            { 
                var scField = item.Fields[field];
                document.Add(new LuceneField(scField.Key, scField.Value, LuceneField.Store.NO, LuceneField.Index.UN_TOKENIZED));
            }
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!