Overview

These are notes on how to connect Django with AWS OpenSearch. The following article was helpful.

https://testdriven.io/blog/django-drf-elasticsearch/

However, since the above article targets Elasticsearch, changes corresponding to OpenSearch were needed.

Changes

Changes for OpenSearch were needed starting from the Elasticsearch Setup section of the article.

https://testdriven.io/blog/django-drf-elasticsearch/#elasticsearch-setup

Specifically, the following two libraries were required.

((eennvv))$$ppiippiinnssttaallllodpjeannsgeoa-rocphe-npsyearch-dsl

After that, by replacing django_elasticsearch_dsl with django-opensearch-dsl and elasticsearch_dsl with opensearchpy, I was able to proceed as described in the article.

For example, it looks like this:

#ffff@c@c@crrrrrlrlrlbooooeaeaealmmmmgsgsgsoisccisiccisa}c}tccgdddbsllsdllsu)a)yll/jjjltUaatCaatAttpaadaaaorsssra=ssrrheessonnngyesns}smf]ytsns}smf]yto''''g'''sns}smf]cggg..raeoi.efaeoi.irifluoind=aeoiuooomrDImtDdergiImtDdercdiasrdaeImtDdem.__oeonetjeleoenetjelel='rsei'msfnetjelecoodgcdi''ald''''grldi''ald''ge:stre:ecidi''ald''''noppeiue=nnnnsifluiyde=nnnnsndiDft_ns'ree=nnnnstccutneelsmxguug=diassDsxguug=aesoif_naf:ilxguug=iorpstnnste:'smmo='rseto.:'smmo=mstceinam=ipd:'smmo=tned.rssenubb:U,strecIcbb:Ceceuleameeftsabb:Altaapieeirts=ees[t_nruna=eea['rrmdlme'flii.r=eer[eettybaam_(erre_na_mttrrt,i_esde':ideoTtrrt'nee.rrpdDr{__rnamdeee{__epdn.s':eslnei{__i,tddaccooosooameonggoogtotO.:fl.d'xcooc'__uhhrcc'ffme'cteoffoic(bIfidIs:tlffl,ddt__tuu__e',u(rr__rouDjnfiesn.Fe__eaahddmmsr',mDFisrynmoetiel.tTfissrtt.ssCeehe,eoiehe'ecceeldOeeie'heeemllannapncesap,nutgldsbgxelaptto.tttrltul'rltmFeds.jetldrliidire)dimddieirs.TerFd(dimmemeg:sce(scneF.TecFisasceelpgo'an)'atliTextie.t'a''soir:st:s)deextFelTt:s,,rsy')':(lxtFilder'itt,1::1:pdtFied(x=1:mr,,r(Fiel()t',pDiA00o)ield),Ft0ooer,,p,eld(,iy,rcsteld(peptuird()rlemict(),od_Uemli),p(tsnpee,e)oetosr,_r,r=tst{itferirsiee=nlg{gdi'ss)t#ryCh#anCgheadngteodotpoenospeeanrsceharch

Populate Elasticsearch

The above article targeting Elasticsearch introduces the following command.

pythonmanage.pysearch_index-rebuild

On the other hand, for OpenSearch, the following commands were needed.

Creating Indices

