Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

c# - Very basic usage of sitecore search

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

<!-- 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):

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

3: Use the new index:

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

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

Here is Lucene query syntax tutorial

and Introducing Lucene.Net


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...