How to perform search for multiple terms in Sitecore 7 ContentSearch API?

时光总嘲笑我的痴心妄想 提交于 2020-01-22 12:32:05

问题


I am exploring the new Sitecore.ContentSearch "LINQ to Sitecore" API in Sitecore 7. It is my understanding that Sitecore recommends using the new LINQ API over the existing Sitecore.Search API, however, I am struggling to perform even the simplest of queries.

Take for instance the following search query: "hello world".

Using the Sitecore.Search API, the terms "hello world" would typically be passed through a QueryParser which would result in documents matching the word "hello" OR "world". Documents containing both terms would be scored higher that those with just one.

How does one perform this same query using LINQ?

Here's what I have 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 really simple. Surely with all the work that went into the new search API, this simple use case wasn't overlooked... right?

As a workaround, I tried opening the default sitecore_web_index using the existing SearchManager, however, this does not work.

Unfortunately, I have had to resort to the existing API until I can figure this out. I will be sure to 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 a BooleanQuery in Lucene.Net, however, it's options are limited and it adds a bit of performance overhead.


回答1:


All of the predicate builders I tried didn't work, however, Sitecore 7 ships with a PredicateBuilder of it's own that 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();

    ...
}



回答2:


I think this is related to linq not to sitecore.

I don't test this but have a look at this article http://www.albahari.com/nutshell/predicatebuilder.aspx

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




回答3:


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



来源:https://stackoverflow.com/questions/17257392/how-to-perform-search-for-multiple-terms-in-sitecore-7-contentsearch-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!