Copied to clipboard

Flag this post as spam?

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


  • Steven McCurrach 7 posts 97 karma points
    Aug 17, 2018 @ 00:20
    Steven McCurrach
    0

    Lucene Search Query

    Hi guys,

    I'm pretty new to Umbraco and I'm trying to get a more advanced search on the go.

    I've managed to implement a simple site search using the default ExternalIndexer, but I'm looking to create another search that uses my own Indexer.

    I've updated my ExamineSettings.config and ExamineIndex.config files and verified my new index is working as intended by using the Examine Management tab and running searches using my new search indexer that appears under 'Searchers'.

    I have a simple Partial View setup, using the code from the Fluent API section of the quick start guide, however, I can't seem to render anything other than the node Id.

    Here's my Partial View code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Examine.LuceneEngine.SearchCriteria;
    
    @{ 
    var query = Request.QueryString["query"];
    var searcher = Examine.ExamineManager.Instance.SearchProviderCollection["MySearchSearcher"];
    
    var searchCriteria = searcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);
    var searchQuery = searchCriteria.Field("nodeName", query.Boost(5)).Or().Field("nodeName", query.Fuzzy()).And().OrderByDescending("createDate");
    var searchResults = searcher.Search(searchQuery.Compile());
    if(searchResults.Any())
     {
        <ul>
            @foreach (var result in searchResults)
            {
                <li>
                    @result.Id;
                </li>
            }
        </ul>
     }
    }
    

    This renders the ID of expected results, but when I try to use @result.nodeName or @result.Url, I get a compiler error message:

    CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
    

    How can I output and render other values, including custom doctype fields?

  • Alex Brown 129 posts 620 karma points
    Aug 17, 2018 @ 10:16
    Alex Brown
    100

    .Search() returns an ISearchResults object.

    You should be able to access these properties similar to how you would a dictionary.

    E.g.

    @result["nodeName"]
    

    Just note that if the property doesn't exist then it'll throw an exception. You can use

    @result.Fields.ContainsKey("nodeName")
    

    If you don't want to debug you can print out all the fields and their values:

    @foreach(var field in result.Fields)
    {
        <div>
            @field.Key @field.Value
       </div>
    }
    
  • Steven McCurrach 7 posts 97 karma points
    Aug 30, 2018 @ 20:45
    Steven McCurrach
    0

    This is just what I was looking for, thanks!

    Had no idea it returned results in a different format from normal and that the code to output it was different as a result. They should really add that to the original help file!

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft