1

I have two IQueryables that i need to merge. This is my code:

enter image description here

such that all results having region name "UK" will appear on top while all results having region other than UK will appear after search results of "UK" When I am trying to run the same getting this issue : enter image description here

Please let me know if there is any other query or way which I should try to get expected output

Below is the screenshot of my updated code : enter image description here But this code is not working working as while building the same I am facing this error: cannot convert from 'System.Collections.Generic.List>' to 'Sitecore.ContentSearch.Linq.SearchResults'
This issue is occuring while I am trying to pass finalList in funtion MapResults()(Declaration of MapResults funtion is as : IEnumerable MapResults(SearchResults hits))

Requirement for the same is that I want to get all job results base on UK region on top and then rest of the job results after that. Please let me know if there is any suggestion on the same.

0

LINQ to Sitecore provides access to search the indexes, using standard LINQ queries in the same way that other LINQ providers, such as LINQ to SQL and LINQ to Objects, work. It uses the standard IQueryable interface and supports most of the available operations. But the LINQ layer does not implement all IQueryable methods. below methods are not supported:

  • Average
  • Concat
  • Facets
  • FirstOrDefault
  • GroupBy
  • Intersect
  • LastOrDefault
  • Match
  • OrderByDescending
  • Reverse
  • SingleOrDefault
  • SkipWhile
  • Sum
  • TakeWhile

If these methods are called, a NotSupportedException or InvalidOperationException exception is thrown at runtime. Based on your requirement you may need to fire two queries or need to perform logic after getting the results.

| improve this answer | |
  • Could you please provide an example for the same as i have already tried writing two queries case but didn't work for me – Arpana Mittal Jun 13 at 8:12
  • I am getting this error Ordering can only be done on 'Sitecore.ContentSearch.Linq.Nodes.FieldNode' while using this query: query = Sort(filter, BuildQuery(filter, context, expiryWindow)) .Page(pagingOptions.Page - 1, pagingOptions.Size).OrderByDescending(m => m.region_names.Contains("US")).ThenBy(m => m); – Arpana Mittal Jun 13 at 10:34
0

As specified by the documentation found here https://doc.sitecore.com/developers/90/sitecore-experience-manager/en/linq-to-sitecore.html, the concat method is not supported.

You can rather do a single query where the region_names is not empty and select the items you want from the result.

query = query.Where(i => !string.IsNullOrEmpty(i.region_names));
var results = query.GetResults();

var finalResult = results.Hits.Where(i => i.Document.region_names.Contains("UK")).ToList();
finalResult.AddRange(results.Hits.Where(i => !i.Document.region_names.Contains("UK")).ToList());

Update: For pagination feature you can use skip and take operations on the finalResult list.

var newList = finalResult.Skip(x).Take(y);
  • x will be the number of contents you want to skip
  • y will be the number of contents to display per page
| improve this answer | |
  • Is the return type of the function MapResults IAsyncEnnumerable? If yes, please refer to this question stackoverflow.com/questions/59380470/… I will update the answer for the pagination – adarsh Jun 13 at 14:07
  • Try this instead. IReadOnlyList<JobSearchResult> bx = MapResults(finalResult).ToList().AsReadOnly(); – adarsh Jun 13 at 15:37
  • Can you please update your question to add the custom code you wrote for your requirement? – adarsh Jun 14 at 6:37
0

I recently implemented this type of requirement in Commerce and same can be used here also.

Use the first predicate to get UK results

query1 = query.Where(i => i.region_names.Contains("UK"));
var results = query1.GetResults();

Use the second predicate to get non UK results

query2 = query.Where(i => !i.region_names.Contains("UK"));
var results = query2.GetResults();

Finally perform Union of the results

var results3 = results1.Union(results2);
SearchResults<SearchResultItem> fianlResults = new SearchResults<SearchResultItem>(results3, results3.Count());

This should give you the expected output. Please let me know if you face any issues

You can refer to my blog here for more details : https://codeandlearnspot.wordpress.com/2020/04/05/sitecore-solr-search-improve-relevancy-boosting-keyword-splitting/

| improve this answer | |

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.