【问题标题】:Very basic usage of sitecore search站点核心搜索的非常基本的用法
【发布时间】:2013-06-24 10:19:09
【问题描述】:

我正在尝试设置一个非常基本的搜索索引,以索引特定文件夹中的所有项目。我并没有真正使用太多搜索,但我正在尝试使用开箱即用的功能,因为它是一个非常简单的搜索。我只想索引所有字段。 sitecore 文档确实没有提供太多信息 - 我读过一些博客,它们似乎都建议我需要高级数据库爬虫 (http://trac.sitecore.net/AdvancedDatabaseCrawler) - 基本上,大意是“它不会在没有自定义爬虫的情况下工作)。

这是对的吗?我只想创建一个简单的索引,然后开始使用它。没有任何共享模块或其他方式,最简单的方法是什么?我浏览了 sitecore 上的文档,但不是很清楚(至少对我而言)。它在 web.config 中定义了索引配置的不同元素,但并没有真正解释它们的作用以及可用的值。也许我没有找对地方..

【问题讨论】:

  • 如果您可以选择 Sitecore 7 this document 应该会有所帮助。
  • 请看我的帖子 M.R.
  • @M.R.我在 Sitecore 中包含了对 Lucene 索引配置的说明以及用于从自定义索引中获取项目的工作代码。

标签: c# asp.net content-management-system sitecore sitecore6


【解决方案1】:

Sitecore 中创建新的 Lucene 索引的简单方法,只需 3 个步骤即可将特定节点下的所有项目:

1:将以下配置添加到Sitecore配置中的configuration/sitecore/search/configuration/indexes

<!-- id must be unique -->
<index id="my-custom-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">__my-custom-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 master database -->
      <Database>master</Database>
      <!-- your folder path -->
      <Root>/sitecore/content/home/my/specific/folder</Root>
    </specificfolder>
  </locations>
</index>

2:重建新索引(只需一次,所有进一步的变化都会自动检测到):

SearchManager.GetIndex("my-custom-index").Rebuild();

3:使用新索引:

// use id of from the index configuration
using (IndexSearchContext indexSearchContext = SearchManager.GetIndex("my-custom-index").CreateSearchContext())
{
    // MatchAllDocsQuery will return everything. Use proper query from the link below
    SearchHits hits = indexSearchContext.Search(new MatchAllDocsQuery(), int.MaxValue);
    // Get Sitecore items from the results of the query
    List<Item> items = hits.FetchResults(0, int.MaxValue).Select(result => result.GetObject<Item>()).Where(item => item != null).ToList();
}

这是描述Sitecore Search and Indexing的pdf。

这是一篇关于 Troubleshooting Sitecore Lucene search and indexing 的博文。

这里是Lucene query syntax tutorial

Introducing Lucene.Net

【讨论】:

  • 是的,浏览了该文件(如问题中所述)。这对我来说不是很清楚,因此提出了这个问题。
【解决方案2】:

Sitecore Search Contrib(高级数据库爬虫的新名称)是最好的选择,您只需在 app config 文件夹中配置它的配置,告诉它启动路径数据库等。

然后,您可以使用其 API 按模板类型在文件夹中搜索,其中特定字段具有特定值。这是一个代码示例。

MultiFieldSearchParam parameters = new MultiFieldSearchParam();

parameters.Database = "web";
parameters.InnerCondition =  QueryOccurance.Should;
parameters.FullTextQuery = searchTerm;        
parameters.TemplateIds = array of pipe seperated ID's

var refinements = Filters.Select(item => new MultiFieldSearchParam.Refinement(item.Value, item.Key.ToString())).ToList();

parameters.Refinements = refinements;

//实际搜索

var returnItems = new List<Item>();
var runner = new QueryRunner(IndexName);
var skinnyItems = runner.GetItems(new[] {parameters});
skinnyItems.ForEach(x => returnItems.Add(Database.GetItem(new ItemUri(x.ItemID))));
return returnItems;

否则,您可以只配置 web.config 以进行标准 lucene 搜索并使用此代码进行搜索。 (使用“web”的数据库,启动项等)

public Item[] Search(string searchterms)
        {
            var children = new List<Item>();

            var searchIndx = SearchManager.GetIndex(IndexName);

            using (var searchContext = searchIndx.CreateSearchContext())
            {
                var ftQuery = new FullTextQuery(searchterms);
                var hits = searchContext.Search(ftQuery);
                var results = hits.FetchResults(0, hits.Length);

                foreach (SearchResult result in results)
                {
                    if (result.GetObject<Item>() != null)
                    {
                        //Regular sitecore item returned       
                        var resultItem = result.GetObject<Item>();

                        if (ParentItem == null)
                        {
                            children.Add(resultItem);
                        }
                        else if (resultItem.Publishing.IsPublishable(DateTime.Now, false) &&
                                 ItemUtilities.IsDecendantOfItem(ParentItem, resultItem))
                        {
                            children.Add(resultItem);
                        }
                    }
                }
            }
            return children.ToArray();
        }

【讨论】:

    【解决方案3】:

    然后您可以下载 Sitecore 的 Lucene Index Viewer 扩展来查看索引,或者您可以下载 Lucene Tool 来查看索引。看看您是否可以填充文档(索引中的文件)。这些在 Lucene 中称为“文档”,从技术上讲,这些文档是您指定的节点下存在的内容项。

    【讨论】:

      【解决方案4】:

      Brian Pedersen 有一篇很好的帖子。您将从一个简单的爬虫开始。需要下载 Advanced Database Crawler 并在构建后添加对您项目的引用。

      然后您必须创建 Brian 的博客中提到的配置文件,并且您必须照原样复制(除了模板 id 的全部)。你基本上明白了这里的意思。

      然后您可以下载 Sitecore 的 Lucene Index Viewer 扩展来查看索引,或者您可以下载 Lucene Tool 来查看索引。看看您是否可以填充文档(索引中的文件)。这些在 Lucene 中称为 “文档”,从技术上讲,这些文档是您指定的节点下存在的内容项。

      希望这会有所帮助!

      Let me google that for you.

      【讨论】:

      • 不是我的反对意见,而是一些见解:由于 Sitecore 中 Lucene.NET 的本机实现版本不匹配,Lucene 索引查看器在 Sitecore 6.5+ 上无法正常运行。至于对 Advanced Database Crawler 的需求,嗯,真的没有。 OP 要求一个简单的实现,而使用 Advanced Database Crawler 将用于更多自定义和非常规的搜索索引方法 - 普通用户不需要它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多