Copied to clipboard

Flag this post as spam?

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


  • Enrico 47 posts 252 karma points
    Dec 18, 2015 @ 16:26
    Enrico
    0

    Umbraco Web Api with Examine Lucene question

    Hello everyone, I am a newbie with Examine and Lucene. I wanted to use them to query the cache instead of hitting the database. It works! The search are performing well and quickly. The only problem that I have is to build queries.

    I want to focus my search on a specific 'nodeTypealias' and get all the 'nodeNames' connected. That was not particularly difficult. The problem started when I wanted to apply another filter to this selection and get only 'nodeName' starting with letter 'a' or 'b'.

    This is my Web APi code:

    public class PeopleApiController : UmbracoApiController
        {
    
    
    
            public IEnumerable<SearchResult> GetPeople(string letter = "") {
    
    
    
                var Searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
                var searchCriteria = Searcher.CreateSearchCriteria(BooleanOperation.Or);
                var q = "nodeTypeAlias:PeopleDetails";
                var query = searchCriteria.RawQuery(q);
    
    
                IEnumerable<SearchResult> results = Searcher.Search(query);
    
                return results;
            }
    
        }
    

    How could I combine the part with wildcards like :

    "nodeName:" + letter + "?";

    I tried also to do something like:

    public IEnumerable<SearchResult> GetPeople(string letter = "") {
    
                var Searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
                var searchCriteria = Searcher.CreateSearchCriteria("content");
                var query = searchCriteria.NodeTypeAlias("PeopleDetails").And().NodeName(letter + "?").Compile();
    
                IEnumerable<SearchResult> results = Searcher.Search(query);
    
                return results;
            }
    
  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Dec 18, 2015 @ 19:52
    Marc Goodson
    100

    Hi Enrico

    I think you are looking for the

    MultipleCharacterWildcard()

    extension method

    in

    using Examine.LuceneEngine.SearchCriteria; (add this as a using statement to get access to the extension method)

    eg

        var criteria = searcher.CreateSearchCriteria();
    var query = searchCriteria.NodeTypeAlias("PeopleDetails").And().NodeName(letter.MultipleCharacterWildcard()).Compile();         
    

    More documentation here on the Fluent Search API here

    https://github.com/Shazwazza/Examine/wiki/FluentSearchApi

  • Enrico 47 posts 252 karma points
    Dec 19, 2015 @ 11:14
    Enrico
    0

    Great Mark

    it worked like a charm

    Thanks again

  • Enrico 47 posts 252 karma points
    Jan 04, 2016 @ 11:08
    Enrico
    0

    Hi Mark,

    thanks to your help I built a function that works great with letters.

    This is the code, I hope it could be helpful to anyone that is dealing with the same feature

    HTML CODE :

    <div id="letter-container">
                <div>&nbsp;</div>
                <div><a href="#" >ALL</a></div>
                <div><a href="#a" >A</a></div>
                <div><a href="#b">B</a></div>
                <div><a href="#c">C</a></div>
                <div><a href="#d">D</a></div>
                <div><a href="#e">E</a></div>
                <div><a href="#f">F</a></div>
                <div><a href="#g">G</a></div>
                <div><a href="#h">H</a></div>
                <div><a href="#i">I</a></div>
                <div><a href="#h">J</a></div>
                <div><a href="#k">K</a></div>
                <div><a href="#l">L</a></div>
                <div><a href="#m">M</a></div>
                <div><a href="#n">N</a></div>
                <div><a href="#o">O</a></div>
                <div><a href="#p">P</a></div>
                <div><a href="#q">Q</a></div>
                <div><a href="#r">R</a></div>
                <div><a href="#s">S</a></div>
                <div><a href="#t">T</a></div>
                <div><a href="#u">U</a></div>
                <div><a href="#w">W</a></div>
                <div><a href="#x">X</a></div>
                <div><a href="#y">Y</a></div>
                <div><a href="#z">Z</a></div>
                <div></div>
       </div>
    

    JQuery code :

      $(function () {
    
            $("#letter-container a").on('click', function (e) {
                    e.stopImmediatePropagation();
                    e.preventDefault();
                    var element = $(this);
                    var letter = element.attr('href').replace('#','');
                    // console.log(letter);
    
    
                    $.ajax({
                        url: '/umbraco/api/peopleapi/GetPeople',
                        data:{letter : letter},
                        dataType:'json',
                        success: displayExamineResult,
                        error: displayError
    
                    });
                });
    
    
    
            var displayExamineResult = function (response) {
                    if (typeof response != 'undefined') {
                        if (response != null) {
                            $.each(response, function (index, value) {
    
                                // console.log(index + " " + value);
                                if (value.Fields != null) {
                                    $.each(value.Fields, function (i, v) {
                                       // your code to display results
                                        console.log( i + " : " + v);
                                    });
                                }
    
                            });
    
                        }
                        else {
                            console.log("No result found for the specified search criteria .");
                        }
    
                    }
                };
    
                var displayError = function (jqXHR, textStatus, errorThrown) {
                    console.log(errorThrown);
                };
    
    
     });
    

    The Api Controller Code :

    [HttpGet]
        public IEnumerable<SearchResult> GetPeople(string letter = "") {
            IEnumerable<SearchResult> results;
    
            var Searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
    
            if (string.IsNullOrEmpty(letter))
            {
                var criteria = Searcher.CreateSearchCriteria();
                var query = criteria.NodeTypeAlias("PeopleDetails").Compile();
                results = Searcher.Search(query);
                return results;
            }
            else {
                var criteria = Searcher.CreateSearchCriteria();
                var query = criteria.NodeTypeAlias("PeopleDetails").And().NodeName(letter.MultipleCharacterWildcard()).Compile();
                results = Searcher.Search(query);
                return results;
            }
    
    
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft