How to search on empty search text with field searching?

Nathalie Vallières asked on October 9, 2020 18:42

Hello,

I am currently having issues setting up my Smart Search. I want to have the ability to search the whole index with no default search parameters.

When I arrive on the search page, I want the whole index to be displayed, paged of course, but without any search parameters, so I tried to pass an empty query and obtained an error:

Search text cannot be null or whitespace in the search pattern.
Nom du paramètre : searchText

I've looked around but haven't seen anything that indicates how to go around that problem as it just simply throws an error. I want to be able to use many different permutations, such as:

Empty query

string.Empty
+*

Search by date

+BirthDate:[20000101 TO 20201231]

Search by text (name) and date

+text* +BirthDate:[20000101 TO 20201231]

It seems like my query hangs up on having an empty searchText. I want the possibility of having an empty searchtext. How can I do that?

Thanks!

Here's my current code (haven't tested if fully due to empty search not working):

public static IEnumerable<ItemViewModel> GetItems(string searchQuery, string orderBy, int page, int pageSize, string searchIndex, out int totalResults)
{
    IEnumerable<string> searchIndexes = new List<string> { searchIndex };
    int pageNumber = page;
    UserInfo searchUser = MembershipContext.AuthenticatedUser;
    SearchPattern searchPattern = new SearchPattern(searchQuery, SearchOptionsEnum.FullSearch);
    SearchParameters searchParameters = SearchParameters.PrepareForPages(searchPattern, searchIndexes, pageNumber, pageSize, searchUser);
    searchParameters.SearchSort = orderBy;

    SearchResult searchResult = SearchHelper.Search(searchParameters);
    totalResults = searchResult.TotalNumberOfResults;

    IEnumerable<SearchResultItem> items = searchResult.Items;

    IEnumerable<ItemViewModel> newItems = items.Select(item => new ItemViewModel((CustomTableItem)item.Data));

    return newItems;
}

EDIT1: So after trying some new things, I found out that the SearchPattern wasn't the ideal solution, so I went with another way of querying, which always returns empty/null for some reasons...

public static IEnumerable<ItemViewModel> GetItems(string searchQuery, out int totalResults, string orderBy = "MyFieldName DESC", int page = 1, int pageSize = 4, string searchIndex = "My.Search.Index")
    {
        UserInfo searchUser = MembershipContext.AuthenticatedUser;
        SearchParameters searchParameters = new SearchParameters()
        {
            SearchSort = orderBy,
            SearchFor = (!string.IsNullOrEmpty(searchQuery)) ? searchQuery : null,
            SearchIndexes = searchIndex,
            StartingPosition = page,
            DisplayResults = pageSize,
            BlockFieldOnlySearch = false,
            NumberOfResults = 50000,
            NumberOfProcessedResults = 50000,
            User = MembershipContext.AuthenticatedUser
        };

        SearchResult searchResult = SearchHelper.Search(searchParameters);
        totalResults = searchResult.TotalNumberOfResults;

        IEnumerable<SearchResultItem> items = searchResult.Items;

        IEnumerable<ItemViewModel> models = items.Select(item => new ItemViewModel((CustomTableItem)item.Data));

        return models;
}

I have already checked my indexes and the amount of items in it and it had my custom table items in it, so it should return them when I pass string.Empty or null, doesn't it?

Correct Answer

Nathalie Vallières answered on October 14, 2020 19:13

I ended up finding the problem. We noticed our global search with Smart Search wasn't working too. We found out our Web farms were disabled. I reactivated it, restarted the sites, loaded up the CMS and MVC sites and rebuilt the index and now I'm getting results.

I also ended up using the first solution in my post but always passing a valid but always true parameter such as dates as "MyDate:[19000101 TO 21000101]" in my search queries.

Hope this helps someone else.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Juraj Ondrus answered on October 12, 2020 07:20

Wouldn't it better to filter the data using filter + data source + viewer web part? Although for the end user it might be still the same, there is a difference between searching and filtering.

When using smart search, some algorithms are executed to return the results based on some relevance and score. When using smart search filters - these are just adding another terms to the initial search term to refine the search results by relevance and score. But still, the results may not contain all items for “green shoes“. It is a search, just like you search on Google for something.

Using custom filters on the other hand you have defined the set of data. E.g. all products are displayed by default when no filter is set. And now, the user wants to display all “green shoes“ - so you will just filter the dataset, basically by adding a WHERE condition to the SQL query which is getting the data from the DB. In this case it is better to use filter+data source+viewer web part as described in the documentation. The data source will select all products by default, but the filter(s) will tell the data source to select only shoes this time, select only green shoes, green shoes size 10…etc.

Otherwise, you can use e.g. faceted search and configure the search filters accordingly. But this may not fit in 100% use cases and I would recommend using the above mentioned filtering.

0 votesVote for this answer Mark as a Correct answer

Nathalie Vallières answered on October 13, 2020 21:54

I am currently using Kentico 12.68 MVC, not the Portal Engine with webparts.

My index seems to work fine, however I can't seem to be able to search through it using the Search API. Maybe I need my own analyzer to run the query or something, but I can't figure out what exactly I need.

This is me, I forgot to swap to my own account before posting.

0 votesVote for this answer Mark as a Correct answer

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