Our searching is done in a way similar to Google or a site like Reddit does their searching. We have a global search bar that is a part of the navbar of the site. With this organization, users can easily find and use the search bar from any portion of the site. At a high level, the search bar allows the user to send a request to the server, which the server then processes, and the total results are returned pre-processed to the website which displays the search results in the same way that other models are displayed but but with some extra sorting logic in order to handle the complexities of comparing several models that might not share the same attributes.
The actual search is done using a POST request that contains the string that the user typed in. This string is sent to the server at the routing point
/search which handles the incoming request and generates the results. The results are sent back in a template called
search.html which is built off of our more generic card template called
models.html. This easily allows us to reuse our old code for a slightly different purpose. Inside the search backend, we split up the search into individual words and search across our database for strings that match those individual terms. The search function then goes through each of our models and combines the results into a centralized list. This list is then sorted based on the attribute selected with a sane default ("" for strings and 0 for ints) in the case that a model does not have the attribute that we are searching for. Finally, the sort list on the search page is comprised of the names of every attribute that is being sent over from the results. Specifically, it excludes attributes that are not being used. For example, if the search results only return food, then only attributes relating to food show up on in the sorting options. Depending on if an
and type search is desired, it might try to filter more or less specifically. All of these results are then compiled and displayed back to the user.