Search not filtering by Document Type

Michael Legacy asked on June 25, 2019 18:58

So I'm building my custom search parameters object and the Lucene string i'm building doesn't seem to be working.

I am attempting to filter results by DocumentType. Here is the code I have so far. Standard search works just fine. Attempting to add a Lucene condition fails to return any results. (Also, if anyone has a better way of getting a total count than running a second search with a much higher paged number, please let me know, its pretty messy this way.)

[Route("Search/Results/{page?}")]
    public ActionResult Results(string q, string type, int page = 1)
    {
        int skip = 0;
        int take = 10;
        string finalQuery = "";
        if (page > 1)
        {
            skip = ((page - 1) * take);
        }

        q = HttpUtility.UrlDecode(q);

        if (String.IsNullOrWhiteSpace(q))
        {
            // Creates a model representing empty search results
            SearchResultViewModel emptyModel = new SearchResultViewModel
            {
                Items = new List<SearchResultItem>(),
                Query = String.Empty
            };

            return View(emptyModel);
        }

        if (!String.IsNullOrWhiteSpace(type))
        {
            finalQuery = q + " +DocumentType:" + type;
        } else
        {
            finalQuery = q;
        }



        // Gets the search index
        SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo("SiteIndex");

        SearchResultViewModel model = new SearchResultViewModel();

        string searchFor = finalQuery;

        if (index != null)
        {
            /*These parameters just get a total count of the results for pagination (up to 1000)*/
            SearchParameters countparameters = new SearchParameters()
            {
                SearchFor = searchFor,
                SearchSort = "##SCORE##",
                Path = "/%",
                CurrentCulture = "EN-US",
                DefaultCulture = CultureHelper.EnglishCulture.IetfLanguageTag,
                CombineWithDefaultCulture = false,
                CheckPermissions = false,
                SearchInAttachments = false,
                User = (UserInfo)MembershipContext.AuthenticatedUser,
                SearchIndexes = index.IndexName,
                StartingPosition = 0,
                DisplayResults = 1000,
                NumberOfProcessedResults = 1000,
                NumberOfResults = 0,
                AttachmentWhere = String.Empty,
                AttachmentOrderBy = String.Empty,
                ClassNames = ""
            };
            // Prepares the real search parameters
            SearchParameters parameters = new SearchParameters()
            {
                SearchFor = searchFor,
                SearchSort = "##SCORE##",
                Path = "/%",
                CurrentCulture = "EN-US",
                DefaultCulture = CultureHelper.EnglishCulture.IetfLanguageTag,
                CombineWithDefaultCulture = false,
                CheckPermissions = false,
                SearchInAttachments = false,
                User = (UserInfo)MembershipContext.AuthenticatedUser,
                SearchIndexes = index.IndexName,
                StartingPosition = skip,
                DisplayResults = 10,
                NumberOfProcessedResults = 1000,
                NumberOfResults = 0,
                AttachmentWhere = String.Empty,
                AttachmentOrderBy = String.Empty,
                ClassNames = ""
            };

            // Performs the search and returns the matching results as a SearchResult object
            SearchResult countResult = SearchHelper.Search(countparameters);
            SearchResult results = SearchHelper.Search(parameters);

            model.PageSize = 10;
            model.CurrentPage = page;
            model.TotalItems = countResult.Items.Count;
            model.Items = results.Items;
            model.Query = q;
        }

        return View(model);
    }

So with a Document Type of "Condition" and a search query of "pediatric, my "searchFor" area ends up looking something like this:

"pediatric +DocumentType:Condition" 

I've tried both using the Display name "Condition" or class name of "CMS.condition" and neither work. I'm at a loss here.

Recent Answers


Peter Mogilnitski answered on June 25, 2019 19:06

not quite sure that the field name is pagetype try +classname: "CMS.condition"

0 votesVote for this answer Mark as a Correct answer

Michael Legacy answered on June 25, 2019 19:16

You're my hero.

0 votesVote for this answer Mark as a Correct answer

Michael Legacy answered on June 25, 2019 19:37

Do you know if there is a better way to get a total results count than running a second search query with a higher "DisplayResults" amount?

It seems pretty inefficient.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on June 25, 2019 21:17

check this answer

0 votesVote for this answer Mark as a Correct answer

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