Umbraco and Indexing

流过昼夜 提交于 2019-12-25 03:15:00

问题


I have a Visual Studio project with 2 solutions: Solution 1: UmbracoCms (Umbraco 7.2 code base) Solution 2: SeachIndexer (lucene.net spatial - Windows Console Application)

In my solution 2 I have reference to the following .dlls from the Umbraco solution:

  • UmbracoCms.dll
  • cms.dll
  • businesslogic.ddl
  • umbraco.dll
  • umbraco.DataLayer.dll

In the Program.cs file I have the following code:

Node rootNode = new Node(1103);
string nodeTypeAlias = "articlePage";

if (node.NodeTypeAlias == nodeTypeAlias)
    listNode.Add(node);

foreach (Node childNode in node.Children)
{
    GetDescendantOrSelfNodeList(childNode, nodeTypeAlias);
}

//some other code

When I run the code I get the following error:

could not load the umbraco.core.configuration.umbracosettings.iumbracosettingssection from config file

What I'm trying to do is index Umbraco pages using Lucene.net spatial (Examine does not support spatial) in a seperate solution keeping the Umbraco base code clean. I want to able to schedule the SearchIndexer at 15 mins interval.

What's best way to go about this?


回答1:


You got that kind of error because Umbraco does not see its configurations.

you could have two solutions:

Umbraco Console: it means to recreate the Umbraco environment in a console application. You could see/use this project (e.g.). As you can see, in the App.config has been recreated all the configurations necessary for Umbraco. I've never used it before (it was my first google result), but it seems a good starting point.

Direct access to Umbraco DB: if you don't need to use the Umbraco API extensively, probably it's better to look for your content directly in the Umbraco DB. Obviously you have to explore the Umbraco DB to understand what to look for, and it could be time-comsuming if you don't know Umbraco




回答2:


Instead of using a console project and scheduling it to run every 15 minutes, you could take advantage of Umbraco's Content event handling and index the node every time it's published. This way your index is refreshed straight away, and you don't have to worry about external scheduling etc.

Here's how:

  1. Create a new solution (I normally use an empty Web Project but you can just create a Library project) and install the UmbracoCms.Core nuget package corresponding to your UmbracoCms version (it will install all the necessary dependencies for you including Examine).
  2. Add the Luncene.Net packages as per the tutorial
  3. Add a class deriving from Umbraco.Core.ApplicationEventHandler and override ApplicationStarted - this will give you access to the Umbraco Services as well as the Examine Indexing events - you can use either for this exercise - you may find the Examine Events more suitable.
  4. In your Umbraco project add a reference to your new project so it get's pulled in.

Your EventHandler class may look something like this:

public class SpacialIndexingEventHandler : ApplicationEventHandler
{
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        Umbraco.Core.Services.ContentService.Publishing += ContentService_Publishing;
    }

    private void ContentService_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<IContent> e)
    {
        // For each published node, perform the necessary indexing.
        string nodeTypeAlias = "ArticlePage";
        foreach (var node in e.PublishedEntities.Where(a => a.ContentType.Alias == nodeTypeAlias))
        {
            // Index this

        }
    }
}

If you wanted to do the indexing whenever Umbraco's indexing triggers, use this instead:

public class SpacialDataIndexingEventHandler : ApplicationEventHandler
{
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        // connect to the GatheringNodeData event of the provider you're interested in:
        foreach(var provider in ExamineManager.Instance.IndexProviderCollection.AsEnumerable<BaseIndexProvider>()) {
            if (provider.Name.StartsWith("External")) {
                provider.GatheringNodeData += provider_GatheringNodeData;
                break;
            }
        }
    }

    void provider_GatheringNodeData(object sender, IndexingNodeDataEventArgs e)
    {
        if (e.IndexType == IndexTypes.Content)
        {
            // Get the Node from the ContentService:
            var node = ApplicationContext.Current.Services.ContentService.GetById(e.NodeId);
            // Do your spacial indexing here;
        }
    }
}


来源:https://stackoverflow.com/questions/29979341/umbraco-and-indexing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!