2016-04-29 50 views
0

我正在尋找代碼中的所有索引項目。在這一刻,我有這個代碼從Kentico獲取所有項目searchindex

string indexName = "indexname"; 
var index = SearchIndexInfoProvider.GetSearchIndexInfo(indexName); 

// returning 0 
var itemChildrenNumbers = index.Children.Count; 

// returning an empty CombinedInfoObjectCollection 
var itemChildren = index.Children.All; 

// returning the right amount of indexed items 
var numberOfIndexedItems = index.NumberOfIndexedItems; 

我怎樣才能得到代碼中的所有索引項目。我想收到一個列表中的項目。

回答

4
private DataSet SearchText(string searchQuery){ 
    // Get the search index 
    SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo(searchIndex); 
    DataSet results = new DataSet(); 

    if (index != null) 
    { 
     // Prepare parameters 
     SearchParameters parameters = new SearchParameters() 
     { 
      SearchFor = searchQuery, 
      Path = "/%", 
      ClassNames = "", 
      CurrentCulture = "EN-US", 
      DefaultCulture = CultureHelper.DefaultUICulture.IetfLanguageTag, 
      CombineWithDefaultCulture = false, 
      CheckPermissions = false, 
      SearchInAttachments = false, 
      User = (UserInfo) CMS.Membership.MembershipContext.AuthenticatedUser, 
      SearchIndexes = index.IndexName, 
      StartingPosition = 0, 
      DisplayResults = 5000, 
      NumberOfProcessedResults = 5000, 
      NumberOfResults = 5000, 
      AttachmentWhere = String.Empty, 
      AttachmentOrderBy = String.Empty, 
     }; 

     // Search returns resultset. 
     results = SearchHelper.Search(parameters); 
    } 
    return results; 
} 
+0

這是假設你正試圖通過Kentico獲取結果。 –

2

我不知道,如果你可以輕鬆地訪問所有索引項目。這樣做的目的是什麼?

然而,你可以做的是做一個手動搜索,然後處理結果。也許這對你來說會更好。

要進行搜索,您可以使用這樣的事情:

 var index = SearchIndexInfoProvider.GetSearchIndexInfo("indexName"); 

     if (index != null) 
     { 
      SearchParameters parameters = new SearchParameters() 
      { 

       SearchFor = "Something", 
       SearchSort = "##SCORE##", 
       Path = "/%", 
       ClassNames = "", 
       CurrentCulture = "EN-US", 
       DefaultCulture = CultureHelper.EnglishCulture.IetfLanguageTag, 
       CombineWithDefaultCulture = false, 
       CheckPermissions = false, 
       SearchInAttachments = false, 
       User = (UserInfo)MembershipContext.AuthenticatedUser, 
       SearchIndexes = index.IndexName, 
       StartingPosition = 0, 
       DisplayResults = 100, 
       NumberOfProcessedResults = 100, 
       NumberOfResults = 0, 
       AttachmentWhere = String.Empty, 
       AttachmentOrderBy = String.Empty, 
      }; 

      // Performs the search and saves the results into a DataSet 
      System.Data.DataSet results = SearchHelper.Search(parameters); 

      if (!DataHelper.DataSourceIsEmpty(results)) 
      { 
       foreach (DataRow searchItem in results.Tables[0].Rows) 
       { 
        // do whatever you need with the search item 
       } 
      } 
     } 

您還可以過濾返回結果的數量,你可以在SearchPatameters看到。