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?