UnifiedSearch yields no results in WebApi

Member since: 2011

Ok, this is pretty weird. When executing unified search in page controller context - it's working as expected (sample code):

 

public class SearchPageController : PageControllerBase<SearchPage>
{
    public ViewResult Index(SearchPage currentPage, string q)
    {
        var ctrl = new SearchController();
        var results = ctrl.Search(new SearchRequest { Query = q, PageSize = currentPage.ResultsCount });
        return View(new SearchPageViewModel(currentPage, q, results));
    }
}

    

WebApi controller:

public class SearchController : ApiController
{
    [HttpGet]
    public UnifiedSearchResults Search([FromUri] SearchRequest request)
    {
        var hitSpec = new HitSpecification
                      {
                          HighlightExcerpt = true,
                          ExcerptLength = 250,
                          PreTagForAllHighlights = "<strong><em>",
                          PostTagForAllHighlights = "</em></strong>"
                      };

        var results = SearchClient.Instance.UnifiedSearchFor(request.Query)
            .TermsFacetFor(p => p.SearchSection)
            .Skip(request.Page * request.PageSize)
            .Take(request.PageSize)
            .GetResult(hitSpec);

        return results;
    }
}

    

 

When accessing WebApi directly from client-side the same method it yields zero results (SearchRequest parameter in WebApi *is* binded correctly):

$.getJSON('/Api/Search/Search', { Query: q.val(), Page: 1, PageSize: 10 }, function (data) {
    alert(data);
});

    

#74013 Aug 17, 2013 9:38
  • What is the value of results when using ajax to call it?

    #74015 Aug 17, 2013 11:15
  • Member since: 2011

    Result is [].

    Results are empty already on server side.

    #74017 Aug 17, 2013 17:59
  • Same behavior if you make the call using the HttpClient class instead of instansiating the SearchController in SearchPageController´s Index method?

    #74020 Aug 18, 2013 13:55
  • Hi Valdis,

    I might guess that your problem is the skip/take. Your API call will set skip = 10 and Take = 10 meaning that you fetch page 11 to 20. If you set Page = 0 you will get the first 10 pages.

    Regards,

    Henrik

    #74025 Aug 19, 2013 9:27
  • Member since: 2011

    I can confirm that there is more than 50 hits. It's the case even without any paging. I'll try to compare packages sent to Find..

    #74027 Aug 19, 2013 10:01
  • Member since: 2006

    Did you find any solution to this. It seems that we have the same problem. GetFilesResult and GetResult for pages gives results, but when I use UnifiedSearchFor, no results gets returned. 

    The code I use for UnifiedSearchFor looks like this (using a query that yields results in the two other cases as mentioned):

    ITypeSearch<ISearchContent> search = SearchClient.Instance.UnifiedSearchFor(query, Language.Norwegian);
    UnifiedSearchResults res = search.GetResult();

    #74529 Aug 30, 2013 12:44
  • Could you check the optional parameters for the GetResult method and try to set the one for filtering for public display (or something like that) to false? I find it hard to believe, but it may be related to the access rights filtering.

    #74532 Aug 30, 2013 13:15
  • Member since: 2006

    That did the trick Joel.

    i.e, the code now looks like this and gives results:

    ITypeSearch<ISearchContent> search = SearchClient.Instance.UnifiedSearchFor(query, Language.Norwegian);
    UnifiedSearchResults res = search.GetResult(new HitSpecification(), false);

    #74533 Edited, Aug 30, 2013 13:33
  • Cool, but now unpublished content or content that a public visitor doesn't have read access to may appear in the result. I'm not entirely sure how to fix that as it depends on how CurrentPrincipal works. 

    #74534 Aug 30, 2013 13:35
  • Member since: 2011

    You know, my story is a bit different :) Without any changes either in code, index content or anything elsewhere - it started to give results back. I believe that this could be related with something happened on find service side.

    #74550 Aug 31, 2013 13:15
First   1 2   Last