1

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

(编辑以完善问题,在第一次评论之后)我知道这篇文章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桌面,控制面板,数据库/重建搜索索引,我看到的唯一索引是“快速搜索”。

4

1 回答 1

3

Here is example search index configuration. You should put this inside Web.config (under configuration/indexes) in section

<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>

This configuration creates new index which indexes items inside root item (/sitecore/content/Pages/Help) with one of two specified templates (helparticle, helpfaq).

After you put this to Web.config you can create simple aspx page and in Page_Load put this:

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

Execute this page once. It should rebuild your index and it should show in control panel. Alternatively you could consider to upgrade to latest version of Sitecore 6.5. In latest version index should be added automatically to control panel.

Then you can use your index from code. This example is more advanced, because it gets also hits score from 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;
}

}

于 2013-08-17T09:28:47.657 回答