Very basic usage of sitecore search
Asked Answered
F

4

7

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

Farceuse answered 27/6, 2013 at 3:57 Comment(3)
If you have the option to choose Sitecore 7 this document should help.Mokpo
Please see my post M.R.Seda
@Farceuse I've included explanation of Lucene index configuration in Sitecore and working code for getting items from the custom indexes.Trey
T
15

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

Trey answered 27/6, 2013 at 8:1 Comment(1)
Yes, went through that document (as mentioned in the question). It wasn't very clear to me, hence the question.Farceuse
G
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.

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.

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)

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();
        }
Gelsemium answered 1/7, 2013 at 15:3 Comment(0)
K
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.

Kept answered 29/3, 2017 at 11:42 Comment(0)
S
-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.

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.

Hope this helps!

Let me google that for you.

Seda answered 27/6, 2013 at 19:51 Comment(1)
Not my downvote, but some insight: Lucene index viewer doesn't play well with sitecore 6.5+ due to a version mismatch in the native implementation of Lucene.NET in Sitecore having been upgraded. As for the need for Advanced Database Crawler, well, there really isn't one. OP is asking for a simple implementation, whereas utilizing Advanced Database Crawler would be for more custom and unconventional search indexing approaches - the average user doesn't need it.Bolduc

© 2022 - 2024 — McMap. All rights reserved.