I just find out that my Solr index doesn't contain a _id field.
and it is not possible to get item id. UniqueId is present but this is not really useful.
public class MyClass
{
[IndexField(BuiltinFields.UniqueId)]
public string UniqueId { get; set; }
[IndexField(BuiltinFields.ID)]
public int Id { get; set; }
}
How to add item id to Solr index ?
What should I add to schema.xml ?
Is next string would be enough ?
<field name="_id" type="string" indexed="true" stored="true" required="true" />
If yes why Sitecore doesn't include it during build schema.xml file for Solr ?
It seems that it could be an issue with duplication records in search result that I have as well :)
As Richard answered, there is a _group
field in Solr. If you want it to be translated automatically into ID
object, you can use:
[TypeConverter(typeof (IndexFieldIDValueConverter))]
[IndexField("_group")]
public virtual ID ItemId { get; set; }
Remember that if you have multiple versions or language versions, _group
field will not be enough, cause there will be multiple documents with the same _group
(id). In that case you may want to use UniqueId
- it contains information about id, version, language and database:
[IndexField("_uniqueid")]
public virtual string UniqueId { get; set; }
And then to get the Item use:
private Item _innerItem;
public virtual Item GetItem()
{
if (_innerItem == null)
{
ItemUri uri = new ItemUri(UniqueId);
_innerItem = Factory.GetDatabase(uri.DatabaseName).GetItem(uri.ItemID, uri.Language, uri.Version);
}
return _innerItem;
}
Or you can just inherit your MyClass
from Sitecore.ContentSearch.SearchTypes.SearchResultItem
and you will have all the built in fields there.
In SOLR the Item Id field is called _group
- this should be added to your schema.xml file when you generate it.
If its not there, this is the definition I have:
<field name="_group" stored="true" indexed="true" type="string"/>
The UniqueId field contains the ItemId along with the version and language of the item.