2016-09-13 85 views
0

在umbraco中設置Lucene搜索引擎時,我遇到了一個問題。我試圖搜索由Umbraco創建的默認索引中存儲的數據。搜索的方法如下:從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. 

我指的這個鏈接來創建Hightlighter等等 Link to Lucene example

任何想法如何阻止在宏中搜索或從高亮顯示的內容中排除它們?

回答

0

如果您只是進行常規搜索,那看起來太複雜了。你知道Umbraco有自己的Lucene「版本」,叫做Examine嗎?它內置到Umbraco中,並且不需要太多設置即可運行標準搜索:https://our.umbraco.org/documentation/reference/searching/examine/

我從未在我的搜索結果中看到過使用檢查的宏或JSON標記,因此可能試試?

0

您可以輕鬆使用檢查。 您只需要選擇您想要的搜索提供程序(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>(); 

希望這是有道理的。

+0

嗨盧西奧,謝謝。我想知道如何製作高亮過程。如果你能給我一個真實的例子嗎?! –

0

我嘗試使用檢查以及下文:

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; 
    } 

如果你有突出的檢查是非常有效的我會非常感激你的迴應任何樣品..

+0

我還沒有試過檢查突出顯示,所以我畫了一個空白,我害怕。但獲取沒有突出顯示文字的鏈接 - 這不是一個CSS問題嗎? –

+0

我遇到的問題只是關於像google這樣的高亮段落鏈接。鏈接是好的。非常感謝您的幫助:) –