<?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: Ritesh Bhat</title>
    <description>The latest articles on DEV Community by Ritesh Bhat (@im_bhatman).</description>
    <link>https://dev.to/im_bhatman</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%2Fuser%2Fprofile_image%2F77408%2F5ac06a5c-8758-4cb8-8754-cbb270f4a31c.jpeg</url>
      <title>DEV Community: Ritesh Bhat</title>
      <link>https://dev.to/im_bhatman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/im_bhatman"/>
    <language>en</language>
    <item>
      <title>Components of Inverted Index - The Dictionary</title>
      <dc:creator>Ritesh Bhat</dc:creator>
      <pubDate>Sat, 29 Aug 2020 19:57:30 +0000</pubDate>
      <link>https://dev.to/im_bhatman/components-of-inverted-index-the-dictionary-1gf5</link>
      <guid>https://dev.to/im_bhatman/components-of-inverted-index-the-dictionary-1gf5</guid>
      <description>&lt;p&gt;This is the fourth article of the &lt;a href="https://dev.to/bhatman/inverted-index-the-story-begins-4l60"&gt;Inverted index series&lt;/a&gt; that I am writing on dev.to. We will be talking about the first component i.e The Dictionary in this article and other component i.e the Posting lists in the coming articles. This post is very closely related to my last article on &lt;a href="https://dev.to/bhatman/introduction-to-inverted-indexes-l04"&gt;Introduction to  Inverted Index&lt;/a&gt; Please do give it a read before reading this one for better understanding.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Please don't assume the dictionary as the python dictionary/HashMap. There is more to it.&lt;/code&gt; 😌&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Topics to be covered&lt;/strong&gt;
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; * Overview and need for Dictionary
 * Supported Operations
 * Types of Dictionary
 * Sort-based Dictionary
 * Hash-based Dictionary
 * Which one is better?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Overview&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let's recall the simple representation of the Inverted Index that we saw in our last post&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vbWYcU0Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8m6i1jb4oig5ohzdd6mx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vbWYcU0Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8m6i1jb4oig5ohzdd6mx.png" alt="Inverted Index Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;In this article, we are going to focus on the term column in the Inverted Index representation in the above diagram.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Since this term column contains all the words/terms in our collection, we also refer to it as our Inverted index's &lt;em&gt;dictionary&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;🙋 &lt;strong&gt;&lt;em&gt;But why do I need a dictionary?&lt;/em&gt;&lt;/strong&gt; &lt;br&gt;
The answer is pretty simple as seen in the diagram too, the main purpose of the dictionary terms is to manage a set of terms in a text collection and provide a mapping from the set of index terms to the locations of their posting lists. Posting lists column does not contain the data but the pointer/reference to the actual data store, they are just references to the actual text data available in-memory or over the disk.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Supported Operations by Dictionary&lt;/strong&gt; 🚀
&lt;/h2&gt;

&lt;p&gt;A "Basic" dictionary implementations in inverted indexes/search engines usually provide the following operations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Insert&lt;/em&gt; new entry to the dictionary&lt;/li&gt;
&lt;li&gt;Search/Find the key in the  dictionary:

&lt;ul&gt;
&lt;li&gt;Find a particular key/term and return the posting list entry.&lt;/li&gt;
&lt;li&gt;Find the entries for aa the terms which start with a given prefix.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Update the dictionary term entries as per the new incoming data. The delete operation can also be part of this since the deletion of old terms is kind of an update for the dictionary based on the new incoming data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will understand the Search/Find and insert operation right now in this post.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The problem in our beautiful dictionary?&lt;/em&gt;&lt;/strong&gt; ❓&lt;br&gt;
Let's assume my Dictionary has 10,000,000 terms. And I plan to search for the term "Scranton" in this dictionary. This itself becomes a problem since scanning/grepping through the dictionary will result in the time complexity of O(n). How do we reduce it down? Now take a seat and let me explain it to you with two of the most popular ways to achieve this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t4WCnUnK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4tnli6divek50c9opwvn.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t4WCnUnK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4tnli6divek50c9opwvn.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types to store the Dictionary terms&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sort based dictionary

