How to implement fast search on Azure Blob?
- by Vicky
I am done with writing the code to upload files (text files) to azure blob storage. Now I want to provide search based on text files content. For ex. If I search for "Hello" then the name of files that contains "Hello" words should appear in search result. Here my code to search
class BlobSearch
{
static void Main(string[] args)
{
string searchText = "Hello";
CloudStorageAccount account = CloudStorageAccount.Parse(azureConString);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference("MyBlobContainer");
blobContainer.FetchAttributes();
var blobItemList = blobContainer.ListBlobs();
foreach (var item in blobItemList)
{
string line = string.Empty;
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(item.Uri.ToString());
if(blockBlob.Name.Contains(".txt"))
{
int lineno = 1;
using (var stream = blockBlob.OpenRead())
{
using (StreamReader reader = new StreamReader(stream))
{
while ((line = reader.ReadLine()) != null)
{
if (line.IndexOf(searchText) != -1)
{
Console.WriteLine("Line : " + lineno +" => "+ blockBlob.Name);
}
lineno++;
}
}
}
}
}
Console.WriteLine("SEARCH COMPLETE");
Console.ReadLine();
}
}
Above code is working but it is too slow. Is there any way to do it faster or Can improve above code.