how to create search function in sitecore

2019-08-18 02:42发布

I am trying to setup a very basic search index, to index all items in a specific folder. but I'm fail to do it. Please, check my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sitecore.Data.Items;
using Sitecore.ContentSearch;
using Sitecore.Search;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.ContentSearch.Linq;
using Sitecore.Data;

protected void Page_Load(object sender, EventArgs e)
{
     var Index = ContentSearchManager.GetIndex("sitecore_web_index");

     using (var Context = Index.CreateSearchContext())
     {
           IQueryable<SearchResultItem> query = 
                       Context.GetQueryable<SearchResultItem>()
                       .Where(x => x.TemplateName.Contains("main"));

            SearchResults<SearchResultItem> results = query.GetResults();

            if (results.Hits.Any())
            {
                rpNewsListing.DataSource =
                      results.Hits.Select(hit => hit.Document.GetItem());

                rpNewsListing.DataBind();

                //rpNewsListing.DataSource = results.Hits.Select(x => x.Document);
                // Extract the Document for each hit - this is the NewsResult object

                //rpNewsListing.DataBind();
            }    
      }      
}

Is this right? I just want to create a simple index, and then start using it.

标签: sitecore
2条回答
看我几分像从前
2楼-- · 2019-08-18 03:00

With the default indexes Sitecore provides, it is not necessary to create a custom index yourself, unless you have very specific needs. The default indexes contain everything from your Sitecore tree. A custom index needs to be configured in the config files, however, you can extend the existing default indexes with custom fields if you like.

The code you provide is correct in getting search results from the standard Sitecore indexes.

查看更多
Luminary・发光体
3楼-- · 2019-08-18 03:12

I suppose you are using the statment TemplateName.Contains("main") to try to filter by this specific folder you mentioned.

Yes, you could create a specific index which will only index items from a given folder. The simplest way would be to copy the sitecore_web_index definition and create your own changing the root node inside crawler.

Like this:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <configuration type="Sitecore.ContentSearch.LuceneProvider.LuceneSearchConfiguration, Sitecore.ContentSearch.LuceneProvider">
        <indexes hint="list:AddIndex">

          <index id="sitecore_MYFOLDER_index" type="Sitecore.ContentSearch.LuceneProvider.LuceneIndex, Sitecore.ContentSearch.LuceneProvider">

            <param desc="name">$(id)</param>
            <param desc="folder">$(id)</param>
            <!-- This initializes index property store. Id has to be set to the index id -->
            <param desc="propertyStore" ref="contentSearch/databasePropertyStore" param1="$(id)" />
            <strategies hint="list:AddStrategy">
              <!-- NOTE: order of these is controls the execution order -->
              <strategy ref="contentSearch/indexUpdateStrategies/onPublishEndAsync" />
            </strategies>
            <commitPolicyExecutor type="Sitecore.ContentSearch.CommitPolicyExecutor, Sitecore.ContentSearch">
              <policies hint="list:AddCommitPolicy">
                <policy type="Sitecore.ContentSearch.TimeIntervalCommitPolicy, Sitecore.ContentSearch" />
              </policies>
            </commitPolicyExecutor>
            <locations hint="list:AddCrawler">
              <crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
                <Database>web</Database>

                <Root>/sitecore/MYFOLDER</Root>

              </crawler>
            </locations>
          </index>
        </indexes>
      </configuration>
    </contentSearch>
  </sitecore>
</configuration>

Notice I only change the index name to *sitecore_MYFOLDER_index* and the root to /sitecore/MYFOLDER.

Then you create your content search using that new index definition

var Index = ContentSearchManager.GetIndex("sitecore_MYFOLDER_index");
查看更多
登录 后发表回答