Difficulty searching on integer field using smart search API

Tom Troughton asked on May 18, 2016 14:57

I have a Pages smart search index which uses the Standard analyzer. When I examine the generated index in Luke I can see that integer fields have a specific format. For example, all pages created by global administrator have the documentcreatedbyuserid field set to 10000000053.

Reading the documentation I see that integer fields like this need to be searched using a particular syntax:

+DocumentCreatedByUserID;(int)53;Administrator

However, when I pass this string to the following code as the searchQuery variable I get no results.

        // Get search results
        var parameters = new SearchParameters()
        {
            AttachmentOrderBy = "",
            AttachmentWhere = "",
            CheckPermissions = false,
            ClassNames = null,
            CombineWithDefaultCulture = false,
            CurrentCulture = this.Context.CultureCode,
            DefaultCulture = CultureHelper.GetDefaultCultureCode(this.Context.SiteName),
            DisplayResults = resultsPerPage,
            NumberOfProcessedResults = 100,
            Path = startPath,
            SearchFor = searchQuery,
            SearchInAttachments = false,
            SearchIndexes = index,
            SearchSort = sort,
            StartingPosition = (page - 1) * resultsPerPage,
            User = this.Context.User.UserInfo
        };

        ds = CMS.Search.SearchHelper.Search(parameters);

This same code works fine for text field search queries. Can anyone explain:

  1. Is there anything obvious I'm doing wrong?
  2. What is the purpose of the final part of the +DocumentCreatedByUserID;(int)53;Administrator query. Why should I need to pass a text value here?

Correct Answer

Tom Troughton answered on May 18, 2016 17:03

I received advice on Stack Overflow which led to a solution.

Here is a method which returns a searchQuery that will query an integer field for a specified value:

protected string GetIntegerIdClause(string field, int id)
{
    var condition = string.Format("{0}:(int){1}", field, id).ToLower();

    return SearchSyntaxHelper.CombineSearchCondition(null, new SearchCondition(condition, SearchModeEnum.ExactPhrase, SearchOptionsEnum.NoneSearch));
}
2 votesVote for this answer Unmark Correct answer

Recent Answers


Marcel Guldemond answered on October 3, 2016 22:18

I have the exact same issue, but your solution isn't working for me. I still get no results from the API on any search on any of my int fields, even though the queries work in the Luke tool.

Does it matter if the search index's Analyzer type is Standard or Subset, for instance?

0 votesVote for this answer Mark as a Correct answer

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