{"id":28747,"date":"2024-04-08T21:48:07","date_gmt":"2024-04-08T20:48:07","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/creando-un-motor-de-busqueda-simple-con-elasticsearch-y-node-js\/"},"modified":"2024-06-03T18:43:51","modified_gmt":"2024-06-03T17:43:51","slug":"creando-un-motor-de-busqueda-simple-con-elasticsearch-y-node-js","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/creating-a-simple-search-engine-with-elasticsearch-and-node-js\/","title":{"rendered":"Creating a Simple Search Engine with Elasticsearch and Node.js"},"content":{"rendered":"<p>In today&#039;s digital age, the ability to quickly and efficiently retrieve information from large volumes of data is essential. Elasticsearch has established itself as a leading solution to facilitate complex searches and deliver results in real time. This tutorial will walk you through the process of implementing a simple search engine using Elasticsearch and Node.js, so you can effectively integrate it into your web development projects.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/creating-a-simple-search-engine-with-elasticsearch-and-node-js\/#%C2%BFQue_es_Elasticsearch\" >What is Elasticsearch?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/creating-a-simple-search-engine-with-elasticsearch-and-node-js\/#Preparativos_Iniciales\" >Initial Preparations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/creating-a-simple-search-engine-with-elasticsearch-and-node-js\/#Configurando_el_Proyecto_Nodejs\" >Setting up the Node.js Project<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/creating-a-simple-search-engine-with-elasticsearch-and-node-js\/#Estructura_Basica_del_Proyecto\" >Basic Project Structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/creating-a-simple-search-engine-with-elasticsearch-and-node-js\/#Conectandose_a_Elasticsearch\" >Connecting to Elasticsearch<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/creating-a-simple-search-engine-with-elasticsearch-and-node-js\/#Indexando_Documentos\" >Indexing Documents<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/creating-a-simple-search-engine-with-elasticsearch-and-node-js\/#Busqueda_de_Documentos\" >Document Search<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/creating-a-simple-search-engine-with-elasticsearch-and-node-js\/#Prueba_tu_Motor_de_Busqueda\" >Test your Search Engine<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/nelkodev.com\/en\/blog\/creating-a-simple-search-engine-with-elasticsearch-and-node-js\/#Conclusiones\" >Conclusions<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_Elasticsearch\"><\/span>What is Elasticsearch?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Elasticsearch is a distributed search and analysis engine, designed to handle large amounts of data very efficiently. It is widely used for full-text search applications, thanks to its ability to index and search documents in real time.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Preparativos_Iniciales\"><\/span>Initial Preparations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Before we dive into the code, you need to make sure you have Node.js and npm (Node&#039;s package manager) installed. You must also have Elasticsearch installed and running on your machine. You can download it from the <a href=\"https:\/\/www.elastic.co\/downloads\/elasticsearch\" rel=\"nofollow noopener\" target=\"_blank\">Elasticsearch official site<\/a> and follow the specific instructions for your operating system.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Configurando_el_Proyecto_Nodejs\"><\/span>Setting up the Node.js Project<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Create a new folder for your project and open a terminal in that location. Start a new Node.js project with the following command:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">npm init -y<\/code><\/pre>\n<p>This will create a file <code>package.json<\/code> with the basic configuration of your project. Now, install the necessary dependencies:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">npm install express body-parser elasticsearch<\/code><\/pre>\n<p>Here, <code>express<\/code> will be our web server, <code>body-parser<\/code> will help us process incoming JSON requests, and <code>elasticsearch<\/code> is the official Elasticsearch client for Node.js.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Estructura_Basica_del_Proyecto\"><\/span>Basic Project Structure<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Create a file called <code>app.js<\/code>. This will be our main file. Here we configure the Express server and define some basic entry points:<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">const express = require(&#039;express&#039;); const bodyParser = require(&#039;body-parser&#039;); const { Client } = require(&#039;@elastic\/elasticsearch&#039;); const app = express(); const client = new Client({ node: &#039;http:\/\/localhost:9200&#039; }); app.use(bodyParser.json()); app.listen(3000, () =&gt; { console.log(&#039;Server running at http:\/\/localhost:3000&#039;); });<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Conectandose_a_Elasticsearch\"><\/span>Connecting to Elasticsearch<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We have already initialized the Elasticsearch client in our code. Make sure Elasticsearch is running on <code>localhost<\/code> and in the port <code>9200<\/code>, which are the default values.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Indexando_Documentos\"><\/span>Indexing Documents<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In order to search for something, you first need to index some data. Let&#039;s define a route in Express to do just that:<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">app.post(&#039;\/index&#039;, async (req, res) =&gt; { try { const resp = await client.index({ index: &#039;books&#039;, body: req.body }); await client.indices. refresh({ index: &#039;books&#039; }); res.status(200).send(resp); } catch (error) { res.status(500).send(error.toString());<\/code><\/pre>\n<p>This endpoint accepts JSON documents via POST requests and indexes them under the index <code>books<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Busqueda_de_Documentos\"><\/span>Document Search<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let&#039;s define another endpoint to search for indexed documents:<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">app.get(&#039;\/search&#039;, async (req, res) =&gt; { try { const { query } = req.query; const resp = await client.search({ index: &#039;books&#039;, body: { query : { match: { content: query } } } }); res.status(200).send(resp.body.hits.hits); ());<\/code><\/pre>\n<p>This route listens to GET queries and performs searches based on the parameter <code>query<\/code> receiving.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Prueba_tu_Motor_de_Busqueda\"><\/span>Test your Search Engine<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Everything is set up. Now you can start your server with <code>node app.js<\/code> and use tools like Postman or cURL to index some documents and perform searches.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusiones\"><\/span>Conclusions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We&#039;ve seen how to set up a basic search engine with Elasticsearch and Node.js, from installing dependencies to creating endpoints to index and search documents. You can expand this basic project by incorporating more Elasticsearch functionality and refining the search logic to better fit your needs.<\/p>\n<p>To learn more about how to integrate these and other technologies into your projects, visit <a href=\"https:\/\/nelkodev.com\/en\/\">nelkodev.com<\/a> and if you have questions or need to contact me directly, feel free to visit <a href=\"https:\/\/nelkodev.com\/en\/contact\/\">nelkodev.com\/contact<\/a>. Exploring new tools and technologies is crucial in the field of web development, and I hope this tutorial has given you a good starting point for your own search projects.<\/p>","protected":false},"excerpt":{"rendered":"<p>In today\u2019s digital age, the ability to quickly and efficiently retrieve information from large volumes of data is essential. Elasticsearch has established itself as a leading solution for facilitating complex searches and delivering real-time results. This tutorial will guide you through the process of implementing a search engine [\u2026]<\/p>","protected":false},"author":1,"featured_media":28748,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,2207,1913],"tags":[1344,205,222,90,652,1923,1924,1193,2208,1008,1922],"class_list":["post-28747","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-nodejs","category-pruebas-de-backend","tag-backend","tag-blog","tag-busqueda","tag-con","tag-creando","tag-elasticsearch","tag-motor","tag-node-js","tag-nodejs","tag-pruebas","tag-simple"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=28747"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28747\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/28748"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=28747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=28747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=28747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}