Reputation: 4100
I am using sitecore Search API, but I am getting exception 'Exception Details: System.InvalidCastException: Invalid cast from 'System.String' to 'Sitecore.Data.ID'.'
On this line: foreach (var result in query)
. I am using following code:
using (var context = ContentSearchManager.GetIndex("my_text_index").CreateSearchContext())
{
IQueryable<Person> query = context.GetQueryable<Person>().Where(p=> p.Firstname.Equals("John"));
foreach(result in query) // I am getting the exception here on this line.
{
lbltest.Text = result.Name;
}
}
This is the class "Person
"
public class Person : SearchResultItem
{
[IndexField("firstname_t")]
public string Firstname { get; set; }
[IndexField("surname_t")]
public string Surname { get; set; }
}
This is the stack trace:
[InvalidCastException: Invalid cast from 'System.String' to 'Sitecore.Data.ID'.]
System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) +14116726
System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +14116814
Sitecore.ContentSearch.Converters.IndexFieldStorageValueFormatter.ReadFromIndexStorage(Object indexValue, Type destinationType) +609
[InvalidCastException: Could not convert value of type System.String to destination type Sitecore.Data.ID: Invalid cast from 'System.String' to 'Sitecore.Data.ID'.]
Sitecore.ContentSearch.Converters.IndexFieldStorageValueFormatter.ReadFromIndexStorage(Object indexValue, Type destinationType) +869
Sitecore.ContentSearch.DocumentTypeMapInfo.SetProperty(Object target, String propertyName, String documentFieldName, Object value) +172
[InvalidCastException: Could not map index document field to property "ItemId" on type Starco.ISK.Website.Search.PersonSearchItem : Could not convert value of type System.String to destination type Sitecore.Data.ID: Invalid cast from 'System.String' to 'Sitecore.Data.ID'.]
Sitecore.ContentSearch.DocumentTypeMapInfo.SetProperty(Object target, String propertyName, String documentFieldName, Object value) +360
Sitecore.ContentSearch.DefaultDocumentMapper`1.MapFieldValuesToType(IDictionary`2 fieldValues, TElement result, DocumentTypeMapInfo documentTypeMapInfo) +792
Sitecore.ContentSearch.LuceneProvider.DefaultLuceneDocumentTypeMapper.ReadDocumentFields(Document document, IEnumerable`1 fieldNames, DocumentTypeMapInfo documentTypeMapInfo, IEnumerable`1 virtualFieldProcessors, TElement result) +1249
Sitecore.ContentSearch.DefaultDocumentMapper`1.MapToType(TDocument document, SelectMethod selectMethod, IEnumerable`1 virtualFieldProcessors, SearchSecurityOptions securityOptions) +283
Sitecore.ContentSearch.LuceneProvider.<GetSearchResults>d__8.MoveNext() +1577
Starco.ISK.Website.Search.Output.Page_Load(Object sender, EventArgs e) in e:\IIS Data\Starco_intra.starco.com\Website\Search\Output.ascx.cs:76
System.Web.UI.Control.LoadRecursive() +71
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178
Config file for index:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<configuration type="Sitecore.ContentSearch.LuceneProvider.LuceneSearchConfiguration, Sitecore.ContentSearch.LuceneProvider">
<indexes hint="list:AddIndex">
<index id="my_text_Index" type="Sitecore.ContentSearch.LuceneProvider.LuceneIndex, Sitecore.ContentSearch.LuceneProvider">
<param desc="name">$(id)</param>
<param desc="folder">$(id)</param>
<param desc="propertyStore" ref="contentSearch/databasePropertyStore" param1="$(id)" />
<Configuration ref="profileSearch/configuration" />
<strategies hint="list:AddStrategy">
<strategy ref="contentSearch/indexUpdateStrategies/onPublishEndAsync" />
</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/Intranet</Root>
</crawler>
</locations>
</index>
</indexes>
</configuration>
</contentSearch>
</sitecore>
</configuration>
The following is the showconfig.aspx page sitecore/admin/showconfig.aspx :
<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>
Upvotes: 2
Views: 3646
Reputation: 3216
The issue is occurring in the Base class SearchResultItem
when converting a string in the index to a Sitecore guid.
This would point to some rogue data in the index for the itemid field. First thing would be to check in your Search.log file to examine the actual query being run then use Luke to run the query to see the results.
It is likely that an entry in the index has a field (__group) that is not in the format for a Sitecore Guid.
Upvotes: 0
Reputation: 1155
I think the configuration is broken.
The IndexFieldStorageValueFormatter
is unable to find a suitable TypeConverter and falls back to Convert.ChangeType. But the ID class does not have any Conversion Operators for strings so the conversion fails.
It should have found a suitable TypeConverter. It is collected from configuration, by default in the Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration
. It should have a indexFieldStorageValueFormatter
section where this line in particular is of use in this situation:
<converter handlesType="Sitecore.Data.ID, Sitecore.Kernel" typeConverter="Sitecore.ContentSearch.Converters.IndexFieldIDValueConverter, Sitecore.ContentSearch" />
Make sure the contentsearch configuration is correct and that your custom my_text_index
does not override this part.
You could use dotPeek to generate a pdb file and debug the IndexFieldStorageValueFormatter
but I think you'll get to the same conclusion that it does not have a converter available for this type.
Upvotes: 1
Reputation: 525
Try to update as following:
using (var context = ContentSearchManager.GetIndex("my_text_index").CreateSearchContext())
{
IQueryable<Person> query = context.GetQueryable<Person>().Where(p=> p.Firstname == "John");
SearchResults<Person> results = query.GetResults();
foreach(result in results) // I am getting the exception here on this line.
{
lbltest.Text = result.Name;
}
}
Upvotes: 1
Reputation: 4266
Normally that would mean that you are missing a conversion attribute from your property. But none of them are Id's.
It does look like you have the IndexField
attributes set incorrectly tho. You don't need to add the type suffix on to the end. The Sitecore API will do that for you.
So change the Person
class to:
public class Person : SearchResultItem
{
[IndexField("firstname")]
public string Firstname { get; set; }
[IndexField("surname")]
public string Surname { get; set; }
}
Also make sure that the fields are not ID fields in the CMS.
Upvotes: 0