BlogAtlas Vector Search voted most loved vector database in 2024 Retool State of AI reportLearn more >>
MongoDB Developer
Ruby
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Languageschevron-right
Rubychevron-right

Integrating Text Search in a Ruby on Rails Application

Humberto Diaz5 min read • Published Jun 12, 2024 • Updated Jun 17, 2024
Ruby
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
In the article "Getting Started with MongoDB Atlas and Ruby on Rails," Luce Carter showed us how to integrate MongoDB Atlas in a Ruby on Rails application named Inspiration. Users could capture and organize their creative ideas, thanks to the integration of MongoDB Atlas. In this article, we'll build upon the foundation laid out in Luce’s article and take a deeper dive as we integrate text search directly within our Rails application. By the end of this article, you will have a fully functional search feature embedded in your Rails app, allowing users to efficiently find the ideas they need.

Prerequisites

Before getting started, you will need to complete the following:
  1. Fork and clone a copy of the Inspiration app available on GitHub.
  2. Create a MongoDB Atlas cluster.
  3. Update the development config section inside config/mongoid.yml and add your own connection string.
  4. Ensure that you have data stored in your MongoDB Atlas cluster that you can query against Atlas Search. This should include the ideas you have added to your Inspiration application.
If you need help with any of these steps, please refer to the previous article.

Creating a text index in your ideas model

Now that we have our environment ready, we can begin by defining our search index, which we will use to query our database. As of Mongoid 9.0, you can define search indexes directly in your Rails model. We want to create a text index that will search through our description and name fields. Text indexes allow for text search queries on fields containing string content. Text indexes are ideal when searching for a specific word or phrase within string fields.
  • Open your idea.rb file
  • Replace the contents of your file with the below:
  • Run the db:mongoid:create_indexes Rake task:

Configure the search method in your model

We now have an index that we can use to conduct search queries, but we still need to define what a search is in the context of our model. We will do this by adding a search method in our Idea model. The method self.search(query): defines a class method named search that takes a single argument query.
  • Open your idea.rb file
  • Replace its contents with the content below:
When you call idea.search("example query"), Mongoid constructs and executes a MongoDB query that:
  1. Searches for documents where the text in the indexed fields matches the provided query string.
  2. Projects a score field into each document, representing the relevance score calculated by MongoDB.
  3. Orders the results by this relevance score in descending order, so the most relevant documents appear first.

Adding a search action in our IdeasController

To search for our Ideas, we need to first define what a search is, and we do that in our IdeasController.
Add the following code in your IdeasController:

Generating our search controller

Now that we have defined what constitutes a search, we will create a separate SearchesController to handle search requests and display the search results.
Run the following command to generate the SearchesController and the display_results view file.
Open your searches controller.rb file and add the following code.
Open the file display_results.html.erb and add the below, which will return our search results.
In your application.css file, add the below to create some basic styling for your search results:
With this setup, when you submit a search query, it will be routed to the SearchesController, which will call the search method in the Idea model to perform the search. The results will then be displayed in the display_results view.

Creating the search form

Now, we just need to create a simple UI so that users can submit queries directly in our Rails application.
  • Open your index.html.erb.
  • Copy and paste the code below to add a search form and a placeholder for search results.
  • Add the following styling for the search bar in your application.css file:

Updating our routes to include our search results

To redirect users to the display_results view file, we need to update our routes.rb.
Add the following get request to your routes file:

Running our local server

It’s time to test out our application. Begin by running the following command in your project directory:
Navigate to Port: 3000 and you should now be able to see a search bar appear at the top left corner of the web page.
Index pages listing out all ideas in our application

Submit a query on the app

To submit a search, add your query term in the search bar and click on “Search.”
Here are the results of a search in my MongoDB database for the word “pet”: A list of ideas are displayed matching the search term "pet."
Please note that search results are dependent on the contents of your own database. So make sure your database has the data you are requesting. However, as the complexity of your data increases, the need for more advanced search queries may be necessary. Refer to the MongoDB documentation for information regarding these queries.

Summary

And with that, we now have a fully functional search feature integrated into our Rails application. This integration not only enhances the usability and functionality of our application but can also help improve user engagement. Our Rails app now offers a powerful tool for users to find and manage their creative ideas effectively. Be sure to explore more about MongoDB Atlas and Rails in the community forums, share your experiences, or ask any questions you may have!

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

Calling the MongoDB Atlas Administration API: How to Do it from Node, Python, and Ruby


Jun 18, 2024 | 4 min read
Article

Why Use MongoDB with Ruby


Oct 07, 2022 | 4 min read
Article

TableCheck: Empowering Restaurants with Best-in-Class Booking Tools Powered by MongoDB


Dec 14, 2022 | 4 min read
Article

MongoDB ORMs, ODMs, and Libraries


Apr 02, 2024 | 3 min read
Table of Contents