&lt;ul&gt;
&lt;li&gt;Search Tree&lt;/li&gt;
&lt;li&gt;Lexicographical order list &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Hash-based dictionary.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Again there are no perfect solutions, you can choose the type based on your requirement. We will have a look at the pros and cons of each implementation which will help you to choose the better approach based on the problem/scenario.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sort based Dictionary 👀
&lt;/h2&gt;

&lt;p&gt;As the name suggests this implementation is based on the arrangement of our text collection(aka Dictionary) in a sorted form. This &lt;em&gt;lexicographically sorted form&lt;/em&gt; can be implemented in two ways, one is &lt;strong&gt;sorted arrays&lt;/strong&gt; and the other one is &lt;strong&gt;search trees&lt;/strong&gt;. Search over the text collection(aka dictionary) happens using &lt;strong&gt;binary search&lt;/strong&gt; in case of sorted arrays and &lt;strong&gt;tree traversal&lt;/strong&gt; in the case of search trees.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hash-Based Dictionary 👀
&lt;/h2&gt;

&lt;p&gt;For hash-based dictionaries, we can use hashtables. Where each term has a corresponding entry in the hashtable. Hashtables are amazingly fast when we are searching for a particular term but this has a catch too which we discuss later in the article(i.e prefix matches). Also, most of the people believe that search and insertions happen in hashtables at O(1) time complexity but IT IS NOT TRUE. To understand this you can read this answer on &lt;a href="https://stackoverflow.com/a/4363602/5687711"&gt;stackoverflow&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Comparison between Hash-based and Sort based dictionaries&lt;/strong&gt;. 🚀
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;If hashtable size is chosen properly, the hashtable implementation to search for a particular term is generally faster than the Sort based dictionary. Because unlike sort based approach binary search or tree traversal is not required.&lt;/li&gt;
&lt;li&gt;Let us consider the query which requires prefix matches like searching "Jef*" in a dictionary should match all index terms starting with "Jef" -&amp;gt; Jeff Bezos, Jeffrey Archer, etc. For this requirement, hashtables will require a system to scan the whole hashtable(i.e term collection) for this whereas in a Sort based approach is it will be much faster generally ~ O(log(n)). 
Because of this reason, the autocomplete feature on websites like amazon.com will be using (kind of a) prefix match over the product catalog data somewhere in the background and for it to be amazingly fast you you just cannot have it in O(n) time complexity, it has to be O(log(n)) or even less. And this data structure should preferably be an amazingly fast Search Tree.
Also, this is a major functionality that is expected from any search engine or an inverted index because at the end of the day you just want to type "lap" and get options as shown in the image here:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5P-IscaT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/q7faad4cngf1tr1hh1nd.png" alt="Alt Text"&gt;
(Accept it, you love this feature and you use it every day, accept this god damn it) 😜&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But as per our human tendency, we always want to know who's better than who, right? Messi VS Ronaldo? Robert DeNiro Vs Al Pacino? dev.to VS medium? 🙄&lt;/p&gt;

&lt;p&gt;🙋 So which one out of sort based and hash-based dictionaries is better? &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qhxijuYi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tb9qm8fv34gscqufcz0m.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qhxijuYi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tb9qm8fv34gscqufcz0m.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thomas Sowell once said: There Are No Solutions, Only Trade-offs.&lt;br&gt;
So, considering Thomas's statement the answer is &lt;strong&gt;Sort based Dictionaries using the Search trees&lt;/strong&gt;. (with decent tradeoffs in query processing time ofcourse).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Pxvyps0p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/x64ajjejpg6lkpbi0c5a.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Pxvyps0p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/x64ajjejpg6lkpbi0c5a.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Besides the prefix query support, predictable performance there is one more reason why I said "Sort based indexes are better". Ever heard of Lucene? the most popular search engine used by elastic search and Solr under the hood.&lt;br&gt;
For the Memory Index Apache Lucene full-text search index, Lucene uses Sort based dictionary approach. Won't believe me check the source code yourself. Here&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bbJO55up--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vo1c0e5qr48upne04ow5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bbJO55up--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vo1c0e5qr48upne04ow5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;code&gt;(I have asked the Lucene committers to confirm this, will update this too after getting the confirmation from them)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bonus Gyaan&lt;/strong&gt;:&lt;br&gt;
FYI: SortedMap is nothing but a beautiful &lt;strong&gt;Red-Black tree&lt;/strong&gt;. 🖖&lt;br&gt;
Also, the holy "Introduction To Algorithm" by Thomas Cormem says "Red-Black trees make good search trees." with proof on page 309. I will be covering this for sure in some of my future posts. Hope you can join the links from here.&lt;/p&gt;

