What is the best way to design a search engine that can identify similar results if it cannot find an exact match? (mongoDB)

I'm currently developing a question feature on my website that allows users to search for a specific question. For example, if someone searches "how do I cook a cake?" they should be able to find a link to a similar question titled "how do I make a cake?". However, at the moment, the user cannot find the second question when searching for the first one. I have only used the search bar in collection.find({}). How can I solve this issue? Is there an API available that can generate sentences with similar meaning to aid in searching?

Thank you!

Answer №1

Although not a full-fledged search engine, this solution showcases the best capabilities of MongoDB.

Refer to $text

  1. Create Index
db.articles.createIndex( { subject: "text" } )
  1. Data
db.articles.insertMany( [
     { _id: 1, subject: "coffee", author: "xyz", views: 50 },
     { _id: 2, subject: "Coffee Shopping", author: "efg", views: 5 },
     { _id: 3, subject: "Baking a cake", author: "abc", views: 90  },
     { _id: 4, subject: "baking", author: "xyz", views: 100 },
     { _id: 5, subject: "Café Con Leche", author: "abc", views: 200 },
     { _id: 6, subject: "Сырники", author: "jkl", views: 80 },
     { _id: 7, subject: "coffee and cream", author: "efg", views: 10 },
     { _id: 8, subject: "Cafe con Leche", author: "xyz", views: 10 }
] )
  1. Query
db.articles.find( { $text: { $search: "coffee" } } )
  1. Result
{ _id: 1, subject: 'coffee', author: 'xyz', views: 50 },
{ _id: 7, subject: 'coffee and cream', author: 'efg', views: 10 },
{ _id: 2, subject: 'Coffee Shopping', author: 'efg', views: 5 }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can we format a number to match the Brazilian number system while still maintaining its ability to be used in calculations?

Is there a known method to convert the number 123,123,214.93 into Brazilian currency format (123.123.214,93) for display purposes only? I attempted to achieve this conversion using a JavaScript function where I added periods after every 3 numbers and repl ...

Tips for embedding external URLs into a div element without using an iframe

I need help loading an external URL into a div without using iframe, embed, or object tags. I have tried examples but they do not seem to be working properly. Here is an example of what I have tried: $("#testDiv").load("//localhost:8000/cities/Mountain%2 ...

Working with subdocuments in MongoDB for updates

I'm currently using MongoDB version 3.4.13 and I need to insert a new element into this specific document: { "_id" : ObjectId("5981d38cf43047f3235febbc"), "code" : "A0001", "subItems": [ { "_id" : ObjectId("5981d38c ...

Delete items from the array based on the relationship between them

Looking for a way to remove elements in an array based on their relationships with each other? For example: const array = [ { id: 14098, parent: 14096, }, { id: 14100, parent: 0, }, { id: 14096, pa ...

Tips for improving the product filtering functionality in a React application

To solve the problem, we need to develop a Filter.jsx component that will be responsible for filtering Products.jsx (available in the repository). Current React Components : Main.jsx import React from "react"; import { Products } from "../ ...

Encountered a issue during the installation of an NPM module

I am a beginner in the world of web development. I attempted to set up an npm module using the command npm install grunt-contrib-watch --save-dev, however, I encountered the following error: npm WARN npm npm does not support Node.js v0.10.37 npm WARN npm ...

Updating objects within MongoDB arrays

Currently, I am working on updating an array of user information objects in mongoose. During the login process, I saved the core user information and now I want to update some of the user information when they place an order. Below is the model code: const ...

Creating a Node.JS environment with Express and Socket.IO: A beginner's guide

Trying to set up a Node.JS server on port 3800 of my CentOS server and encountering difficulties: var app = require('express')(); var http = require('http').Server(app); app.get('/', function(req, res){ res.send('&l ...

The art of sketching precise lines encircling a circular shape through the

What is the best way to use a for loop in JavaScript to draw lines around a circle, similar to those on a clock face? ...

Vue.js not populating select option tags

I've encountered an issue with my profie.html code that is intended to display division dropdown values using vue.js v-for and {{ val.division_name }}. However, the dropdown is rendering blank values even though there are supposed to be values present ...

Apply Data Filtering upon Submission

I'm in the process of developing a flexible search filter that can display or hide properties based on specific states and cities, while also updating the count to reflect the current search results. Although I've made progress, I'm encount ...

Effective methods for transferring parameters between two separate JavaScript files within an express.js application

Currently, I am working with Express.js and facing a challenge in passing parameters from one JavaScript file to another. How can this be accomplished? The two files involved are 1. process.js var WebPageTest = require('webpagetest'); var wpt ...

Attempting to verify the existence of a value in an SQL table prior to insertion

I have a simple table that contains an ID column and a location name column. Additionally, I have created an HTML form where users can input a new location into the table. Before adding the new location, I want to check if that location already exists in m ...

The $.ajax method seems to be experiencing some issues, but fortunately, the $.getJSON

I'm in the process of retrieving the share count for Pinterest and the following code is functioning effectively: var pi_like_count = 0; PIUrl = "https://api.pinterest.com/v1/urls/count.json?url=" + url1 + "&format=jsonp" + '&callback=? ...

Is it advisable to start a function within the componentDidMount() method in ReactJS when using promises?

I am developing an application that utilizes promises to manage animations. After a few attempts, I encountered the following error message: Warning: Can’t call setState on a component that is not yet mounted. This is a no-op, but it might indica ...

Manipulating arrays with added and removed bound class methods

let array: any[] = []; class Test { constructor() { // adding a bound method to the array array.push(this.testMethod.bind(this)); console.log('xxx', array); // expecting [ [Function: bound ] ], actually gettin ...

Maintain the newly selected background color for the current day in fullcalendar when navigating to the next or previous month

Currently, I am working on a project in React using npm fullcalendar. One of the requirements is to change the color of the current day. After some trial and error, I was able to achieve this by adding the following code: $('.fc-today').attr(&ap ...

Finding clarity amidst the chaos of require and export in Express.js (Node.js)

Is there a way to make my socket.io connection modular so that it can run once and be accessible anywhere? How can I export it? var server = require("http").Server(express); var io = require("socket.io")(server); server.listen(5000); io.on('connect ...

an array or objects within another array or objects

I've been working on arrays and objects, but I'm still struggling with creating nested objects and arrays. I have a particular array setup that I want to convert into a nested structure, but I'm finding it difficult. I'm not sure if it ...

When file type plugin indent is enabled, Vim struggles to properly indent the text

I currently have the filetype plugin indent setting in my ~/.vimrc file, but it seems to be causing issues with the indentation of JSON objects. When I start vim using vim -N -u NONE <filename.js> I am enabling both :filetype plugin indent on and : ...