Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jamie Pollock 174 posts 853 karma points c-trib
    May 27, 2014 @ 11:44
    Jamie Pollock
    0

    Examine - creating indexer/searcher programmatically

    Hey all,

    I basically wish to create Examine indexset/indexer/searcher dynamically (through say an eventhandler). Is this at at all possible to create on the fly programmatically? A quick Google didn't bring back anything.

    My set up is on v6.1.5, running on IIS7.5.

    Here's why

    I have a multilingual site which gets deployed dependant on its market/language (eg. gl-en, fr-fr, de-de). This is a business concept rather than a .net culture, although they're loosely based on those ideas. We have a folder structure which dictates the Content structure.

    These markets are then deployed as static files from an Umbraco authoring instance. While it may not be the best way to do things, it's the current way the site works and unfortunately that can not change for now.

    I wish to add search functionality via Examine on a per market basis. This search application will live outside of this static file site and I wish to send the Examine indexes out to this app so it may search through them.

    So recap...

    • Static Website
      • gl/en/
      • fr/fr/

    These sites get deployed along with the programmatically created index file for that market.

    • Search site (MVC)
      • gl/en/

    Routing will handle which index needs to be used based on a convention (eg. ExternalMarketSpecificglen_Searcher). I already have this set up to work with multiple searchers.

    Why not do it via one file?

    I'd rather not have one file as this would be giving the search app information which is potentially not live yet on the static website.

    Why not configure this via the config files?

    This is a possibility and the market root nodes rarely change at all. However it would be a nice to have to remove any developer intervention required to set up Search functionality for a market. Especially if a developer is not available when a new market is created or the team simply forgets this is a requirement.

    Thanks for reading. Hopefully we can reach a solution.
    Jamie

  • Benas Brazdziunas 34 posts 155 karma points c-trib
    May 29, 2014 @ 17:37
    Benas Brazdziunas
    101

    I'm not sure but it worth to try adding them like this.

    Code isn't finished just guidelines:

            var indexer = new UmbracoContentIndexer(IIndexCriteria indexerData, Directory luceneDirectory, IDataService dataService, Analyzer analyzer, bool async);
            var searcher = new UmbracoExamineSearcher(Directory luceneDirectory, Analyzer analyzer);
    
            Examine.ExamineManager.Instance.IndexProviderCollection.Add(indexer);
            Examine.ExamineManager.Instance.SearchProviderCollection.Add(searcher);
    
  • Jamie Pollock 174 posts 853 karma points c-trib
    May 29, 2014 @ 18:20
    Jamie Pollock
    1

    Hi Benas,
    Thanks for this. I had something like this set up. It was just the last "Add()" part I was missing. You get karma!

    Also there is a requirement to call Initialize(string key, NameValueCollection config) for both indexer and searcher before you can add them.

    The purpose of such a thing is to init other attributes. Stuff you can typically set within a config file. For example check out the LuceneIndexer source from github.

    One thing to note is, that this will not change any Examine config files, however the index files are created. I'll have a look exactly how these dynamically created indexes work after they've been created.

    Thanks,
    Jamie

  • Benas Brazdziunas 34 posts 155 karma points c-trib
    May 29, 2014 @ 18:40
    Benas Brazdziunas
    0

    I had a look at Umbaco source and there is test class to get Index:

    This may help:

            public static UmbracoContentIndexer GetUmbracoIndexer(Lucene.Net.Store.Directory luceneDir, Analyzer anzer = null,IDataService dataService = null)
        {
            if (dataService == null)
            {
                dataService = new TestDataService();
            }
    
            if (analyzer == null)
            {
                analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
            }
    
            var indexSet = new IndexSet();
            var indexCriteria = indexSet.ToIndexCriteria(dataService, UmbracoContentIndexer.IndexFieldPolicies);
    
            var i = new UmbracoContentIndexer(indexCriteria,
                                              luceneDir, //custom lucene directory
                                              dataService,
                                              analyzer,
                                              false);
    
            //i.IndexSecondsInterval = 1;
    
            i.IndexingError += IndexingError;
    
            return i;
        } 
    
  • Jamie Pollock 174 posts 853 karma points c-trib
    Jul 03, 2014 @ 10:31
    Jamie Pollock
    0

    Hey folks,
    Sorry to do this but, I'm going to have to say adding Indexers/Searchers at runtime is a bad idea and wouldn't recommend anyone do it programmatically.

    Therefore I'll have to retract my selected answer so as not to give people the wrong idea.

    Thanks,
    Jamie

  • Comment author was deleted

    Jul 08, 2015 @ 14:45

    Jamie,

    Can you give an explanation for your conclusion?

    I'm trying to update the config file, so maybe I can just use a different approach.

    kg

  • Jamie Pollock 174 posts 853 karma points c-trib
    Jul 14, 2015 @ 15:57
    Jamie Pollock
    0

    Hi Kevin,
    It's been a while since I've written this code. However, as I recall, generally trying to modify the examine configuration files ended up in getting a "resolution is frozen" YSOD when editing the files during an Umbraco event handler.

    As it's a readonly provider collection, I concluded the best way to go about adding in new markets was to manually add them in and check changes into our source control. It seemed to be something that needed to be done before the app started.

    Thanks,
    Jamie

  • Comment author was deleted

    Jul 14, 2015 @ 16:13

    Thanks Jamie.

    I ended up parsing the config files and appending new tags and it works a treat.

    Thanks again for replying.

Please Sign in or register to post replies

Write your reply to:

Draft