Creating a bag of words plays an important role. There are lot of documents in the web. The idea of creating a bag of words for each document is not so good, as it increases the time and space constraints. So many search engines makes use of Inverted Index data structure.
Above image makes you understand clearly about the creation of bag of words.
The purpose of an inverted index is to allow fast full text searches. at a cost of increased processing when a document is added to the database. It is the most popular data structure used in document systems, used on a large scale for example in search engines.
I think we will use this data structure in our search engine project.