Sitecore ContentSearch duplicate indexes when savi

2019-05-23 21:55发布

问题:

I have implemented an index for our application which is supposed to index our items from an item bucket on the master database, but it seems that each time an item is updated and saved (not published, since the index works on master) an additional index is created for that item, which means another search result for the item.
After regenerating the indexes there seems to be no issue, each item in the bucket is indexed exactly once. I'm adding the index config for the search functionality below:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
    <customSearchNewsItem>
        <Configuration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
            <IndexAllFields>true</IndexAllFields>
            <fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch">
                <!-- Add fields to the Default Index Configuration -->
                <fieldNames hint="raw:AddFieldByFieldName">
                    <fieldType fieldName="_rendering" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" type="Sitecore.SharedSource.Search.DynamicFields.RenderingField,Sitecore.SharedSource.Search"
                        settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
        <field fieldName="category" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String"   settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
                </fieldNames>
            </fieldMap>
            <indexFieldStorageValueFormatter type="Sitecore.ContentSearch.LuceneProvider.Converters.LuceneIndexFieldStorageValueFormatter, Sitecore.ContentSearch.LuceneProvider">
                <converters hint="raw:AddConverter">
                    <converter handlesType="System.Guid"                                                          typeConverter="Sitecore.ContentSearch.Converters.IndexFieldGuidValueConverter, Sitecore.ContentSearch" />
                    <converter handlesType="Sitecore.Data.ID, Sitecore.Kernel"                                    typeConverter="Sitecore.ContentSearch.Converters.IndexFieldIDValueConverter, Sitecore.ContentSearch" />
                    <converter handlesType="Sitecore.Data.ShortID, Sitecore.Kernel"                               typeConverter="Sitecore.ContentSearch.Converters.IndexFieldShortIDValueConverter, Sitecore.ContentSearch" />
                    <converter handlesType="System.Boolean"                                                       typeConverter="Sitecore.ContentSearch.LuceneProvider.Converters.IndexFieldBooleanValueConverter, Sitecore.ContentSearch.LuceneProvider" />
                    <converter handlesType="System.DateTime"                                                      typeConverter="Sitecore.ContentSearch.LuceneProvider.Converters.IndexFieldDateTimeValueConverter, Sitecore.ContentSearch.LuceneProvider" />
                    <converter handlesType="System.DateTimeOffset"                                                typeConverter="Sitecore.ContentSearch.Converters.IndexFieldDateTimeOffsetValueConverter, Sitecore.ContentSearch" />
                    <converter handlesType="System.TimeSpan"                                                      typeConverter="Sitecore.ContentSearch.Converters.IndexFieldTimeSpanValueConverter, Sitecore.ContentSearch" />
                    <converter handlesType="Sitecore.ContentSearch.SitecoreItemId, Sitecore.ContentSearch"        typeConverter="Sitecore.ContentSearch.Converters.IndexFieldSitecoreItemIDValueConvertor, Sitecore.ContentSearch">
                        <param type="Sitecore.ContentSearch.Converters.IndexFieldIDValueConverter, Sitecore.ContentSearch"/>
                    </converter>
                    <converter handlesType="Sitecore.ContentSearch.SitecoreItemUniqueId, Sitecore.ContentSearch"  typeConverter="Sitecore.ContentSearch.Converters.IndexFieldSitecoreItemUniqueIDValueConverter, Sitecore.ContentSearch">
                        <param type="Sitecore.ContentSearch.Converters.IndexFieldItemUriValueConverter, Sitecore.ContentSearch"/>
                    </converter>
                    <converter handlesType="Sitecore.Data.ItemUri, Sitecore.Kernel"                               typeConverter="Sitecore.ContentSearch.Converters.IndexFieldItemUriValueConverter, Sitecore.ContentSearch" />
                    <converter handlesType="Sitecore.Globalization.Language, Sitecore.Kernel"                     typeConverter="Sitecore.ContentSearch.Converters.IndexFieldLanguageValueConverter, Sitecore.ContentSearch" />
                    <converter handlesType="System.Globalization.CultureInfo"                                     typeConverter="Sitecore.ContentSearch.Converters.IndexFieldCultureInfoValueConverter, Sitecore.ContentSearch" />
                    <converter handlesType="Sitecore.Data.Version, Sitecore.Kernel"                               typeConverter="Sitecore.ContentSearch.Converters.IndexFieldVersionValueConverter, Sitecore.ContentSearch" />
                    <converter handlesType="Sitecore.Data.Database, Sitecore.Kernel"                              typeConverter="Sitecore.ContentSearch.Converters.IndexFieldDatabaseValueConverter, Sitecore.ContentSearch" />
                </converters>
            </indexFieldStorageValueFormatter>
            <IndexDocumentPropertyMapper type="Sitecore.ContentSearch.LuceneProvider.DefaultLuceneDocumentTypeMapper, Sitecore.ContentSearch.LuceneProvider" />
            <include hint="list:ExcludeTemplate">
                <template>{A87A00B1-E6DB-45AB-8B54-636FEC3B5523}</template>
            </include>
        </Configuration>
    </customSearchNewsItem>
    <contentSearch>
        <configuration type="Sitecore.ContentSearch.LuceneProvider.LuceneSearchConfiguration, Sitecore.ContentSearch.LuceneProvider">
            <indexes hint="list:AddIndex">
                <index id="sitecore_cope_news_index" type="Sitecore.ContentSearch.LuceneProvider.LuceneIndex, Sitecore.ContentSearch.LuceneProvider">
                    <param desc="name">$(id)</param>
                    <param desc="folder">$(id)</param>
                    <!-- This initializes index property store. Id has to be set to the index id -->
                    <param desc="propertyStore" ref="contentSearch/databasePropertyStore" param1="$(id)" />
                    <Configuration ref="customSearchNewsItem/Configuration" />
                    <strategies hint="list:AddStrategy">
                        <!-- NOTE: order of these is controls the execution order -->
                        <strategy ref="contentSearch/indexUpdateStrategies/syncMaster" />
                    </strategies>
                    <commitPolicyExecutor type="Sitecore.ContentSearch.CommitPolicyExecutor, Sitecore.ContentSearch">
                        <policies hint="list:AddCommitPolicy">
                            <policy type="Sitecore.ContentSearch.TimeIntervalCommitPolicy, Sitecore.ContentSearch" />
                        </policies>
                    </commitPolicyExecutor>
                    <locations hint="list:AddCrawler">
                        <crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
                            <Database>master</Database>
                            <Root>/sitecore/content/Cope/COPE/Site Settings/Site Content/News</Root>
                        </crawler>
                    </locations>
                </index>
            </indexes>
        </configuration>
    </contentSearch>
</sitecore>

回答1:

Lucene recognises the index by its unique id which is the _uniqueid field in the <fieldNames> section

Since you're creating your own/separate indexing config, you need to have that field section added in the fieldNames section so it will recognise the index.

so it should be like

<fieldNames hint="raw:AddFieldByFieldName">
    <field fieldName="_uniqueid" storageType="YES" indexType="TOKENIZED"    
                  vectorType="NO" boost="1f" type="System.String"      
                 settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
         <analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider" />
    </field>
    <!-- Your custom Fields -->
</fieldNames>


回答2:

The problem is that you are overriding the default fieldNames configuration. You will have to include more fields in the fieldMap/fieldNames section because Sitecore needs to convert several fields to lowercase to be able to match documents correctly in lucene.

Instead of only adding your custom fields _rendering and category, copy the entire section from the DefaultIndexConfiguration and then add your custom fields.

<fieldNames hint="raw:AddFieldByFieldName">
    <fieldType fieldName="_rendering" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" type="Sitecore.SharedSource.Search.DynamicFields.RenderingField,Sitecore.SharedSource.Search" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />

    <field fieldName="category" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String"   settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />

    <!-- Fields from DefaultIndexConfiguration here -->         

</fieldNames>