<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: TechThrusters</title>
    <description>The latest articles on DEV Community by TechThrusters (@techthrusters).</description>
    <link>https://dev.to/techthrusters</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F5601%2Fb3345bb4-665c-4982-b461-387315dfb0be.jpeg</url>
      <title>DEV Community: TechThrusters</title>
      <link>https://dev.to/techthrusters</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/techthrusters"/>
    <language>en</language>
    <item>
      <title>Build a Custom Search Engine</title>
      <dc:creator>Mrunank Pawar</dc:creator>
      <pubDate>Tue, 09 Aug 2022 12:15:00 +0000</pubDate>
      <link>https://dev.to/techthrusters/custom-search-engine-3o1f</link>
      <guid>https://dev.to/techthrusters/custom-search-engine-3o1f</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;Build a personalised search engine with Google's search API.&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Just a heads up that this is not actually an entire search engine but you can definitely learn to play and customise this project as per your needs using Google's API&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We are going to build a custom search engine using Google's Search API to fetch queries and display the most relevant search results. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv93q0udvc81rkftk8nrv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv93q0udvc81rkftk8nrv.png" alt="Final Output" width="800" height="437"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To build this search engine, we're going to need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTML&lt;/li&gt;
&lt;li&gt;CSS&lt;/li&gt;
&lt;li&gt;Javascript&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://replit.com/~" rel="noopener noreferrer"&gt;Repl.it&lt;/a&gt; account&lt;/li&gt;
&lt;li&gt;Google account&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll also be getting our hands on Google APIs, and you don't need to worry if you don't know about how you can use it, because we'll be taking you through that as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt; Getting Started&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;We are providing you the &lt;a href="https://replit.com/@mrunankpawar/CSEHCTBStarter" rel="noopener noreferrer"&gt;starter files&lt;/a&gt; for this project and &lt;strong&gt;fork&lt;/strong&gt; it so you can make the changes and get started with building the search engine. The starter file includes the styles and preliminary code to help you kickstart this project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F25helgvi6hgpzcblznmq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F25helgvi6hgpzcblznmq.png" alt="Starter files" width="800" height="437"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once this is ready, head on over to the &lt;code&gt;index.html&lt;/code&gt; file and add the following between the &lt;code&gt;&amp;lt;body&amp;gt; &amp;lt;/body&amp;gt;&lt;/code&gt; tags:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img src="brewgle.png" width="525" class="image"&amp;gt;

&amp;lt;div class="search"&amp;gt;
  &amp;lt;input type="text" id="search" class="input"&amp;gt;
  &amp;lt;button id="submit" class="searchbtn" onclick="submit()"&amp;gt;Search&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will reference the image with it's corresponding styling! Feel free to add your own to make it personalized. Then, we are going to add a search input and button that calls submit() when clicked.&lt;/p&gt;

&lt;p&gt;Now, we need to create a section for the content to appear! Add the following right below the &lt;code&gt;&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="buttons" class="buttons"&amp;gt;
  &amp;lt;button id="allbutton" class="all" onclick="submit()"&amp;gt;&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div id="content"&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, let's reference the javascript files to connect the queries to the web!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="script.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script id="query" src="query.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt; Google API&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Supercool—the interface is now complete! Now let's fetch some queries. In your &lt;code&gt;query.js&lt;/code&gt; file, you should see a function that calls &lt;code&gt;submit()&lt;/code&gt;. Within that function, paste the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.getElementById('buttons').style.display = 'block';
