How to do Indexing in MongoDB with Elastic Search? Part 1

Nwdys its very mmn t hve serh feture in ny website r . This usully hens with ltfrms tht hve lts f infrmtin t ffer t their users.

In project we have faced this issue as we need to do indexing in MongoDB and we have achieved it using Elastic Search.

Nwdys its very mmn t hve serh feture in ny website r . This usully hens with ltfrms tht hve lts f infrmtin t ffer t their users. Frm e-mmere websites whih hve thusnds f rduts in different tegries, t blgs r news sites whih hve thusnds f rtiles.

Whenever lient/user/reder rehes this kind f websites, they utmtilly tend t find serh bx where they n tye query t get t the seifi rtile/rdut/whtever theyre lking fr. Hving bd serh engine leds t frustrted users whih will mst rbbly never me bk t ur websites gin.

Full text serh wers ll thse serh bxes yu use dily in websites t find the stuff yu lk fr. Whenever yu wnt t find tht btmn hne se in the mzn rduts dtbse, r when yu serh fr ts lying with lser lights vides n Yutube. f urse these huge websites rely n mny ther things tht wer u their serh engines, but the bse f ll serhes is full text indexes. Tht sid, lets see wht this st is but.

MngDB Limittins

If yu quikly d ggle serh fr MngDB full text yull find in the MngDB ds tht full text serh is surted. S why wuld we bther lerning new mlex tehnlgy like Elsti Serh, nd why wuld we wnt t intrdue new mlexity int ur system rhiteture? Lets hve lk t MngDB text serh surt t find ut the resns.

I will ssume yu lredy hve MngDB instlled nd tht yu knw the bsis f it. If thts the se, then g hed nd en nsle nd run the mng mmnd t ess the MngDB nsle nd rete dtbse lled fulltext.


ur test dtbse will stre rtiles, s lets dd lletin whih well ll rtiles.


Nw lets dd few duments tht will be useful t test. Well insert rtiles with title nd rgrh s ntent. Ive tken sme rgrhs frm tw rtiles in the New Yrk Times Delbk.

riginl rtile referene: Yhs Sle t Verizn Leves Shrehlders With Little Sy


riginl rtile referene: hinese Gru t y $4.4 Billin fr esrs Mbile Gmes


Nw tht we hve duments, we need t index them using MngDB text index. S lets rete text index in bth the title nd ntent fields f the rtiles lletin:


Index reted, nw its time t d sme serhes t see hw tht ges, lets see!


Good, seems its wrking fine, we serhed fr the wrd hinese nd it mthed with the rtile but the hinese gru. Nw lets mke it bit hrder fr MngDB. Lets sy we wnt t build n utmlete inut (ne f thse tht remmend the user s he/she tyes n it). Fr this t wrk, I will ssume tht MngDB will return the sme rtile if I serh fr the wrd hi:


Emty! This is ne f the biggest limittins tht MngDB hs n the full text serh feture. The rblem is tht it indexes duments n the wrd level, s its imssible by using text index t d wht its lled rtil mthing. This is, mthing rtil rts f wrd.

t this int is when mre werful text indexing ltfrm is useful. In ur se Ive hsen Elsti Serh, minly beuse dumenttin is suer helful, nd it rvides ut f the bx full set f RESTful I endints tht mkes it very esy t test.

We'll have a deeper look at elasting search in the second part of our article.

Original post can be found here.

Interested in upgrading your skills? Check out our trainings.

Siddharth Garg
Software Development Engineer

Share the knowledge

Still have questions?
Connect with us
Thank you.
Your request has been received.
Thank you!
The form has been submitted successfully.