&lt;p&gt;So we have discussed all the major tradeoffs between the Dictionary implementation in this article. For the next article, we will be looking into something again related to the dictionary implementation i.e the Tokenization of term. As of now we only consider "space" in between the sentences/documents to identify the terms but there are a lot of other things to be considered.&lt;/p&gt;

&lt;p&gt;Also, hope you liked the article and it was helpful to you. As always, I am open to suggestions and feedback w.r.t the series.&lt;/p&gt;

</description>
      <category>database</category>
      <category>java</category>
      <category>searchengines</category>
      <category>invertedindex</category>
    </item>
    <item>
      <title>Forward and Inverted Indexes - Requirement based differences</title>
      <dc:creator>Ritesh Bhat</dc:creator>
      <pubDate>Sun, 21 Jun 2020 08:55:02 +0000</pubDate>
      <link>https://dev.to/im_bhatman/forward-and-inverted-indexes-requirement-based-differences-1m3e</link>
      <guid>https://dev.to/im_bhatman/forward-and-inverted-indexes-requirement-based-differences-1m3e</guid>
      <description>&lt;p&gt;So this is my first post in the series of &lt;code&gt;#explainSearchLikeImFive&lt;/code&gt;. I hope you guys find this useful.&lt;/p&gt;

&lt;p&gt;This is also a part of my series named "Understanding Inverted Indexes".&lt;/p&gt;

&lt;p&gt;In this article, we try to understand the use of Forward and Inverted Indexes based on different requirements. The article is not about why the forward indexes are better than inverted, or vice-versa. Because both of them serve different purposes/requirements as explained in the post.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Topics to be covered&lt;/strong&gt;
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; * Definitions
 * Requirement 1 and using Forward Indexes
 * Requirement 2 and using Inverted Indexes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note: Forward Indexes are heavily used in traditional SQL databases like B-tree, Hash Indexes etc. So if you have ever heard of "indexes" in databases then chances are it was referring to &lt;em&gt;forward indexes&lt;/em&gt;. Whereas Inverted Index articles and documentation specifically mention &lt;em&gt;inverted index&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So now moving to the question of the hour, what is the main difference between Traditional forward indexes and inverted indexes?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Inverted Index stores the words as index and document name(s) as mapped reference(s).&lt;br&gt;
Forward Index stores the document name as index and word(s) as mapped reference(s).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But Ritesh you told me that you will explain it like I am five ? Stop this gibberish now..&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_uGmGNMh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qdb0h18zkyjxqwr8w28k.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_uGmGNMh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qdb0h18zkyjxqwr8w28k.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay, let me give you real-world examples of forward and Inverted Index which we all see in our daily lives.&lt;/p&gt;

&lt;p&gt;So I have this book with me "Team of Rivals", a great book written by Doris Kearns Goodwin about the political genius of Abraham Lincoln. Let's use this book to explain the difference between forward and reverse indexes.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D_VRj2Bg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/y5mxni0ijnw25d6au9m8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D_VRj2Bg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/y5mxni0ijnw25d6au9m8.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Requirement 1&lt;/strong&gt;: I know that I want to read the section "Showdown in Chicago" of the book, but I don't know which page it is on. &lt;/p&gt;

&lt;p&gt;So, how can I do this? How can I reach the "Showdown in Chicago" section of this 880 pages book?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 1 (Grepping)&lt;/strong&gt;: I will turn every page of the book from the beginning and check if it is the desired section. The technique is called &lt;em&gt;grepping&lt;/em&gt;. But the section "Showdown in Chicago" is on page 237. So the number of checks required to reach the section will be ~237, and this is not acceptable because of the time and effort required in this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 2 (Forward Indexes)&lt;/strong&gt;: Let's use Forward Indexes to solve this issue. You must have seen the first few pages which tell you about the exact location of the chapter/section, like this image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wp2PhNNq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/94ym46ikwgpzhoowiiin.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wp2PhNNq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/94ym46ikwgpzhoowiiin.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the actual idea of the working of forward indexes. Use a key(here chapter/section name) name to point to the specific record in the db (here starting of the content of the chapter in the book). So now the number of checks to reach the "Show Down in Chicago" gets reduced down to 1. Hence reducing the time and effort of our search. (It's not exactly 1 comparison but yes comparisons and time required in this approach are wayyyyyyyyy less than that of our approach 1 i.e grepping).&lt;/p&gt;