document.getElementById('content').innerHTML = '';
var val = document.getElementById('search').value;
var newelement = document.createElement('script');
newelement.src = `https://www.googleapis.com/customsearch/v1?key=API_KEY&amp;amp;cx=003606982592251140240:5xbiwoxb3m0&amp;amp;q=${val}&amp;amp;callback=hndlr`;
newelement.id = 'mainscript';
document.body.appendChild(newelement);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we need to set up the Google Search API! To get started, head on over to &lt;a href="https://developers.google.com/custom-search/v1/overview" rel="noopener noreferrer"&gt;Google's Developer Portal&lt;/a&gt; and make sure that you are logged in. Once complete, scroll down to Get a Key and create a project called Search. Once you click next, copy your API key and click Done. In your &lt;code&gt;query.js&lt;/code&gt; file, you will see a variable called &lt;code&gt;newelement.src&lt;/code&gt;. Where it says &lt;em&gt;API_KEY&lt;/em&gt;, paste in your API Key and you're good to go!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4gtaclqkrxiieo8d1wex.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4gtaclqkrxiieo8d1wex.png" alt="Google Developer Portal" width="800" height="437"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So what is this URL and why is it so long?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;https://www.googleapis.com/customsearch/v1?&lt;/code&gt; lets us know that we are using the Google Custom Search API, the first version of it&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;key=API_KEY&lt;/code&gt; indicates the user of the API, as well as any limitations. For example, this will associate the number of queries per day with your account&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;amp;cx=&lt;/code&gt; associates any searches with a search engine Id&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;q=${val}&lt;/code&gt; is the actual query that a user inputs, which we will fetch from the submit() function in our HTML&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;callback=hndlr&lt;/code&gt; is used as a parameter that is called after the function is executed&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;u&gt; Formatting Query Results&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Now that this is completed, let's format our content. Head over to the script.js file and you should see a function called hdnlr(response). Inside this function, add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try {

} catch(error) {

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Essentially, the function will try some code, and if it does not work, will catch &amp;amp; output an error. Within the try function, add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.getElementById('content').innerHTML += `
  &amp;lt;div style="color: grey;"&amp;gt;
    Wohooo! About ${response.searchInformation.formattedTotalResults} results in ${response.searchInformation.formattedSearchTime} seconds!
  &amp;lt;/div&amp;gt;`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This fetches the amount of results and the time it took to retrieve, just like how you see on Google! Then, to fetch the actual information, add the following to your &lt;code&gt;try&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (var i = 0; i &amp;lt; response.items.length; i++) { 
  document.getElementById('content').innerHTML += `
  &amp;lt;div style="align-items: center;"&amp;gt;
    &amp;lt;br&amp;gt;
    &amp;lt;a style="color: grey; font-size: 12px; text-decoration: none;" href=${response.items[i].link} target="_blank"&amp;gt;${response.items[i].link}&amp;lt;/a&amp;gt;
    &amp;lt;a target="_blank" href=${response.items[i].link} style="text-decoration: none;"&amp;gt;
      &amp;lt;h2 style="margin-top: 2px;"&amp;gt;${response.items[i].title}&amp;lt;/h2&amp;gt;
    &amp;lt;/a&amp;gt;
    &amp;lt;div style="margin-top: -8px;"&amp;gt;
      ${response.items[i].htmlSnippet}
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;`;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, your code will fetch the &lt;code&gt;link, title and htmlSnippet&lt;/code&gt; to display for each query. To output any errors, add the following to the catch function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.getElementById('content').innerHTML = 'error!';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And just like that, you've successfully fetched your meta data for each query! Now, click 'Run' at the top of your Repl and watch the magic happen!&lt;/p&gt;

&lt;p&gt;If your code outputs an error, feel free to reference the final code!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt; Magic Time!&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7607xjs6hw4cb2ydso49.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7607xjs6hw4cb2ydso49.png" alt="Search Engine" width="800" height="437"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1uy0e2aa6le825sc1qyp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1uy0e2aa6le825sc1qyp.png" alt="Search query results" width="800" height="437"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's it! If you click run and head to your replit link, you should see a full functional search engine at your service! Here's a link to the &lt;a href="https://replit.com/@mrunankpawar/SearchEngine" rel="noopener noreferrer"&gt;final code&lt;/a&gt; (excluding the API key) if you need any help!&lt;/p&gt;

&lt;h2&gt;
  
  
  Few important points to remember:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Please do not share your API key with anyone&lt;/li&gt;
&lt;li&gt;Do not exceed 100 queries per day (Because Google provides only 100 free queries a day, or else you can exceed your limit by paying the extra charges)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Happy Hacking and join &lt;a href="https://techbrewers.hackclub.com/" rel="noopener noreferrer"&gt;Hack Club TechBrewers&lt;/a&gt; for building more amazing stuff 🚀&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>api</category>
      <category>javascript</category>
      <category>searchengine</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
