Copied to clipboard

Flag this post as spam?

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


  • Vanita 4 posts 74 karma points
    Jun 09, 2016 @ 06:00
    Vanita
    0

    Examine Search

    Hi All, The problem here is , i wrote my customevent class which is inheritted from ApplicationEventHandler, Here is my class :

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { ExamineManager.Instance.IndexProviderCollection["MySearchIndexer"].RebuildIndex();

            ExamineManager.Instance.IndexProviderCollection["MySearchIndexer"].GatheringNodeData += OnGatheringNodeData;
    
    
            //UmbracoContentIndexerCustom _umContentIndexerCustom = new UmbracoContentIndexerCustom();
        }
    
        private void OnGatheringNodeData(object sender, IndexingNodeDataEventArgs e)
        {
            try
            {
                string nodeTypeAlias = e.Fields["nodeTypeAlias"];
                LogHelper.Info<UmbracoContentIndexer>("Gathering node data for node #" + e.NodeId + " (type: " + nodeTypeAlias + ")");
    
    
                if (nodeTypeAlias == "Home" || nodeTypeAlias == "LandingPage")
                {
                    string value;
                    if (e.Fields.TryGetValue("myContent", out value))
                    {
    
                        LogHelper.Info<UmbracoContentIndexer>("Node has \"content\" value\"");
                        GridDataModel grid = GridDataModel.Deserialize(e.Fields["myContent"]);
    
                        //StringBuilder contentFocus = new StringBuilder();
                        StringBuilder contentNormal = new StringBuilder();
    
                        StringBuilder contentPanelMacroContent = new StringBuilder();
                        StringBuilder ctaMacroContent = new StringBuilder();
                        StringBuilder newsMacroContent = new StringBuilder();
    
                        foreach (GridControl ctrl in grid.GetAllControls())
                        {
    
                            switch (ctrl.Editor.Alias)
                            {
    
                                case "rte":
                                    {
    
                                        // Get the HTML value
                                        string html = ctrl.GetValue<GridControlRichTextValue>().Value;
    
                                        // Strip any HTML tags so we only have text
                                        string text = Regex.Replace(html, "<.*?>", "");
    
                                        // Extra decoding may be necessary
                                        text = HttpUtility.HtmlDecode(text);
    
                                        // Now append the text
                                        contentNormal.AppendLine(text);
    
                                        break;
    
                                    }
    
                                case "media":
                                    {
                                        GridControlMediaValue media = ctrl.GetValue<GridControlMediaValue>();
                                        contentNormal.AppendLine(media.Caption);
                                        break;
                                    }
    
                                case "headline":
                                    contentNormal.AppendLine(ctrl.GetValue<GridControlTextValue>().Value);
                                    break;
    
                                case "headline_centered":
                                    contentNormal.AppendLine(ctrl.GetValue<GridControlTextValue>().Value);
                                    break;
    
                                case "macro":
                                    {
    
                                        GridControlMacroValue macro = ctrl.GetValue<GridControlMacroValue>();
                                        if (macro.MacroAlias == "ContentPanel")
                                        {
    
                                            Dictionary<string, object> myDictionary = new Dictionary<string, object>(macro.Parameters.Count);
                                            myDictionary = macro.Parameters;
                                            foreach (KeyValuePair<string, object> keyvaluepair in myDictionary)
                                            {
                                                var itemKey = keyvaluepair.Key;
                                                var itemValue = keyvaluepair.Value;                                                
    
    
                                                var contentPanelItem = new DynamicNode(new Node(Convert.ToInt32(itemValue)));
                                                var descValue = (contentPanelItem.HasValue("Description"))
                                                                   ? contentPanelItem.GetPropertyValue("Description").StripHtml()
                                                                   : contentPanelItem.Name;
    
    
                                                var headlineValue = (contentPanelItem.HasValue("Headline"))
                                                                   ? contentPanelItem.GetPropertyValue("Headline").StripHtml()
                                                                   : contentPanelItem.Name;
    
    
                                                contentPanelMacroContent.AppendLine(headlineValue); 
                                                contentPanelMacroContent.AppendLine(descValue);
    
    
                                            }
    
                                        }
    
                                        if (macro.MacroAlias == "HomeCalltoAction")
                                        {
    
                                            Dictionary<string, object> ctaDictionary = new Dictionary<string, object>(macro.Parameters.Count);
                                            ctaDictionary = macro.Parameters;
                                            foreach (KeyValuePair<string, object> keyvaluepair in ctaDictionary)
                                            {
                                                var itemKey = keyvaluepair.Key;
                                                var itemValue = keyvaluepair.Value;
    
    
                                                var ctaItem = new DynamicNode(new Node(Convert.ToInt32(itemValue)));
                                                var descValue = (ctaItem.HasValue("Description"))
                                                                   ? ctaItem.GetPropertyValue("Description").StripHtml()
                                                                   : ctaItem.Name;
    
    
                                                var headlineValue = (ctaItem.HasValue("Headline"))
                                                                   ? ctaItem.GetPropertyValue("Headline").StripHtml()
                                                                   : ctaItem.Name;
    
    
                                                ctaMacroContent.AppendLine(headlineValue + "\n");
                                                ctaMacroContent.AppendLine(descValue + "\n");
                                                ctaMacroContent.AppendLine("\n");
    
    
    
    
                                            }
    
                                        }
    
    
                                        if (macro.MacroAlias == "NewsArticles")
                                        {
    
                                            Dictionary<string, object> newsArticleDictionary = new Dictionary<string, object>(macro.Parameters.Count);
                                            newsArticleDictionary = macro.Parameters;
                                            foreach (KeyValuePair<string, object> keyvaluepair in newsArticleDictionary)
                                            {
                                                var itemKey = keyvaluepair.Key;
                                                var itemValue = keyvaluepair.Value;
    
    
                                                var newsItems = new DynamicNode(new Node(Convert.ToInt32(itemValue)));
    
                                                foreach (var newsArticle in newsItems.Children)
                                                {
                                                    var descValue = (newsArticle.HasValue("description"))
                                                                  ? newsArticle.GetPropertyValue("description").StripHtml()
                                                                  : newsArticle.Name;
    
    
                                                    var titleValue = (newsArticle.HasValue("title"))
                                                                       ? newsArticle.GetPropertyValue("title")
                                                                       : newsArticle.Name;
    
                                                    newsMacroContent.AppendLine(titleValue);
                                                    newsMacroContent.AppendLine(descValue);
                                                    newsMacroContent.AppendLine("\n");
                                                }
    
    
    
    
    
                                            }
    
                                        }
    
    
                                        break;
                                    }
    
                                case "quote":
                                    {
                                        contentNormal.AppendLine(ctrl.GetValue<GridControlTextValue>().Value);
                                        break;
                                    }
                            }
                        }
    
                        //e.Fields["refContent"] = contentFocus.ToString();
    
                        e.Fields["myContent"] = contentNormal.ToString();
    
                        e.Fields["refContentPanelContent"] = contentPanelMacroContent.ToString();
                        e.Fields["refCTAContent"] = ctaMacroContent.ToString();
                        e.Fields["refNewsArticleContent"] = newsMacroContent.ToString();
    
                        contentNormal.Clear();
                        contentPanelMacroContent.Clear();
                        ctaMacroContent.Clear();
                        newsMacroContent.Clear();
    
    
    
                        ////var path = e.Fields["path"];
                        //XmlNode node = ((System.Xml.IHasXmlNode)umbraco.library.GetXmlNodeById(e.NodeId.ToString()).Current).GetNode();
                        //var currentNodePath = getPath(node);
                        ////var currentNodePath = getFullPath(e.Fields["path"]);                        
                        //e.Fields.Add("searchPath", currentNodePath.ToString());
                    }
                    else
                    {
    
                        LogHelper.Info<UmbracoContentIndexer>("Node has no \"content\" value\"");
    
                    }
    
                }
    
            }
            catch (Exception ex)
            {
    
                LogHelper.Error<UmbracoContentIndexer>("MAYDAY! MAYDAY! MAYDAY!", ex);
    
            }
    
        }
    

    By doing above, i got my all macros(ContentPanel, CalltoAction, NewsArticle) content in my reference fields.

    and now i am searching result in my Search razor file, below is my search.cshtml :

    @{ //Get search query var q = Request.QueryString["q"]; if (q == null) { //If it is null then the form was not posted and the page was visited directly

    Please use the search box

    //Stop all other code running in this Macro return; }

    var Searcher = ExamineManager.Instance.SearchProviderCollection["MySearchSearcher"];
    
    
    var searchCriteria = Searcher.CreateSearchCriteria(UmbracoExamine.IndexTypes.Content, BooleanOperation.Or);
    
    //var query = searchCriteria.Field("nodeName", "women").Or().Field("myContent", "women").Or().Field("siteDescription", "women").Or().Field("refContentPanelContent", "women").Or().Field("refCTAContent", "women").Compile();
    //var searchResults = Searcher.Search(query);
    
    
    
    IBooleanOperation filter = null;
    var q_split = q.Split(' ');
    int i = 0;
    for (i = 0; i < q_split.Length; i++)
    {
    
        if (filter == null)
        {
            filter = searchCriteria.GroupedOr(new string[] { "nodeName", "myContent", "footer", "siteDescription", "bodyText", "refContent", "refContentPanelContent", "refCTAContent", "refNewsArticleContent" }, q_split[i]);
        }
        else
        {
            filter = filter.Or().GroupedOr(new string[] { "nodeName", "myContent", "footer", "siteDescription", "bodyText", "refContent", "refContentPanelContent", "refCTAContent", "refNewsArticleContent" }, q_split[i]);
        }
    }
    
    var searchResults = Searcher.Search(filter.Compile());
    
    if (searchResults.Any())
    {
        <p> Your search for @q returned @searchResults.Count().ToString() result(s)</p>
        <ul id="content" style="list-style:none;">
            @foreach (var result in searchResults)
                {
                    var node = Umbraco.TypedContent(result.Id);
    
                    var pathIds = result["__Path"].Split(',');
                    var path = Umbraco.TypedContent(pathIds).Where(p => p != null).Select(p => new { p.Name }).ToList();
    
    
                <li>
                    <section class="results-box">
    
                       <h3><a href="@node.Url">@String.Join(" > ", path.Select(p => p.Name))</a></h3>
                        @*<h3>@node.Name</h3>*@
                        @*<a href="@node.Url" class="results-url">@node.Url</a>*@
    
    
    
                        @if (result.Fields.ContainsKey("bodyText"))
                        {
                            <h1>Body Text Content</h1>
                            <p>@result["bodyText"].Truncate(250)</p>
                        }
    
                        @if (result.Fields.ContainsKey("siteDescription"))
                        {
                            <h1>Site Description</h1>
                            <p>@result["siteDescription"].Truncate(250)</p>
                        }
    
                        @if (result.Fields.ContainsKey("myContent"))
                        {
                            <h1>Grid Normal Content</h1>
                            <p>@result["myContent"]</p>
                        }                        
    
                        @*@if (result.Fields.ContainsKey("refContent"))
                        {
                            <p>@result["refContent"]</p>
                        }*@
    
                        @if (result.Fields.ContainsKey("refContentPanelContent"))
                        {
                            <h1>Macro Content Panel Content</h1>
                            <p>@result["refContentPanelContent"]</p>
                        }
    
                        @if (result.Fields.ContainsKey("refCTAContent"))
                        {
                            <h1>Macro CTA Content</h1>
                            <p>@result["refCTAContent"]</p>
                        }
    
                        @if (result.Fields.ContainsKey("refNewsArticleContent"))
                        {
                            <h1>Macro News List Content</h1>
                            <p>@result["refNewsArticleContent"]</p>
                        }
    
                    </section>
                    <hr />
                </li>
            }
        </ul>
    }
    else
    {
        <p> There are no results matching your search criteria.</p>
    }
    

    }

    Now the problem is , in searchResults variable , it is displaying search result set of all fields which has values, irrespective of searched keyword.

    and filter.complie() in my case is: SearchIndexType: "content", LuceneQuery: {+((nodeName:women myContent:women footer:women siteDescription:women bodyText:women refContent:women refContentPanelContent:women refCTAContent:women refNewsArticleContent:women)) +__IndexType:content} Examine.SearchCriteria.ISearchCriteria {Examine.LuceneEngine.SearchCriteria.LuceneSearchCriteria}

    Out of 9 fields defined in my

    I am badly stuck in this, resultset should only give me results of those fields which has "women" keyword, not the result of other fields as well.

    Please help Vanita Chawla

Please Sign in or register to post replies

Write your reply to:

Draft