&lt;p&gt;Now look at the next requirement related to a term search. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Requirement 2&lt;/strong&gt;: I want to search for all the documents which have mentioned the term "Baltimore" in the book. And let me remind you there are 880 pages in the book. And more than 300,000 words. Therefore grepping(aka scanning) in this case will require you to make 300,000 comparisons. This is enough to make any sane man go crazy. &lt;/p&gt;

&lt;p&gt;So how do we do this? How do we find all the pages which have mentioned "Baltimore"?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 1(grepping)&lt;/strong&gt;: You know the run, check each and every term of the book from start to end, and note down the page which has mentioned "Baltimore". Again very time consuming as already seen for the Requirement 1 too.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 2 (Inverted Indexes)&lt;/strong&gt;: Since we are talking about searching a term in a large collection of documents(aka collection of chapters in this case) we can use Inverted Indexes to solve this issue, and yes almost all books use these Inverted Indexes to make your life easier. Just like many other books "Team of Rivals" has inverted indexes at the end of the book as shown in this image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--REcFcmWx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/m40ibvqdoapeo68l80e2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--REcFcmWx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/m40ibvqdoapeo68l80e2.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So after checking the Inverted indexes at the end of the book we know that "Baltimore" is mentioned on pages 629 and 630. So there are two parts in this &lt;em&gt;searching&lt;/em&gt; for "Baltimore" in the lexicographically ordered Inverted Index list and &lt;em&gt;fetching&lt;/em&gt; the pages based on the value of the index (here 629 and 630). The search time is very less for the term in the inverted index since in computing we actually use dictionaries(hash-based or search trees) to keep track of these terms and hence reduces down the search complexity from &lt;strong&gt;O(n)&lt;/strong&gt; to &lt;strong&gt;O(log n)&lt;/strong&gt; theoretically* when using the binary search or using a search tree, where &lt;strong&gt;n&lt;/strong&gt; is the numbers of words/terms in our index. &lt;/p&gt;




&lt;p&gt;GIST: Forward Indexes are used to map a column's value to a row or group of records. Whereas Inverted Indexes are usually used to maps the words/terms/content of a large document to a list of articles.&lt;/p&gt;

&lt;p&gt;There are many other differences but I don't want to go into too many jargon words/topics since this post is part of the #explainmelikeiamfive section. If you are interested in reading a lot more about the Inverted Index, you can follow this series &lt;a href="https://dev.to/bhatman/inverted-index-the-story-begins-4l60"&gt;Inverted Index - The Story begins&lt;/a&gt; and the corresponding posts where the topics discussed will be more at intermediate and advanced levels.&lt;/p&gt;

&lt;p&gt;You can read the same article on Github to on this link: &lt;a href="https://github.com/bhatman17/inverted-index-series/blob/master/introduction/diff-frwd-invrrtd-index/README.md"&gt;Forward Indexes and Inverted Indexes - Requirement based differences&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Your feedbacks are most welcome and if you think something can be improved about this post please feel free to write that out in the comment section.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>database</category>
      <category>sql</category>
      <category>nosql</category>
    </item>
    <item>
      <title>Introduction to Inverted Indexes</title>
      <dc:creator>Ritesh Bhat</dc:creator>
      <pubDate>Sun, 14 Jun 2020 20:51:22 +0000</pubDate>
      <link>https://dev.to/im_bhatman/introduction-to-inverted-indexes-l04</link>
      <guid>https://dev.to/im_bhatman/introduction-to-inverted-indexes-l04</guid>
      <description>&lt;p&gt;You can have a look at the &lt;a href="https://dev.to/hackslanger/inverted-index-the-story-begins-4l60"&gt;first post&lt;/a&gt; of the series here. To know about all the upcoming articles in the series.&lt;/p&gt;

