【问题标题】:Exclude macros from searching in umbraco从 umbraco 中的搜索中排除宏
【发布时间】:2016-09-13 10:55:06
【问题描述】:

我在 umbraco 中设置 Lucene 搜索引擎时遇到问题。我正在尝试搜索存储在 Umbraco 创建的默认索引中的数据。搜索方法如下:

        private DictionaryResult GetRowContent(
        Lucene.Net.Highlight.Highlighter highlighter,
        Lucene.Net.Analysis.Standard.StandardAnalyzer analyzer
        ,Lucene.Net.Documents.Document doc1, string criteria)
    {
        JavaScriptSerializer jsScriptSerializer = new JavaScriptSerializer();
        DictionaryResult controls = new DictionaryResult();
        Lucene.Net.Analysis.TokenStream stream = analyzer.TokenStream("", new StringReader(doc1.Get("bodyContent")));
        dynamic rowContentHtmlDocument = JObject.Parse(((JValue)doc1.Get("bodyContent")).ToString(CultureInfo.CurrentCulture));
        foreach (dynamic section in rowContentHtmlDocument.sections)
        {
            foreach (var row in section.rows)
            {
                foreach (var area in row.areas)
                {
                    foreach (var control in area.controls)
                    {
                        if (control != null && control.editor != null) // && control.editor.view != null)
                        {
                            JObject rowContentHtml = null;
                            try
                            {
                                rowContentHtml = JObject.Parse(((JContainer)control)["value"].ToString());
                            }
                            catch (Exception e)
                            {
                            }
                            if (rowContentHtml != null)
                            {
                                try
                                {
                                    var macroParamsDictionary = JObject.Parse(((JContainer)rowContentHtml)["macroParamsDictionary"].ToString());
                                    var documentText = macroParamsDictionary.GetValue("dokument");
                                    if (documentText != null)
                                    {
                                        var document = documentText.ToString().Replace(""", "\"");
                                        dynamic documents = jsScriptSerializer.Deserialize<dynamic>(document);
                                        foreach (Dictionary<string, object> doc in documents)
                                        {
                                            if (doc.ContainsKey("FileName") && doc.ContainsKey("DocumentId"))
                                            {
                                                if (doc["FileName"].ToString().Length > 0 && 
                                                    doc["FileName"].ToString().ToLower().Contains(criteria.ToLower()))
                                                {
                                                    controls.Add(new RowResult()
                                                    {
                                                        Type = 0,
                                                        Object = new Document()
                                                        {
                                                            DocumentName = doc["FileName"].ToString(),//highlighter.GetBestFragments(stream, doc["FileName"].ToString(), 1, "..."),
                                                            DocId = Guid.Parse(doc["DocumentId"].ToString())
                                                        } // StringBuilder(@"<a href=" + Url.Action("DownloadDocument", "Document", new { DocumentId = doc["DocumentId"] }) + "> " + @doc["FileName"] + "</a>").ToString()
                                                    }
                                                    );
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                }
                            }
                            else
                            {
                                var text = HtmlRemoval.StripTagsRegex(((JContainer)control)["value"].ToString()).Replace("ë", "e").Replace("ç", "c");
                                var textResultFiltered =  highlighter.GetBestFragments(stream,doc1.Get("bodyContent"), 5, "...");
                                controls.Add(new RowResult()
                                {
                                    Type = 1,
                                    Object = textResultFiltered
                                });
                            }
                        }
                    }
                }
            }
        }
        return controls;
    }

在这里,我试图从简单的 html 内容中过滤宏文档并以不同的方式呈现。但在这部分的最后

var text = HtmlRemoval.StripTagsRegex(((JContainer)control)["value"].ToString()).Replace("ë", "e").Replace("ç", "c");
                            var textResultFiltered =  highlighter.GetBestFragments(stream,doc1.Get("bodyContent"), 5, "...");
                            controls.Add(new RowResult()
                            {
                                Type = 1,
                                Object = textResultFiltered
                            });

它在搜索中包含宏。结果我得到了文档属性,但突出显示的 html 内容具有如下宏内容:

6th Edition V413HAV.pdf","FileContent"... Framework 6th Edition V413HAV.pdf","... with Java 8 - 1st Edition (2015) - Copy.pdf"... 4.5 Framework 6th Edition V413HAV.pdf","... And The NET 4.5 Framework 6th Edition V413HAV.pdf" which is coming from Json data of the macro. Any idea how to exclude the macros from searching or to customize the hmtl content not to search on specific macro ? Thanks in advance. 

我正在参考此链接来创建荧光笔等... Link to Lucene example

知道如何防止搜索宏或将它们从突出显示的内容中排除吗?

【问题讨论】:

    标签: lucene macros umbraco7 umbraco6


    【解决方案1】:

    如果您只是进行常规搜索,这看起来太复杂了。你知道 Umbraco 有自己的 Lucene“版本”,叫做 Examine 吗?它内置在 Umbraco 中,不需要太多设置即可运行标准搜索:https://our.umbraco.org/documentation/reference/searching/examine/

    我从未在使用检查的搜索结果中看到宏或 JSON 标记,所以不妨试试看?

    【讨论】:

      【解决方案2】:

      您可以轻松使用检查。 您只需选择所需的搜索提供程序 (config/ExamineSettings.config),它允许您选择是否要避免未发布和受保护的内容。然后,您只需执行下一段代码之类的操作,您可以在其中选择要搜索的字段或不想避免的 Dact 类型。

      string term = "test"
      
      var criteria = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"].CreateSearchCriteria();
      var crawl = criteria.GroupedOr(new string[] { "nodeName", "pageTitle", "metaDescription", "metaKeywords" }, term)
                      .Not().Field("nodeTypeAlias", "GlobalSettings")
                      .Not().Field("nodeTypeAlias", "Error")
                      .Not().Field("nodeTypeAlias", "File")
                      .Not().Field("nodeTypeAlias", "Folder")
                      .Not().Field("nodeTypeAlias", "Image")
                      .Not().Field("excludeFromSearch", "1")
                      .Compile();
      
       ISearchResults SearchResults = ExamineManager.Instance
                      .SearchProviderCollection["ExternalSearcher"]
                      .Search(crawl);
      
       IList<JsonSearchResult> results = new List<JsonSearchResult>();
      

      希望这是有道理的。

      【讨论】:

      • 嗨卢西奥,感谢您的回复。我想知道如何进行突出显示过程。能不能举个真实的例子?!
      【解决方案3】:

      我尝试使用检查以及以下:

      SearchQuery = string.Format("+{0}:{1}~", SearchField, criteria);
      var Criteria = ExamineManager.Instance
                          .SearchProviderCollection["ExternalSearcher"]
                          .CreateSearchCriteria();
      var crawl = Criteria.GroupedOr(new string[] { "bodyContent", "nodeName" }, criteria)
                          .Not()
                          .Field("umbracoNaviHide", "1")
                          .Not()
                          .Field("nodeTypeAlias", "Image")
                          .Compile();
      IEnumerable<Examine.SearchResult> SearchResults1 = ExamineManager.Instance
                          .SearchProviderCollection["ExternalSearcher"]
                          .Search(crawl);
      

      我使用了两种方法来突出显示下面的文本,但是这些方法效率不高!!!我有一些链接根本没有突出显示任何文本。

              public string GetHighlight(string value, string highlightField, BaseLuceneSearcher searcher, string luceneRawQuery)
          {
              var query = GetQueryParser(highlightField).Parse(luceneRawQuery);
              var scorer = new QueryScorer(searcher.GetSearcher().Rewrite(query));
      
              var highlighter = new Highlighter(HighlightFormatter, scorer);
      
              var tokenStream = HighlightAnalyzer.TokenStream(highlightField, new StringReader(value));
              return highlighter.GetBestFragments(tokenStream, value, MaxNumHighlights, Separator);
          }
          protected QueryParser GetQueryParser(string highlightField)
          {
              if (!QueryParsers.ToString().Contains(highlightField))
              {
                  var temp = new QueryParser(_luceneVersion, highlightField, HighlightAnalyzer);
                  return temp;
              }
              return null;
          }
      

      如果您有任何在检查中突出显示的样本非常有效,我将不胜感激..

      【讨论】:

      • 我没有尝试过用高亮显示检查,所以恐怕我画的有点空白。但是获取没有突出显示文本的链接 - 这难道不是某个地方的 CSS 问题吗?
      • 我遇到的问题只是链接下面的突出显示段落,就像谷歌一样。链接没问题。无论如何感谢您的帮助:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 2014-01-10
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      相关资源
      最近更新 更多