Copied to clipboard

Flag this post as spam?

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


  • Jonathan Roberts 409 posts 1063 karma points
    Sep 05, 2017 @ 11:11
    Jonathan Roberts
    0

    Examine search on Multi site set up - 7.4.3

    HI,

    I have a multi site set up where there are three separate sites in the CMS. At present there is only 1 Site Index and the search on all 3 sites bring back the same results.

    How can I separate the search results to only show results relevant to the current site?

    Thanks

    Jon

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Sep 05, 2017 @ 14:01
    Alex Skrypnyk
    0

    Hi Jon

    Do you have separate trees for each site? How does solution look from Umbraco admin part?

    You can filter search results on render stage, just check what site this result bongs and show or hide it, another solution is to add a new param to index entries, that will store which site is this page.

    Thanks,

    Alex

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Sep 05, 2017 @ 14:14
    Simon Dingley
    0

    I have a multisite install with 12+ sites in it and to be able to filter search results I have an event handler to add a custom field into the search index when a document is saved that allows me to filter search results later on.

    So, in my ApplicationEventHandler ApplicationStarted method I add a new event handler as follows:

    foreach (BaseIndexProvider index in ExamineManager.Instance.IndexProviderCollection)
    {
        if (index.Name == "SiteSearchIndexer" || index.Name == "MyCustomSearchIndexer")
        {
            index.GatheringNodeData += this.SearchIndexerGatheringNodeData;
            ((LuceneIndexer)index).DocumentWriting += LuceneDocumentWriting;
        }
    }
    

    This is then implemented as follows:

    private void SearchIndexerGatheringNodeData(object sender, IndexingNodeDataEventArgs e)
    {
        if (e.IndexType == IndexTypes.Content)
        {
            try
            {
                if (e.Fields.ContainsKey("path"))
                {
                    // Split the path to the current node and extract the homepage id
                    string rootNodeId = e.Fields["path"].Split(',')[2];
                    e.Fields.Add("siteId", rootNodeId);
                }
    
                [...]
    
            }
            catch (Exception ex)
            {
                LogHelper.Error(this.GetType(), "Failed to update search index node data", ex);
            }
        }
    }
    

    Then in my search query something like this:

    if (Model.AncestorOrSelf("MyHomepageDoctypeAlias") != null)
    {
        filter.And().Field("siteId", site.Id.ToString());
    }
    

    Hope that helps.

    Simon

Please Sign in or register to post replies

Write your reply to:

Draft