从Kentico searchindex获取所有项目

时间:2016-04-29 09:28:45

标签: c# indexing kentico

我希望获得代码中的所有索引项。此时,我将此作为代码

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;

如何获取代码中的所有索引项。我想收到一份包含这些物品的清单。

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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中所示。