&lt;p&gt;So this article is not going to tell you how to use inverted indexes in any DB/framework, but will give you a nice overview of what exactly an inverted index is, its basic structure, how is it different from traditional forward indexes and how are they used in Search engines.&lt;/p&gt;

&lt;h2&gt;
  
  
  Topics to look out for in this article:
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    * Introduction to Information Retrieval
    * What is an Inverted Index ?
    * Traditional database vs Search Engine
    * Components of Inverted Index
    * Dictionary
    * Posting Lists
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Introduction to Information Retrieval&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ksbk2Fae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u7rymi2xlx9j1853rsdo.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ksbk2Fae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u7rymi2xlx9j1853rsdo.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Let us suppose you wanted to determine what all news articles of The Washington Post contain the words "environment" and "health" since it's inception. One approach is to start at the beginning and to read through all the text, noting down each article which contains the mentioned words. This technique is generally refereed to as &lt;em&gt;grepping&lt;/em&gt; through text. And this process is going to take half your life to complete, pretty sad right ? So what shall we do ?&lt;br&gt;
One of the most popular way to avoid such linear scanning for each query is to &lt;em&gt;index&lt;/em&gt; the documents in advance. With proper indexing in place you can do the above mentioned task in like few seconds/minutes on a modern machines. One such index which is heavily used in indexing of large collection of data is &lt;em&gt;Inverted indexes&lt;/em&gt;. All of your popular search engines like Elasticsearch/Lucene/Solr use Inverted Indexes heavily to provide you with amazingly fast search systems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is an Inverted Index ? How does it make the info retrieval so fast?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To put in extremely simple words - "Reverse/Inverted Index provide a mapping between terms and their location of occurrence in a text collection". Therefore you don't need to scan the whole text collection to retrieve the information, which eventually reduces downs the search time.&lt;br&gt;
A lot of other functionalities like &lt;em&gt;ranked retrievals&lt;/em&gt;, &lt;em&gt;spell correction&lt;/em&gt; can be implemented over these Inverted Index system to provide hell lot of more functionalities.(These topics will be covered later in this series)&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Traditional Database (Forward Indexes) vs Search Engines(Inverted Index)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Lets us assume our System needs us to a collection which has 4 documents: Doc1, Doc2, Doc3, Doc4&lt;br&gt;
&lt;em&gt;Doc1&lt;/em&gt; : Welcome to the Hotel California Such a lovely place&lt;br&gt;
&lt;em&gt;Doc2&lt;/em&gt; : She's buying a stairway to Heaven&lt;br&gt;
&lt;em&gt;Doc3&lt;/em&gt; : Hey Jude, don't make it bad&lt;br&gt;
&lt;em&gt;Doc4&lt;/em&gt; : Take me to the heaven&lt;/p&gt;

&lt;p&gt;In traditional SQL DB the data will look something like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Doc ID&lt;/th&gt;
&lt;th&gt;Doc Content&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;1&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Welcome to the Hotel California Such a lovely place&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;2&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;she's buying a stairway to Heaven&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;3&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Hey Jude, don't make it bad&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;4&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Welcome to the heaven&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;And it is very clear that for any data/information retrieval on the basis of &lt;em&gt;Doc Content&lt;/em&gt; column is going to be difficult and complex. Performance in traditional SQL DBs is gained by querying over primary key or or by building efficient "indexes" for traversing these db tables. You can use inverted indexes in SQL DBs like postgresql, but they are not as efficient as they are in search engines like elasticsearch/lucene etc. The indexes used in SQL like B-Tree index(the default one), HashIndexes are kind of a &lt;em&gt;forward indexes&lt;/em&gt; where generally the mapping is from Document(aka doc Id) to the whole data row.&lt;/p&gt;