pTCCCCyhorrrteneeehtaaaofitttnoniiilunnnmlegggao---nw?iiiaiucannngnsar[dddegettyeee.rei]xxxpisgceyn.ols'''dreucaoiis[sarpce.nettees]reins.osgcs'olew:.reai.isrly.e'cls.hO'.bK..ie.n.OdcKerOxeKactreeda:te

Registering Documents

pTCIII535yhonnntendddUCAhteeesarofixxxettnoniiirei3lunnngclegggsolmo---ureaw?535cyni535csan[UCAesuggUCAysarsucesar]ettscc.dettereifceporeis:gcuesyc.gcollssuol[OrelsfomrenKy:yfupey.]:ulen.oOillntOKnlyss:Kdyeeiawyxinriendclddehl,exxedb0edoed,ce,uir0mnr0edoenerertxsrre:roidorn:rsds:e:x

Rebuilding Indices

pythonmanage.pyopensearchindexrebuild

Additional: Adding Analyzers and Fields

Try the Field Classes described in the following section.

https://django-opensearch-dsl.readthedocs.io/en/latest/fields/#field-classes

In the following example, an html_strip analyzer and a Keyword field are set for username.

#fffffh@crrrrrtrlbooooomealmmmmmlgso_'tfcisuccgdddboshoihssll/jjjlpttklatUeaadaaaoermetrrsrssonnngnilne_yenafsns}smf]cggg.sp_irf.raniaeoiuooomesz=irDmaeImtDdem.__oa=te[leoellnetjelecoodrrr"tgcyddi''ald'''#noppecai=leiu=zse=nnnnsifltneelhnp"orsme=xguug=dia'stnnspa'sw=tefr{:'smmo='rsu.rssyl,te[eni='ubb:U,stspieeiyar"rtehrs=ees[t_eybaamiznch_(ltaerre_nr.rrpmedatdDdmwr{__rnanaccoprasmoosl'sooamauhhro(relcc._:'ffmemt__trd"_uuTs__e'ehddt",smmetfsr','.ssC,teexrihe,,mllaa"rnntieapo.tnsittFplrldireatp)i,ddiemeglo":essclpgoyp]l.'asoirz"dK:srsye,(e'itt,ry1:mr,"w,pDiAso0ooertnr,rcstoodtuikwFmicebiUemlnaesnpeilletozldr,re"(tr])f,}ireelgdisstry

As a result of the above, the following mapping was registered in OpenSearch.

{"}us"}emra"}spp"pr"}"}"}"}iof,i,l,u:npi"d"a"s""}"gert"tstetf,a{srsyytyryi"}n"ttp:p_pnperai_eeneaela"l:en"{"a"m"dwtysames"yz{"m::e:":"pee":er:"""":":""ti:tt{{:ene{e{::xt{xx{tett"""g""khe,etrym"wlo_rsdt"rip"

By using username.raw, sorting and aggregation become possible. Below is an example of views. Adding - appears to sort in descending order.

#iffffffcmrrrrrrlspooooooaeommmmmmsarssd@ddrtdorrbbeoaeecjpeellPrcbffhaaessooaiuc/bnnttgggam.g"agtevcgs__..ileae"nerxioeffdsninbn"dtyce.arroeaztseT(:ewhraacrte_trhrsqsrprsrpprstcmmuiercraieeeereeetre.theemad_latstl=asisrtitpppwwelOcaceufrpnuiuEnuyyooniplst_mr,sc"otflarxtrirrtzeasmqeneh-n('tlnc(nmikksense_trlus*sieepm..rSs=tehaef=se*zsp)Hoppvisehxoq.e*=eettroaima=NopdQugsr=rlittrgepirodr(eeenFsfoptiwomcNnes)snlasoe=.nRHnsrphoeshtefmeulgetQatoAnsoe,r.eanfseasttirPeiuxad.rd.etsppimAtIolpqtorcpl_oRoprVndruecah{afpenenotAi(ee_uw.rg.a:ssriresbsrqm"eeisgepitctweesy_exsnei(omli(li)eneparnenpAecAfo:xtcotia,soPDlP,vnp_uneaterIoeIe.rcts_lestVcSVqr"eleeqidtiueiur"sa(.uz_aLemreei"ss)heertiweiwrdisirreumna,ydo.ty_ssitl)enssscp=t,iL:n(e.elo5Oziqattan0fUemuro(ss0fsriectrse)se,trhae((erOy(lsrstDUf)).peePosf.vosracesqanuigureulslaimSteuetlneePre,sianray},zttig(rei,aiqhemroln)iqa.nCia.tundazts(eyateioss=teror)tTag,nt,r)o)(furC:oveyari)Dteoeqwcgu=uoesmrreeyylnS:fte)r"i{aqluiezreyr}"')

Summary

I hope this serves as a useful reference for connecting Django with AWS OpenSearch.