Nowadays searching has become a necessary part of any web or mobile application. Consider a case of an E-Commerce website with no searching capabilities, you might end up going to competitor sites to find your favourite headphones or if you are an admin of the same website and checking logs for errors, viewing millions of logs without search is as good as useless for you.
We can use different architectures for storing data and searching. Using MongoDB as datastore and Elastic search for Searching is a commonly used architecture. MongoDB provides a NoSQL DBMS for storing huge amount of data. Elastic search on the other hand provides capability to store, index, search and analyse data in real time to extract value from datastore.
This article is hands on and mostly focuses on basic to advanced level searching in a healthcare application. Let’s start with making basic Patient model to store data in MongoDB using NodeJS and mongoose.
For simplicity we are considering the patient to contain name, type(Type of disease the patient has) and an array prescriptions containing local file paths where the prescription is stored as a file. We can perform following possible search operations on this model.
This one is pretty straight forward and we can use mongoose queries to achieve this easily.
This is when things start getting complicated and we introduce Elastic search to improve our search capabilities. We can still achieve this in MongoDB by making use of regular expressions and search using OR conditions in every field but elastic search can make our life much easier here.
We will use mongoosastic to integrate elastic search with MongoDB. Mongoosastic is a mongoose plugin which can automatically index your models into elastic search.
To achieve this, we need to first index the required fields using mongoosastic and then update our search query to use elastic search.
We have made a little complex Patient model here by adding another field Organization which is another Schema. In the above example name, email, phone, type and org.name are the indexed fields and we can search using these fields.
#apache-tika #full-text-search #product-development #elasticsearch #searching