Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom 4 posts 104 karma points
    Jul 28, 2017 @ 03:10
    Tom
    0

    Azure Search Front End Implementation

    Hey guys, I have installed the Moriyama Azure Search package and currently have the back office running through Azure Search. I already have my data indexed within the search service, but am having trouble pointing my search at my index from within Umbraco. Any help will be greatly appreciated.

    Thanks.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Jul 30, 2017 @ 09:57
    Marc Goodson
    0

    Hi Tom

    Do you mean the search in the backoffice ?

    or the search on the frontend of your site ?

    Anyway there should be included in the package an example of using the search on the front end of your site, in the file AzureSearch.cshtml

    (and in the read.me here; https://github.com/darrenferguson/UmbracoAzureSearch)

    But the gist of it is you need to construct an instance of the 'AzureSearchClient' class and use the methods off of this to query your index..

    eg

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Moriyama.AzureSearch.Umbraco.Application
    @using Moriyama.AzureSearch.Umbraco.Application.Interfaces
    
    @{
    //get your search client
     var azureSearchClient = AzureSearchContext.Instance.SearchClient;
    // read your search term from the querystring
    var searchTerm = Request["search"];
    var hasSearchResults = false;
    if (!string.IsNullOrEmpty(searchTerm)){
    
    //the search will return a SearchResult Class containing 'content' and 'facets'
    
    //but we're doing a basic 'search term' search here:
    ISearchResult searchResults  = azureSearchClient .Term(searchTerm).Results(); 
    hasSearchResults = searchResults.Content.Any();
    }}
    
    @if(hasSearchResults){
    
    <h1>Search Results</h1>
    <p>Count: searchResults.Count</p>
    
    <ul>
    @foreach (var searchResult in searchResults.Content){
    //each result is represented by a SearchContent class, which has properties for Id, Url, Path (IsContent,IsMedia etc) and a dictionary of your custom properties.
    
    <li>
    <a href="@searchResult.Url">@searchResult.Id @searchResult.Name</a>
    </li>
    }
    </ul>
    }
    

    You can chain up the search criteria on the search client, before you call .Results()

    for example, from a project listing news articles by category:

       var documentTypesFilter = catTagId > 0 ? new[] { "NewsArticle", "VideoArticle", "PhotoStoryArticle", "printArticle" } : new[] { "NewsArticle", "VideoArticle", "PhotoStoryArticle"};
    
                var client = new AzureSearchClient(_umbracoApplicationPath);
                var query = client
                    .Content()
                    .DocumentTypes(documentTypesFilter)
                    .Filter("Published", true);
                // only if a category is pickedwe need to filter on catId
                if (catTagId > 0)
                {
                    query = query.Contains("allTags", catTagId.ToString());
                }
                    query = query.PageSize(articlesPerPage)
                    .OrderBy("articleDate desc");              
    
                var newsArticlesResult = query.Results(page);
    

    I have a list of documentTypes that are the news article ones, and I build a 'content query' (.Content()) and chain on the filter by DocumentTypes, then I'm only interested in ones that are published, as this is the front end of my site, so I use .Filter and then I might be filtering by news article category tag - and so I can 'chain on again' (because I haven't called .results() yet, a Contains clause to see if an article has been tagged with that category and then I can set the number of articles to return per page via the PageSize, (the count property will still include the total number matched) , and finally tag on an OrderBy clause so I get the latest articles first.

    Does that help? let us know where you are coming unstuck?

    regards

    Marc

  • Tom 4 posts 104 karma points
    Jul 30, 2017 @ 17:30
    Tom
    0

    Hey Marc, thanks for the in depth response. Yes this is very helpful. I think this will help me overcome the issues that I have had implementing a front end solution.

    I have two indexes within my Azure Search Service ('umbraco', and a sample index generated from an azure sql database). I need to query the sample index via my front end, but as far as I can tell only one index can be referenced within the AzureSearch.config. Both indexes are recognized within the Azure search tab in Umbraco. Is there some way to reference my sample index via the front end within Umbraco?

    I'm new to Umbraco and the community, but you guys have been supremely helpful. Thanks again for all of your insights.

Please Sign in or register to post replies

Write your reply to:

Draft