简体   繁体   中英

How to you create a custom index in Sitecore 7 to index non Sitecore items (f. ex. Users)

I want to index the Sitecore Users which are not Sitecore Items.

The same way if I want to create custom indexes that combine different fields from different Sitecore items how do I do that. Assuming I have the data returned by some kind of data provider/repository, how I create, configure, index this data.

I have already written some configuration for this index:

<configuration type="Sitecore.ContentSearch.LuceneProvider.LuceneSearchConfiguration, Sitecore.ContentSearch.LuceneProvider">
    <indexes hint="list:AddIndex">
      <index id="custom_user_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)" />-->

        <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="PT.Forum.Framework.Users.Infrastructure.UserCrawler, PT.Forum.Framework.Users"/>
        </locations>                
        </index>
    </indexes>  
  </configuration>

My crawler does this:

public class UserCrawler : AbstractProviderCrawler
{
    public override void Initialize(ISearchIndex index)
    {
      Assert.ArgumentNotNull(index, "index");
      Database = "web";

      Assert.IsNotNull(Database, "Database element not set.");
      Assert.IsNotNull(Root, "Root element not set.");
      base.Initialize(index);
      LuceneIndex index2 = index as LuceneIndex;
      if (Operations == null)
      {
        Operations = new LuceneIndexOperations(index2);
        CrawlingLog.Log.Info(string.Format("[Index={0}] Initializing LuceneDatabaseCrawler. DB:{1} / Root:{2}", index.Name, Database, Root));
      }
      var users = new UserRepository().GetByConstraint(UserRepositoryContraints.IsNejTakPlusUser);
      index.Configuration = new LuceneIndexConfiguration();
      var updateContext = index.CreateUpdateContext();

    Operations.Add(new ForumUser(users.First()), updateContext, index.Configuration);
    }
}

But the Index configuration is null.

The configuration above uses the DefaultIndexConfiguration which includes several computed fields. Try using an own configuration instead and adding just the fields/computed fields/other settings you need for your index.

Example how to use a custom configuration:

<customSearch>
  <myCustomConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">

    <indexAllFields>false</indexAllFields>

    <!-- include parts of default configuration -->
    <Analyzer ref="contentSearch/configuration/defaultIndexConfiguration/analyzer" />

    <!-- more settings here -->

   </myCustomConfiguration>
</customSearch>
<contentSearch>
   <configuration type="Sitecore.ContentSearch.LuceneProvider.LuceneSearchConfiguration, Sitecore.ContentSearch.LuceneProvider">
   <indexes hint="list:AddIndex">
       <index id="custom_user_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" aram1="$(id)" />-->

       <Configuration ref="customSearch/MyCustomConfiguration" />

       <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="PT.Forum.Framework.Users.Infrastructure.UserCrawler, PT.Forum.Framework.Users"/>
       </locations>                
    </index>
   </indexes>  
   </configuration>
</contentSearch>

If you are going to use incremental updates, you will also need to make sure, Sitecore can match existing documents in lucene. By default, this is done using Sitecore fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM