sitecore搜索的基本用法

时间:2022-08-31 23:34:52

I am trying to setup a very basic search index, to index all items in a specific folder. I haven't really used much searching, but I'm trying to use out-of-the-box features, because its a very simple search. I just want to index all the fields. The sitecore documentation really doesn't provide much information - I've read a few blogs, and they all seem to suggest that I need the advanced database crawler (http://trac.sitecore.net/AdvancedDatabaseCrawler) - basically, something to the effect of 'it won't work without a custom crawler).

我正在尝试设置一个非常基本的搜索索引,以索引特定文件夹中的所有项目。我没有真正使用太多的搜索,但我正在尝试使用开箱即用的功能,因为它是一个非常简单的搜索。我只想索引所有字段。 sitecore文档确实没有提供太多信息 - 我已经阅读了一些博客,他们似乎都建议我需要高级数据库爬虫(http://trac.sitecore.net/AdvancedDatabaseCrawler) - 基本上,是如果没有自定义抓取工具,它将无效。

Is this right? I just want to create a simple index, and then start using it. What is the simplest way to do this, without any shared modules or otherwise? I went through the documentation on sitecore, but its not very clear (at least to me). It defines different elements of the index configuration in web.config, but doesn't really explain what they do, and what values are available. Maybe I'm not looking in the right place..

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

4 个解决方案

#1


15  

A simple way of creating new Lucene index in Sitecore with all the items below the specific node in just 3 steps:

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

1: Add the configuration below to the configuration/sitecore/search/configuration/indexes in Sitecore configuration:

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: Rebuild the new index (only one time, all further changes will be detected automatically):

2:重建新索引(仅一次,将自动检测所有进一步的更改):

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

3: Use the new index:

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();
}

Here is a pdf describing Sitecore Search and Indexing.

这是一个描述Sitecore搜索和索引的pdf。

And here is a blog post about Troubleshooting Sitecore Lucene search and indexing.

这是一篇关于Sitecore Lucene搜索和索引排除故障的博客文章。

Here is Lucene query syntax tutorial

这是Lucene查询语法教程

and Introducing Lucene.Net

并介绍Lucene.Net

#2


3  

Sitecore Search Contrib (new name for advanced database crawler) is the best option, you just configure its config in the app config folder to tell it start path database etc.

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

You can then use its API to search within folders, by template type, where a certain field has a certain value. Here is a code example.

然后,您可以使用其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;

//The actual Search

//实际搜索

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;

Otherwise you can just configure the web.config for standard lucene search and use this code to search. (Data base to use "web", start item etc)

否则,您只需配置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


0  

Then you can download Lucene Index Viewer extension for Sitecore to view the index or you can download the Lucene Tool to view the indexes. See if you can populate the documents (files in your indexes). These are called 'Documents' in Lucene and technically these documents are content item present under the node that you specified.

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

#4


-2  

Brian Pedersen has a nice post on it. You would start with a simple crawler. Need to download the Advanced Database Crawler and add the reference to your project after building it.

布莱恩佩德森有一个很好的帖子。您将从一个简单的爬虫开始。需要下载高级数据库爬网程序,并在构建后添加对项目的引用。

Then you have to create the config files which is mentioned in Brian's Blog and you have to copy as it is (except for the template id's n all). You get the point basically here.

然后你必须创建Brian博客中提到的配置文件,你必须按原样复制(除了模板ID的全部)。你基本上得到了这一点。

Then you can download Lucene Index Viewer extension for Sitecore to view the index or you can download the Lucene Tool to view the indexes. See if you can populate the documents (files in your indexes). These are called 'Documents' in Lucene and technically these documents are content item present under the node that you specified.

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

Hope this helps!

希望这可以帮助!

Let me google that for you.

让我谷歌那个给你。

#1


15  

A simple way of creating new Lucene index in Sitecore with all the items below the specific node in just 3 steps:

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

1: Add the configuration below to the configuration/sitecore/search/configuration/indexes in Sitecore configuration:

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: Rebuild the new index (only one time, all further changes will be detected automatically):

2:重建新索引(仅一次,将自动检测所有进一步的更改):

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

3: Use the new index:

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();
}

Here is a pdf describing Sitecore Search and Indexing.

这是一个描述Sitecore搜索和索引的pdf。

And here is a blog post about Troubleshooting Sitecore Lucene search and indexing.

这是一篇关于Sitecore Lucene搜索和索引排除故障的博客文章。

Here is Lucene query syntax tutorial

这是Lucene查询语法教程

and Introducing Lucene.Net

并介绍Lucene.Net

#2


3  

Sitecore Search Contrib (new name for advanced database crawler) is the best option, you just configure its config in the app config folder to tell it start path database etc.

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

You can then use its API to search within folders, by template type, where a certain field has a certain value. Here is a code example.

然后,您可以使用其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;

//The actual Search

//实际搜索

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;

Otherwise you can just configure the web.config for standard lucene search and use this code to search. (Data base to use "web", start item etc)

否则,您只需配置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


0  

Then you can download Lucene Index Viewer extension for Sitecore to view the index or you can download the Lucene Tool to view the indexes. See if you can populate the documents (files in your indexes). These are called 'Documents' in Lucene and technically these documents are content item present under the node that you specified.

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

#4


-2  

Brian Pedersen has a nice post on it. You would start with a simple crawler. Need to download the Advanced Database Crawler and add the reference to your project after building it.

布莱恩佩德森有一个很好的帖子。您将从一个简单的爬虫开始。需要下载高级数据库爬网程序,并在构建后添加对项目的引用。

Then you have to create the config files which is mentioned in Brian's Blog and you have to copy as it is (except for the template id's n all). You get the point basically here.

然后你必须创建Brian博客中提到的配置文件,你必须按原样复制(除了模板ID的全部)。你基本上得到了这一点。

Then you can download Lucene Index Viewer extension for Sitecore to view the index or you can download the Lucene Tool to view the indexes. See if you can populate the documents (files in your indexes). These are called 'Documents' in Lucene and technically these documents are content item present under the node that you specified.

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

Hope this helps!

希望这可以帮助!

Let me google that for you.

让我谷歌那个给你。