&lt;p&gt;Whereas, the same operation over a search engine is much simplified. Due to the use of reverse indexes, retrieval of information across huge number of documents is comparatively very easy and efficient when compared to the traditional dbs.&lt;br&gt;
In &lt;em&gt;Reverse Indexes&lt;/em&gt; the mapping is from "terms" to the Documents(as shown in the table below)&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Term&lt;/th&gt;
&lt;th&gt;Doc Id&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;buying&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Doc2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;california&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Doc1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Heaven&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Doc2, Doc4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;hotel&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Doc1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Jude&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Doc3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;lovely&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Doc1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;stairway&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Doc2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;welcome&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Doc1, Doc4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;and so on....&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;....&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This table shows how a simple inverted index works (Much complex implementation is discussed in future posts, but this will give you the gist of it). And it showcases the power of inverted indexes when terms are being searched.&lt;br&gt;
For example if you just search "welcome heaven", we don't have any exact match in the database but using the inverted index we can see that the user is looking for Doc4 or Doc2 or Doc1 (Doc4 having the highest rank score since it is in both the document list for the term welcome and heaven). &lt;/p&gt;

&lt;p&gt;To know more about the differences in between forward and inverted indexes you can read this article:&lt;a href="https://dev.to/bhatman/forward-and-inverted-indexes-requirement-based-differences-1m3e"&gt;Forward and Inverted Indexes- &lt;br&gt;
requirement based differences&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Components of Inverted Indexes
&lt;/h2&gt;

&lt;p&gt;Lets start with understanding the components of the inverted index. The two main components of a inverted index are &lt;strong&gt;Dictionary&lt;/strong&gt; and &lt;strong&gt;Postings Lists&lt;/strong&gt;. For each term in a text collection, there is a posting list which contains information about the term's occurrence in the provided collection.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Dictionary&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The dictionary works as a lookup data structure on top of the posting lists. Given an inverted index and a query, our first task is to determine whether each query term exists in the vocabulary. Like in The Washinton Post example we first need to identify if the word "environment" is actually available in our vocabulary i.e the inverted index and if so identify the corresponding postings. This lookup operation uses a classic data structure called the dictionary. It has two broad sections of solutions: &lt;em&gt;hashing&lt;/em&gt; and &lt;em&gt;search trees&lt;/em&gt;. We will be going through them in the next articles.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Posting List&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The actual index data is stored in posting list. It is accessed through the search engine's dictionary. Each term has its own postings list assigned to it.&lt;br&gt;
Since the actual size of posting list is too large and therefore its better to keep this stored over disk to reduce the cost. Of course the implementation of disk systems are much more complex than keeping this whole thing in RAM.&lt;br&gt;
Only during query processing are the query term's posting list is loaded into the memory, as required by the query processing routines.&lt;br&gt;
There is no fixed format of posting lists and index, there are alot of different versions for its index like docid-index, frequency index, positional index, schema independent etc. We will be going through them in the coming articles.&lt;/p&gt;

&lt;p&gt;Here is the diagram which shows a very simplified structure of an inverted index&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vbWYcU0Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8m6i1jb4oig5ohzdd6mx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vbWYcU0Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8m6i1jb4oig5ohzdd6mx.png" alt="Inverted Index Image"&gt;&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stop Words&lt;/strong&gt; : Some extremely common words that would appear to be of little value in helping select documents matching a query need are excluded from the the &lt;em&gt;vocabulary entirely&lt;/em&gt;. Like a, an, and, are, as etc.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Terms&lt;/strong&gt; on the left column of the inverted index table is contains the whole vocabulary of our collection which we have received from parsing N number of documents.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Documents/Posting list&lt;/strong&gt; column helps us to identify the location of the term in our collection. For example: identify in which document is a particular term occurring, at what position in document the term is occurring etc.  Other information like frequency of the term in doc, position of the term in doc etc can also be saved in the posting lists column with the document id. There are different ways to implement this too which are discussed in the coming articles.&lt;/p&gt;




&lt;p&gt;As of now we have provided a overview of components of the inverted indexes i.e dictionary and posting list. In the next article we will be discussing about the dictionaries and its implementations in-depth.&lt;/p&gt;

&lt;p&gt;If you feel some part of the article needs more clarification or some topics which can be covered in the series, please do put them in the comments below and I will for sure work on them. &lt;/p&gt;

&lt;p&gt;Thanks. Stay Safe, Stay Strong.&lt;/p&gt;

</description>
      <category>underdstandingsearchengines</category>
      <category>invertedindex</category>
      <category>java</category>
      <category>database</category>
    </item>
  </channel>
</rss>
