需要设置Sitecore Lucene的示例搜索Sitecore v6,以索引生产数据库

时间:2013-08-16 14:27:53

标签: search lucene sitecore

我搜索了一个简单的英语示例,包含完整的说明和所需的所有代码,以便在运行Sitecore.NET 6.5.0的站点上为我的实时站点数据库设置索引(修订版111230) )。来自Sitecore MVP和Sitecore的官方文档的博客可以解决这个问题,但是我很沮丧,从来没有见过所有代码的完整示例。我是一名初级开发人员,进入这个位置(是的,我知道,你推荐培训 - 好吧,我的雇主认为还不适合支付它)。我使用四种不同的方法尝试了至少四次不同的时间,但我仍然没有能够找到所有实时页面的搜索索引。 Sitecore的官方文档不完整,因此没有帮助。所以我转向这个社区。请帮忙。

(在第一次评论后编辑完善问题)我知道这篇文章Very basic usage of sitecore search但是仍然坚持第2步。那里有一行代码,但是我该怎么做呢?我很抱歉看起来很无知,但我不知道该怎么做,而且没有任何文件可以达到这个细节水平。如果不在这里,那么我在哪里可以找到这些信息?

编辑2:这是我配置索引的方式。

<!-- id must be unique -->
          <index id="milo-live-index" type="Sitecore.Search.Index, Sitecore.Kernel">
            <!-- name - not sure if necessary but use id and forget about it -->
            <param desc="name">$(id)</param>
            <!-- folder - name of directory on the hard drive -->
            <param desc="folder">__milo-live-index</param>
            <!-- analyzer - reference to analyzer defined in Sitecore.config -->
            <Analyzer ref="search/analyzer" />
            <!-- list of locations to index - each of the with unique xml tag -->
            <locations hint="list:AddCrawler">
              <!-- first location (and the only one in this case) - specific folder from you question -->
              <!-- type attribute is the crawler type - use default one in this scenario -->
              <specificfolder type="Sitecore.Search.Crawlers.DatabaseCrawler,Sitecore.Kernel">
                <!-- indexing itmes from production database -->
                <Database>production</Database>
                <!-- your folder path -->
                <Root>/sitecore/content/homeowners</Root>
                </specificfolder>
              </locations>

我转到sitecore桌面,控制面板,数据库/重建搜索索引,我看到的唯一索引就是“快速搜索”。

1 个答案:

答案 0 :(得分:3)

以下是示例搜索索引配置。你应该将它放在

部分的Web.config(在配置/索引下)中
<index id="help_pages_index" type="Sitecore.Search.Index, Sitecore.Kernel">
    <param desc="name">$(id)</param>
    <param desc="folder">help_pages_index</param>
    <Analyzer ref="search/analyzer" />
    <locations hint="list:AddCrawler">
        <web-help type="Sitecore.Search.Crawlers.DatabaseCrawler,Sitecore.Kernel">
            <Database>web</Database>
            <Root>/sitecore/content/Pages/Help</Root>
            <templates hint="list:IncludeTemplate">
                <helparticle>{FF48919D-393C-4F8D-9D1A-AC6B58CEC896}</helparticle>
                <helpfaq>{E66F97CF-18CF-4D73-AC84-0064D7626524}</helpfaq>
            </templates>
        </web-help>
    </locations>
</index>

此配置创建新索引,使用两个指定模板之一(helparticle,helpfaq)索引根项目(/ sitecore / content / Pages / Help)中的项目。

将此文件放到Web.config后,您可以创建简单的aspx页面,并在Page_Load中输入:

SearchManager.GetIndex("help_pages_index").Rebuild();

执行此页面一次。它应该重建您的索引,它应该显示在控制面板中。或者,您可以考虑升级到Sitecore 6.5的最新版本。在最新版本中,索引应自动添加到控制面板。

然后您可以使用代码中的索引。这个例子比较先进,因为它也会从lucene获得分数。

public class SearchHelper
{
public static SearchResults SearchHelp(string searchExpression, int start, int end)
{
    var searchResults = new SearchResults { SearchPhrase = string.Empty, TotalHits = 0, Items = new List<SearchResultItem>() };

    if (!string.IsNullOrEmpty(searchExpression)) 
    {
        searchResults.SearchPhrase = searchExpression;

        var searchIndex = SearchManager.GetIndex("help_pages_index");
        if (searchIndex != null) 
        {
            using (var searchContext = searchIndex.CreateSearchContext()) 
            {
                var queryParser = new QueryParser(BuiltinFields.Content, searchIndex.Analyzer);
                queryParser.SetDefaultOperator(QueryParser.Operator.AND);

                var fullTextQuery = queryParser.Parse(QueryParser.Escape(searchExpression.ToLowerInvariant()));

                var languageQuery = new PhraseQuery();
                languageQuery.Add(new Term(BuiltinFields.Language, Sitecore.Context.Language.Name.ToLowerInvariant()));

                var innerQuery = new BooleanQuery();
                innerQuery.Add(fullTextQuery, BooleanClause.Occur.MUST);
                innerQuery.Add(languageQuery, BooleanClause.Occur.MUST);

                var hits = searchContext.Searcher.Search(innerQuery);

                var hitsLength = hits.Length();
                searchResults.TotalHits = hitsLength;
                if (hitsLength > 0 && start < hitsLength) 
                {
                    if (end >= hitsLength) 
                    {
                        end = hitsLength - 1;
                    }

                    for (var i = start; i <= end; i++) 
                    {
                        var url = hits.Doc(i).Get(BuiltinFields.Url);

                        if (!string.IsNullOrEmpty(url)) 
                        {
                            var item = Database.GetItem(ItemUri.Parse(url));

                            if (item != null && !item.Empty) 
                            {
                                searchResults.Items.Add(new SearchResultItem { Item = item, Score = hits.Score(i) });
                            }
                        }
                    }
                }
            }
        }
    }

    return searchResults;
}

}