How do I search for multiple terms in the Sitecore 7 ContentSearch API?

I’m learning the new Sitecore.ContentSearch LINQ to Sitecore.ContentSearch API in Sitecore 7. I understand that Sitecore recommends using the new LINQ API on top of the existing Sitecore.Search API, but I am struggling to make even the simplest of the queries.

Take, for example, the following search query: "hello world" .

Using the Sitecore.Search API, the terms “hello world” are usually passed through QueryParser , which will result in documents matching the word “hello” OR “peace”. Documents containing both conditions will be rated higher than those with only one.

How to execute the same query using LINQ?

Here is what I tried:

 var results = SearchContext.GetQueryable<MyResultItem>(); var terms = searchTerm.Split(' '); // Not supported exception results = results.Where(r => terms.Any(t => r.Content.Contains(r))); // Close, but performs an "AND" between terms, and does not appear // to score documents properly foreach (var term in terms) { results = results.Where(r => r.Content.Contains(t)); } 

UPDATE

I am convinced that I am missing something very simple. Of course, with all the work that went into the new search API, this simple use case was not missed ... right?

As a workaround, I tried to open sitecore_web_index by default using the existing SearchManager, however this will not work.

Unfortunately, I had to resort to the existing API until I could figure it out. I will definitely update this question with my findings.

UPDATE 2

I found the Sitecore.ContentSearch.Utilities.LinqHelper class, which partially solves the problem. You can use it to dynamically build a query similar to BooleanQuery in Lucene.Net, however its parameters are limited and this adds a bit of overhead.

+6
source share
3 answers

All the predicate creators I tried did not work, however Sitecore 7 comes with its own PredicateBuilder , which did the trick.

 using Sitecore.ContentSearch; using Sitecore.ContentSearch.Linq; using Sitecore.ContentSearch.SearchTypes; using Sitecore.ContentSearch.Utilities; // Sitecore 7 (Update 1+): using Sitecore.ContentSearch.Linq.Utilities; ... var index = ContentSearchManager.GetIndex("sitecore_web_index"); using (var context = index.CreateSearchContext()) { var predicate = PredicateBuilder.True<SearchResultItem>(); foreach (var t in term.Split(' ')) { var tempTerm = t; predicate = predicate.Or(p => p.Content.Contains(tempTerm)); } var results = context.GetQueryable<SearchResultItem>().Where(predicate).GetResults(); ... } 
+21
source

I think this is due to linq, not sitecore.

I am not testing this, but I will look at this article http://www.albahari.com/nutshell/predicatebuilder.aspx

You can also see this documentation http://sdn.sitecore.net/upload/sitecore7/70/developer's_guide_to_item_buckets_and%20search_sc7-a4.pdf

+1
source

I was able to use PredicateBuilder with Solr and implement queries, including the OR operator. See http://www.nttdatasitecore.com/en/Blog/2013/November/Building-Facet-Queries-with-PredicateBuilder.aspx

0
source

Source: https://habr.com/ru/post/947916/


All Articles