Sitecore 不会从自定义 solr 索引中删除记录



当我试图从自定义solr索引中删除一些记录时,我有奇怪的问题。

我创建了这样的代码

public void DeleteRecordsFromIndex(string indexName,IEnumerable<IIndexableUniqueId> uniqueIds)
        {
            if (uniqueIds == null || string.IsNullOrEmpty(indexName) || !uniqueIds.Any())
            {
                return;             
            }
            using (IProviderDeleteContext deleteContext = ContentSearchManager.GetIndex(indexName).CreateDeleteContext())
            {
                foreach (var indexId in uniqueIds)
                {
                    deleteContext.Delete(indexId);
                }                               
                deleteContext.Commit();
            }
        }

当我需要获得UniqueId时搜索项目属性

[IndexField("_uniqueid")]
public IIndexableUniqueId UniqueId
    {
        get
        {
            return new `enter code here`IndexableUniqueId<string>(this.Uri.ToString());
        }
    }

根据调试信息IIndexableUniqueId包含正确的值如: " sitecore://web/{66 d75448 - 72 - a5 4 - d94 - 9788 - 61 - c6c64b9251} ? lang = en-au&版本= 1"在solr索引中_uniqueid字段等于什么?

我的自定义solr索引中有4条记录。

第一次运行后,从索引中删除了一条记录,但有3条记录在那里。我已经运行了几次代码,但3条记录总是在索引里面。

我的代码有什么问题?

我找到了解决方案:与其使用IIndexableUniqueId,不如使用IIndexableId

public IIndexableId GetIndexableId(ID itemId)
    {
        //map to _group value
        var id = itemId.ToString().ToLower().Replace("{", "").Replace("}", "").Replace("-", "");
        return new IndexableId<string>(id);
    }

public int DeleteRecordsFromIndex(string indexName, IIndexableId uniqueIds)
    {
        if (uniqueIds == null || string.IsNullOrEmpty(indexName))
        {
            return -1;
        }
        using (var deleteContext = ContentSearchManager.GetIndex(indexName).CreateDeleteContext())
        {
            var resuls = deleteContext.Delete(uniqueIds);//this method use _group fields for remove records. All languages will be removed.
            deleteContext.Commit();
            return resuls;
        }
    }

关于ILSpy, sitecore remove item by _group

// Sitecore.ContentSearch.SolrProvider.SolrUpdateContext
public void Delete(IIndexableId id)
{
    Assert.ArgumentNotNull(id, "id");
    object obj = this.index.Configuration.IndexFieldStorageValueFormatter.FormatValueForIndexStorage(id);
    SolrQueryByField solrQueryByField = new SolrQueryByField("_group", obj.ToString());
    SolrQueryByField solrQueryByField2 = new SolrQueryByField("_indexname", this.index.Name);
    this.solr.Delete(solrQueryByField && solrQueryByField2);
    this.CommitPolicyExecutor.IndexModified(this, id, IndexOperation.Delete);
}

最新更新