Indexing data can be done on any database or specialized search framework. The example provided in this article uses Elasticsearch to index data.
Index by Date:
The index format for indexing by date can be date/type/id.
Examples: 2017-03-19/logs/1, web-2017-03-19/logs/1 and mobile-2017-03-19/logs/1 and so on.
Executing the requests from code: Elasticsearch
*Use appropriate date formatting.
C#: myrestapi.post(“http://localhost:9200/”+new System.DateTime() + “/logs/” + userid.ToString(), userjson)
Java: myrestapi.post(“http://localhost:9200/”+new java.util.Date() + “/logs/” + userid.ToString(), userjson)
You can retrieve all the user’s logs on a particular date using the query.
C#: myrestapi.get(“http://localhost:9200/”+new System.DateTime() + “/logs/_search”)
Java: myrestapi.get(“http://localhost:9200/”+new java.util.Date() + “/logs/_search”)
Index by Country/Region:
The index format for indexing by country can be country/type/id
Examples: usa/users/1, web-usa/users/1 and mobile-usa/users/1 and so on.
Index by Words:
The index format for indexing by words can be word/type/id
You can create as many indexes as the number of words in English dictionary. I hope you don’t need all of them.
Example: mail/links/1, search/links/1, google/links/1, facebook/links/1
Whenever a user enters the word facebook in the search box and clicks enter, you can go to the index facebook/links to retrieve your top results.
[CBC show=”y” country=”in”]
[CBC show=”y” country=”us”]
Index by Sentences (Search Engines):
Indexing by sentences can be done similar to index by words, however this is where things get complex. Each of you can have your own algorithm based on your needs to index by sentences. Google, Yahoo and Bing all have their own ways of indexing links based on sentences. We will go through one simple strategy
- Display the top few results from the indices with each word (word indices) in the sentence. User clicks on one of those links.
- Now create a new index with the whole sentence as index name and add the user clicked link to this index.
- Next time when the user enters a similar sentence in the search box and clicks enter then go get the results directly from the sentence index.
- If no sentence index exists then do step 1.
- Eventually you will build enough sentence indexes to cater the needs of your search.
- Initially the search might be slow as it keeps on creating sentence indexes, once there are enough sentence indexes your search will be blazing fast.