Search capability using ElastisSearch and NEST Client in asp.net webapi


(Charles Kabue) #1

I am creating a asp.net webapi application with a search capability using elastisSearch ( id="Elasticsearch.Net" version="1.5.0" targetFramework="net45") and NESTclient (id="NEST" version="1.5.0" targetFramework="net45").
I have managed to get basic searching but its only searching full words
only. I mean, if I search for the term, “god is good” it will return all
the results matching the word “good”, and “god”, and sometimes “is”.
Could someone please guide me through on how to write a query that could
return results for good with the following terms, “go”, “goo”, “good”, “goodz” and even prefix matches, with all the results being highlighted, please. And how would I go about using typehead.js and bloodhood.js to return suggestions?
This is my implementation so far

SearchPaper class

public class SearchPaper
    {
        public string Id { get; set; }

        public string UnitName { get; set; }

        public string Course { get; set; }

        public string UnitCode { get; set; }

        public string School { get; set; }

        public string ApplicationUser { get; set; }

        public string Description { get; set; }

    }

indexing

var paper = new SearchPaper
                {
                    Id = newPaper.PastPaperID,
                    School = newPaper.School,
                    UnitName = newPaper.UnitName,
                    UnitCode = newPaper.UnitCode,
                    Course = _ctx.Courses.Find(newPaper.CourseID).Name,
                    ApplicationUser = _ctx.Users.Find(newPaper.ApplicationUserId).UserName,
                    Description = newPaper.Description
                };


var index = client.Index(paper, i => i
                .Index("epa")
                .Type("paper")
                .Id(newPaper.PastPaperID)
                .Refresh()
                .Ttl("1m")
            );

search

public async Task<IEnumerable> Search(string query)
        {

            var result = client.Search<SearchPaper>(s => s
                .From(0)
                .Size(10)
                .AllIndices()
                .MatchAll()
                .Index("epa")
                .Type("paper")
                .Query(q => q
                    .QueryString(qs => qs
                        .Query(query)))

                .Highlight(h => h
                    .OnFields(f => f
                        //.OnAll()
                        .OnField("*")
                        .PreTags("<em>")
                        .PostTags("</em>")))
            );



            /*
            var result = client.Search<SearchPaper>(s => s
                .From(0)
                .Size(10)
                .Query(q => q
                     .Term(p => p.Description, "design")
                )
            );


            var result = client.Search<SearchPaper>(s => s
                .From(0)
                .Size(10)
                .Index("epa")
                .Type("paper")
                .Query(q => q
                     .Term(p => p.Description, query)
                )
            );

            */
            return result.Documents;
        }

i really would appreciate if someone could sort me out on this...


(system) #2