0
votes

I have strange issue when I am trying to remove some records from custom solr index.

I created code like this

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();
            }
        }

Search Item property when I need to get UniqueId

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

Based on debug info IIndexableUniqueId contains correct values like:"sitecore://web/{66d75448-72a5-4d94-9788-61c6c64b9251}?lang=en-au&ver=1" what is equal to _uniqueid field in solr index.

I had 4 records in my custom solr index.

After first run one records was removed from index, But 3 records are steel there. I have ran code couple of times, but 3 records always inside of index.

What could be wrong with my code ?

1

1 Answers

0
votes

I found solution: insted of using IIndexableUniqueId it is better to use 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;
        }
    }

regarding 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);
}