Smart Search API - How to get full fields from result

Richard Lloyd asked on August 31, 2018 16:02

I have a working search index that returns 10 results currently. The only thing is that it only returns a limited amount of information. In debug i can see the path to the page the content was on but can't see a way of accessing this in code. I'd like to be able to do something like the following but with the data being extracted out and bound to an object to serve up to my api:

      foreach (SearchResultItem<BaseInfo> searchResultItem in searchResult.Items)
        {
          var searchResultModel = new SearchResultItemModel(searchResultItem.Fields);

            var link = searchResultModel.pagelink;//how do i get this?
            var pageName = searchResultModel.pageName;//how do i get this?
            var title = searchResultModel.title;//how do i get this?
            var content = searchResultModel.content;//how do i get this?

            itemModels.Add(searchResultModel);

        }; 

Correct Answer

Dragoljub Ilic answered on August 31, 2018 16:31

Hi Richard,

Did you tried something like this to get data from search index:

var link = ValidationHelper.GetString(searchResultItem.Data.GetValue(nameof(TreeNode.NodeAliasPath)), String.Empty);
var pageName = ValidationHelper.GetString(searchResultItem.Data.GetValue(nameof(TreeNode.DocumentName)), String.Empty);
var title = searchResultItem.Fields.Title;
var content = searchResultItem.Fields.Content;

Best regards, Dragoljub

0 votesVote for this answer Unmark Correct answer

Recent Answers


Zach Perry answered on August 31, 2018 17:31

What are you doing to get your Search Results from the index?

Here is kind of a stripped down version of what I do:

  SearchParameters parameters = new SearchParameters
            {
                SearchFor = searchText,
                SearchSort = string.Empty,
                Path = string.Empty,
                ClassNames = string.Empty,
                CurrentCulture = "##ALL##",
                DefaultCulture = null,
                CombineWithDefaultCulture = false,
                CheckPermissions = true,
                SearchInAttachments = false,
                User = MembershipContext.AuthenticatedUser,
                SearchIndexes = searchIndexes,
                StartingPosition = 0,
                DisplayResults = 10,
                NumberOfProcessedResults = 100,
                NumberOfResults = 0,
                AttachmentWhere = null,
                AttachmentOrderBy = null,
                BlockFieldOnlySearch = false
            };



            // Search
            var searchResults = SearchHelper.Search(parameters);

            foreach (DataRow row in searchResults.Tables[0].Rows)
            {
            var id = row["id"].ToString();
                     var title = ValidationHelper.GetString(SearchContext.GetSearchValue(id, "title"), "");
            var content = ValidationHelper.GetString(SearchContext.GetSearchValue(id, "content"), "");
            var date = ValidationHelper.GetString(row["created"], "");

            var url = ValidationHelper.GetString(SearchContext.GetSearchValue(id, SearchFieldsConstants.CUSTOM_URL),
                "");
            }
0 votesVote for this answer Mark as a Correct answer

James Ison answered on December 5, 2018 19:27

Zach your code does not work for me any clues?

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on December 5, 2018 20:07 (last edited on December 5, 2018 20:09)

I use mine in a service that returns some json, but here is the full method that searches the index:

    private List<string[]> SearchUsersAdvanced(string searchString, int topNum)
    {
        DocumentSearchCondition docCondition = new DocumentSearchCondition();
        var condition = new SearchCondition("", SearchModeEnum.AnyWord, SearchOptionsEnum.BasicSearch, docCondition,
            false);

        var searchText = SearchSyntaxHelper.CombineSearchCondition(searchString, condition);
        SearchParameters parameters = new SearchParameters
        {
            SearchFor = searchText,
            SearchSort = string.Empty,
            Path = string.Empty,
            ClassNames = string.Empty,
            CurrentCulture = LocalizationContext.PreferredCultureCode,
            DefaultCulture = CultureHelper.GetDefaultCultureCode(SiteContext.CurrentSiteName),
            CombineWithDefaultCulture = false,
            CheckPermissions = false,
            SearchInAttachments = false,
            User = MembershipContext.AuthenticatedUser,
            SearchIndexes = "UsersIndex",
            StartingPosition = 0,
            DisplayResults = topNum,
            NumberOfProcessedResults = 100,
            NumberOfResults = topNum,
            AttachmentWhere = string.Empty,
            AttachmentOrderBy = string.Empty,
            BlockFieldOnlySearch = false
        };

        // Search
        DataSet users = SearchHelper.Search(parameters);

        var result = new List<string[]>();
        if (users != null && users.Tables[0] != null && users.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow user in users.Tables[0].Rows)
            {
                //var userToAdd = new string[] { user["username"].ToString(), user["fullname"].ToString()};
                var id = user["id"].ToString();
                var userId = SearchContext.GetSearchValue(id, "userid");
                var avatarId = ValidationHelper.GetInteger(SearchContext.GetSearchValue(id, "UserAvatarID"), 0);
                var fullname = SearchContext.GetSearchValue(id, "FullName");
                var imageUrl = AvatarInfoProvider.GetUserAvatarImageUrl(avatarId, userId, null, 0, 0, 0);
                var position = SearchContext.GetSearchValue(id, "UserPosition");
                var department = SearchContext.GetSearchValue(id, "UserDepartment");


                result.Add(new string[]
                {
                    "user",userId.ToString(), fullname.ToString(), imageUrl,
                    position.ToString(),
                    department.ToString(),"",""
                });
            }
        }

        return result;
    }

You can look at SearchResults.ascx.cs control for some more on how it works.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.