Tom Troughton
Tom Troughton

Reputation: 4325

Difficulty searching Kentico smart search index on integer field using API

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?

The field I actually want to search is a custom page type field called newstypeid, which I can see is storing its value in the same way in the index (e.g. a value of 34 is stored as 10000000034).

In Luke if I query +newstypeid:10000000034 I get results. So maybe an easier solution is to find a way to translate an integer to this Lucene format? (i.e. 34 to 10000000034)

UPDATE WITH SOLUTION

Thanks to @richard-Šůstek for pointing me in the right direction. The following method will return a search clause in the required format:

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

Upvotes: 1

Views: 749

Answers (2)

Plaz
Plaz

Reputation: 83

I think you should use SearchValueConverter class from the namespace CMS.Search. This class has static methods to convert specific data type values (int,datetime,etc.) to it's string representation for search terms construction.

Upvotes: 2

Enn
Enn

Reputation: 2209

Can you try using something like this to transform the searchQuery?:

var condition = new SearchCondition(null, searchModeEnum, SearchOptionsEnum.FullSearch);

searchQuery = SearchSyntaxHelper.CombineSearchCondition(searchText, condition);

I noticed that Kentico is internally calling this method when passing the value from search text box to SearchParameters. I haven't had a chance to test this though. Maybe some other method in SearchSyntaxHelper would be useful too.

Upvotes: 1

Related Questions