1

Background: Sitecore 8.2.6, SOLR 5.4.1

I need to use class indexer to resolve field name for SOLR.

To simplify issue description let me provide next code snippet:

var fieldName = ..method parameter.ToLowerInvariant();
var products = context.GetQueryable<SearchItemResult>()
                    .Where(x => x[fieldName] != null)
                    .ToList().AsEnumerable();

For example, fieldName value that is passed to predicate is "shape". Index actually contains field shape_s.

The issue is such - when I use standard sitecore_web_index, in Search.log I can observe query where this shape is actually resolved to shape_s, and query does its job. But when I switch to custom sitecore_products_index which is based on web database and indexes just specific set of templates via <include hint="list:AddIncludedTemplate">, in Search.log I get error message '["undefined field shape"]'.

As far as I can think after all my experiments the only superficial difference is that web index contains all the templates, but custom product index contains reduced set of them.

If I inherit SearchItemResult to my custom class and define some property decorated with IndexField attribute, everything works fine if I use this approach. But in this specific task I have to use dynamic field name passed as parameter, so I want to use C# indexer for a SearchItemResult class to resolve this into SOLR's field name.

4
  • 1
    What does your index config look like?
    – Gatogordo
    Jul 30, 2018 at 18:08
  • Actually it is a copy of sitecore/contentSearch/indexConfigurations/defaultSolrIndexConfiguration node with some set of templates specified
    – The Yur
    Jul 30, 2018 at 18:30
  • is the field "shape" a computed field or it is present on the item in sitecore?
    – adarsh
    Jul 31, 2018 at 12:41
  • It is a field in sitecore item
    – The Yur
    Aug 2, 2018 at 15:31

1 Answer 1

5

Based on previous projects, when using Where(x => x[fieldName] != null), the fieldName should match the one from the Solr Index. From your question, if you pass Where(x => x["shape_s"] != null), it should work.

Since you have dynamic fields being passed to the search context, you will need to get the field type. You can get it by using

var fieldNameWithType = context.Index.FieldNameTranslator.GetIndexFieldName("shape")

Solr will return the value shape_s. While debugging, you can see in the dictionary typeFieldMap the list of all fields how they are in the Solr Index.

Your code will look like below

var fieldName = ..method parameter.ToLowerInvariant();
var fieldNameWithType = context.Index.FieldNameTranslator.GetIndexFieldName(fieldName)
var products = context.GetQueryable<SearchItemResult>()
                .Where(x => x[fieldNameWithType] != null)
                .ToList().AsEnumerable();
1
  • 2
    Many thanks @Hishaam! I was thinking about this kind of solution but did not find it because of lack of search API knowledge. Actually I've made workaround to the issue in different way - I've added field for 'shape' into index config file, just like this: <fieldNames hint="raw:AddFieldByFieldName"> .... <field fieldName="shape" returnType="string" /> .... </fieldNames> and it started get recognized by Solr.
    – The Yur
    Aug 2, 2018 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.