Search box ASP . NET

Asked

Viewed 177 times

1

I’m having doubts in the creation of an item search box. I already say that I’m a beginner.

I want when the user type a name in the search box to appear the items that contain the name that the user typed or part of it, for example: the user typed materials or mat then you should show up:

  • office supplies
  • automotive materials
  • etc....

My biggest question is how to implement this using DDD. I saw this example, and I found it relatively simple, but I can’t imagine a form of implementation. I saw that it implements Pagedlist.Mvc and found simple, but how to leave uncoupled (Using DDD)?

An example of what was implemented there

public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
   ViewBag.CurrentSort = sortOrder;
   ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
   ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

   if (searchString != null)
   {
      page = 1;
   }
   else
   {
      searchString = currentFilter;
   }

   ViewBag.CurrentFilter = searchString;

   var students = from s in db.Students
                  select s;
   if (!String.IsNullOrEmpty(searchString))
   {
      students = students.Where(s => s.LastName.Contains(searchString)
                             || s.FirstMidName.Contains(searchString));
   }
   switch (sortOrder)
   {
      case "name_desc":
         students = students.OrderByDescending(s => s.LastName);
         break;
      case "Date":
         students = students.OrderBy(s => s.EnrollmentDate);
         break;
      case "date_desc":
         students = students.OrderByDescending(s => s.EnrollmentDate);
         break;
      default:  // Name ascending 
         students = students.OrderBy(s => s.LastName);
         break;
   }

   int pageSize = 3;
   int pageNumber = (page ?? 1);
   return View(students.ToPagedList(pageNumber, pageSize));
}

In my view, there is another way to do this to leave uncoupled.

  • 1

    Could you post your code? Only with this it is difficult to help you.

  • @Randrade I’m just with the textbox code ready. Nothing else.

1 answer

0

There are two items to be implemented to achieve this kind of search-rich experience.

  1. Typeahead.js

    With this component you will be able to create this autocomplete or sugestions while searching in your app.

  2. Search Engine

    You can develop a backend to perform the searches, or use some service to do it in a more performative, relevant and scalable way. I strongly recommend using the Azure Search as a search platform. It is accessible and also very easy to implement. It has not only service research, but also has service of suggestions to do exactly what you need.

In my blog posted a course, free, with a Overview of Azure Search. In it I explain all the main Features of this Platform as a Service and as a consumer.

It also has a source code to demonstrate how to use each resource. From how to upload data to the service, to how to use the suggestions service with surveys Fuzzy - ex: vc search for "floers" and it suggests "flowers".

  • Very interesting what you posted but, for me this is still a bit performatic. I wanted a simpler solution.

  • @tek6, just create your own search backup. Typeahead is super simple to implement and use. But it should be "fed" in some way. Do it the way you know/find it easier. Then you "evolve" this solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.