Linq没有使用sitecore solr

时间:2016-06-14 11:12:11

标签: linq solr sitecore

我正在使用sitecore和solr显示搜索结果。场景是如果用户搜索某些内容,搜索代码将首先检查该关键字是否在"关键字" sitecore项目的字段,如果它发现它将显示结果,如果找不到,它将在标题和描述字段中检查该关键字。

现在的问题是 -
尽管关键字字段中存在值,但下面的条件始终为false且永远不会给出结果。

var Query = searchContext.GetQueryable<SearchResultItem>()
                .Where(i => (i["keywords"].Contains(SearchQuery)));

其中标题和说明的相同查询工作正常

var Query2 = searchContext.GetQueryable<SearchResultItem>()
                 .Where(i => (i["title"].Contains(SearchQuery) 
                          || i["description"].Contains(SearchQuery)));

对于每个sitecore项目,我都有标题,说明和关键字字段。

以下是详细代码段。

public class SearchModel
{
    public string SearchQuery { get; set; }

    List<WebSiteSearchResult> lst = new List<WebSiteSearchResult>();

    public IEnumerable<WebSiteSearchResult> SiteSearchResult()
    {
        var searchContext = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext();
        var Query = searchContext.GetQueryable<SearchResultItem>().Where(i => (i["keywords"].Contains(SearchQuery)));
        var result = Query.GetResults();
        if (result.TotalSearchResults != 0)
        {
            //some operations
        }

        else
        {
            var Query2 = searchContext.GetQueryable<SearchResultItem>().Where(i => (i["title"].Contains(SearchQuery) || i["description"].Contains(SearchQuery)));
            var result2 = Query2.GetResults();
            if (result2.TotalSearchResults != 0)
            {
                //some operations
            }
        }

        lst = lst.OrderBy(i => i.itemBoostingRatio).ToList();

        return lst;
    }
}

public class WebSiteSearchResult
{
    public string itemKeywords { get; set; }
    public int itemBoostingRatio { get; set; }
    public string itemTitle { get; set; }
    public string itemDescription { get; set; }
}

而且,这是我的sitecore项目:http://i.stack.imgur.com/liw59.png

1 个答案:

答案 0 :(得分:3)

鉴于您的Sitecore项目关键字位于SLT字段(&#34;贷款,汽车贷款,自行车贷款和#34;),请记住此字段是标记化索引。这意味着在存储处理期间,查询将使用空格和标点字符作为分隔符分割为标记,如下所示:

enter image description here enter image description here
现在,如果我们使用EqualsContains进行搜索,搜索日志将为我们提供适当的序列化查询以在solr中进行测试:

等于?q=keywordstest_t:("bike loan")

  • 这告诉我们在列表中找到与此给定值匹配的任何术语。如果我们的字词是"scooter loan",我们就不会得到任何结果。

包含?q=keywordstest_t:(*bike loan*)

  • 这告诉我们返回任何结果项,其中列表中的任何术语都包含任何单词/子词。这不是搜索效果。如果我们的字词是"scooter L",我们会因为 L 而得到结果。


要检查的几件事

: 此值是否在Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.config

中设置为true

&#13;
&#13;
<indexAllFields>true</indexAllFields>
&#13;
&#13;
&#13;

要获得最佳做法,请创建一个继承自Sitecore基础SearchResultItem的新搜​​索结果项类,以便您可以使用此实例直接调用字段属性&#39; :

public class BasePageSearchResultItem : SearchResultItem
{
    [IndexField("keywords")]
    public string Keywords { get; set; }
}


Equals查询应该用于在索引字段中查找一个这些字词:

var Query = searchContext.GetQueryable<SearchResultItem>()
            .Where(i => i.Keywords.Equals(SearchQuery));

结果集取决于搜索查询"scooter loan"规定的要求,并提供了示例项的关键字字段值"Loans, Car Loan, Bike Loan"

  1. 分别搜索滑板车贷款,其中每个搜索查询字词完全匹配或包含在索引字词中
  2. 搜索踏板车贷款作为一个整体