4

I have requirements to create a search interface that allows the end user to perform faceted search against an index. One of the requirements is to be able to do a range query against certain fields (that are configured at runtime by the content owner). The search is executed as part of a REST call and the results are returned to the front end for display.

Under normal circumstances, one would do a range query using the .Between() method in LINQ, like so:

var productQuery = PredicateBuilder.True<SearchResultItem>();
productQuery = productQuery.And(x => x[filterField].Between(lowerLimit, upperLimit, Inclusion.Both));

When I create the above predicate and pass it to the search context:

using (var context = ContentSearchManager.GetIndex(string.Format(IndexBaseName, Sitecore.Context.Database.Name)).CreateSearchContext())
{
    var searchResults = context.GetQueryable<SearchResultItem>().Where(productQuery).ToList(); 
}

The above throws a System.NotSupportedException with the message "Unsupported expression node type: ArrayIndex". After a bit of Googling, it appears that this might be a limitation of LINQ, which explains why most examples of calling .Between() don't use SearchResultItem and instead use a custom model where the range field is mapped to a property. I'm my case, since the fields can vary based on how the content editor configures which fields are used for these range queries, I need to find a way around this.

  • Can you try the following for the predicate: predicate.Or(x => x[filterField]).Between(lowerLimit, upperLimit, Inclusion.Both)); – Hishaam Namooya May 22 '18 at 18:56
  • That doesn't work since .Or is going to want a lambda function that returns a bool. Also predicate isn't defined. – Derek Dysart May 22 '18 at 19:48
2

So in simplifying my code for posting as a question here, I left out the fact that lowerLimit and upperLimit above were actually values coming from an arrary and were actually:

productQuery = productQuery.And(x => x[filterField].Between(range[0],range[1], Inclusion.Both));

Which is the actual source of the "Unsupported expression node type: ArrayIndex" message (which is exactly the issue in the linked Stack Overflow question.)

  • is this a solution to the question or an update? If its an update can you update the question, if its an answer can you clarify what the solution is please? – Richard Seal May 22 '18 at 21:02
  • 1
    I had to wait in order to mark my answer my own question as accepted. – Derek Dysart May 29 '18 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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