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.
( ( e e n n v v ) ) $ $ p p i i p p i i n n s s t t a a l l l l o d p j e a n n s g e o a - r o c p h e - n p s y e a r c h - d s l
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:
# f f f f @ c @ c @ c r r r r r l r l r l b o o o o e a e a e a l m m m m g s g s g s o i s c c i s i c c i s a } c } t c c g d d d b s l l s d l l s u ) a ) y l l / j j j l t U a a t C a a t A t t p a a d a a a o r s s s r a = s s r r h e e s s o n n n g y e s n s } s m f ] y t s n s } s m f ] y t o ' ' ' ' g ' ' ' s n s } s m f ] c g g g . . r a e o i . e f a e o i . i r i f l u o i n d = a e o i u o o o m r D I m t D d e r g i I m t D d e r c d i a s r d a e I m t D d e m . _ _ o e o n e t j e l e o e n e t j e l e l = ' r s e i ' m s f n e t j e l e c o o d g c d i ' ' a l d ' ' ' ' g r l d i ' ' a l d ' ' g e : s t r e : e c i d i ' ' a l d ' ' ' ' n o p p e i u e = n n n n s i f l u i y d e = n n n n s n d i D f t _ n s ' r e e = n n n n s t c c u t n e e l s m x g u u g = d i a s s D s x g u u g = a e s o i f _ n a f : i l x g u u g = i o r p s t n n s t e : ' s m m o = ' r s e t o . : ' s m m o = m s t c e i n a m = i p d : ' s m m o = t n e d . r s s e n u b b : U , s t r e c I c b b : C e c e u l e a m e e f t s a b b : A l t a a p i e e i r t s = e e s [ t _ n r u n a = e e a [ ' r r m d l m e ' f l i i . r = e e r [ e e t t y b a a m _ ( e r r e _ n a _ m t t r r t , i _ e s d e ' : i d e o T t r r t ' n e e . r r p d D r { _ _ r n a m d e e e { _ _ e p d n . s ' : e s l n e i { _ _ i , t d d a c c o o o s o o a m e o n g g o o g t o t O . : f l . d ' x c o o c ' _ _ u h h r c c ' f f m e ' c t e o f f o i c ( b I f i d I s : t l f f l , d d t _ _ t u u _ _ e ' , u ( r r _ _ r o u D j n f i e s n . F e _ _ e a a h d d m m s r ' , m D F i s r y n m o e t i e l . t T f i s s r t t . s s C e e h e , e o i e h e ' e c c e e l d O e e i e ' h e e e m l l a n n a p n c e s a p , n u t g l d s b g x e l a p t t o . t t t r l t u l ' r l t m F e d s . j e t l d r l i i d i r e ) d i m d d i e i r s . T e r F d ( d i m m e m e g : s c e ( s c n e F . T e c F i s a s c e e l p g o ' a n ) ' a t l i T e x t i e . t ' a ' ' s o i r : s t : s ) d e e x t F e l T t : s , , r s y ' ) ' : ( l x t F i l d e r ' i t t , 1 : : 1 : p d t F i e d ( x = 1 : m r , , r ( F i e l ( ) t ' , p D i A 0 0 o ) i e l d ) , F t 0 o o e r , , p , e l d ( , i y , r c s t e l d ( p e p t u i r d ( ) r l e m i c t ( ) , o d _ U e m l i ) , p ( t s n p e e , e ) o e t o s r , _ r , r = t s t { i t f e r i r s i e e = n l g { g d i ' s s ) t # r y C h # a n C g h e a d n g t e o d o t p o e n o s p e e a n r s c e h a r c h
Populate Elasticsearch# The above article targeting Elasticsearch introduces the following command.
p y t h o n m a n a g e . p y s e a r c h _ i n d e x - r e b u i l d On the other hand, for OpenSearch, the following commands were needed.
Creating Indices# p T C C C C y h o r r r t e n e e e h t a a a o f i t t t n o n i i i l u n n n m l e g g g a o - - - n w ? i i i a i u c a n n n g n s a r [ d d d e g e t t y e e e . r e i ] x x x p i s g c e y n . o l s ' ' ' d r e u c a o i i s [ s a r p c e . n e t t e e s ] r e i n s . o s g c s ' o l e w : . r e a i . i s r l y . e ' c l s . h O ' . b K . . i e . n . O d c K e r O x e K a c t r e e d a : t e
Registering Documents# p T C I I I 5 3 5 y h o n n n t e n d d d U C A h t e e e s a r o f i x x x e t t n o n i i i r e i 3 l u n n n g c l e g g g s o l m o - - - u r e a w ? 5 3 5 c y n i 5 3 5 c s a n [ U C A e s u g g U C A y s a r s u c e s a r ] e t t s c c . d e t t e r e i f c e p o r e i s : g c u e s y c . g c o l l s s u o l [ O r e l s f o m r e n K y : y f u p e y . ] : u l e n . o O i l l n t O K n l y s s : K d y e e i a w y x i n r i e n d c l d d e h l , e x x e d b 0 e d o e d , c e , u i r 0 m n r 0 e d o e n e r e r t x s r r e : r o i d o r n : r s d s : e : x
Rebuilding Indices# p y t h o n m a n a g e . p y o p e n s e a r c h i n d e x r e b u i l d
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.
# f f f f f h @ c r r r r r t r l b o o o o o m e a l m m m m m l g s o _ ' t f c i s u c c g d d d b o s h o i h s s l l / j j j l p t t k l a t U e a a d a a a o e r m e t r r s r s s o n n n g n i l n e _ y e n a f s n s } s m f ] c g g g . s p _ i r f . r a n i a e o i u o o o m e s z = i r D m a e I m t D d e m . _ _ o a = t e [ l e o e l l n e t j e l e c o o d r r r " t g c y d d i ' ' a l d ' ' ' # n o p p e c a i = l e i u = z s e = n n n n s i f l t n e e l h n p " o r s m e = x g u u g = d i a ' s t n n s p a ' s w = t e f r { : ' s m m o = ' r s u . r s s y l , t e [ e n i = ' u b b : U , s t s p i e e i y a r " r t e h r s = e e s [ t _ e y b a a m i z n c h _ ( l t a e r r e _ n r . r r p m e d a t d D d m w r { _ _ r n a n a c c o p r a s m o o s l ' s o o a m a u h h r o ( r e l c c . _ : ' f f m e m t _ _ t r d " _ u u T s _ _ e ' e h d d t " , s m m e t f s r ' , ' . s s C , t e e x r i h e , , m l l a a " r n n t i e a p o . t n s i t t F p l r l d i r e a t p ) i , d d i e m e g l o " : e s s c l p g o y p ] l . ' a s o i r z " d K : s r s y e , ( e ' i t t , r y 1 : m r , " w , p D i A s o 0 o o e r t n r , r c s t o o d t u i k w F m i c e b i U e m l n a e s n p e i l l e t o z l d r , r e " ( t r ] ) f , } i r e e l g d i s s t r y
As a result of the above, the following mapping was registered in OpenSearch.
{ " } u s " } e m r a " } s p p " p r " } " } " } " } i o f , i , l , u : n p i " d " a " s " " } " g e r t " t s t e t f , a { s r s y y t y r y i " } n " t t p : p _ p n p e r a i _ e e n e a e l a " l : e n " { " a " m " d w t y s a m e s " y z { " m : : e : " : " p e e " : e r : " " " " : " : " " t i : t t { { : e n e { e { : : x t { x x { t e t t " " " g " " k h e , e t r y m " w l o _ r s d t " r i p "
By using username.raw, sorting and aggregation become possible. Below is an example of views. Adding - appears to sort in descending order.
# i f f f f f f c m r r r r r r l s p o o o o o o a e o m m m m m m s a r s s d @ d d r t d o r r b b e o a e e c j p e e l l P r c b f f h a a e s s o o a i u c / b n n t t g g g a m . g " a g t e v c g s _ _ . . i l e a e " n e r x i o e f f d s n i n b n " d t y c e . a r r o e a z t s e T ( : e w h r a a c r t e _ t r h r s q s r p r s r p p r s t c m m u i e r c r a i e e e e r e e e t r e . t h e e m a d _ l a t s t l = a s i s r t i t p p p w w e l O c a c e u f r p n u i u E n u y y o o n i p l s t _ m r , s c " o t f l a r x t r i r r t z e a s m q e n e h - n ( ' t l n c ( n m i k k s e n s e _ t r l u s * s i e e p m . . r S s = t e h a e f = s e * z s p ) H o p p v i s e h x o q . e * = e e t t r o a i m a = N o p d Q u g s r = r l i t t r g e p i r o d r ( e e e n F s f o p t i w o m c N n e s ) s n l a s o e = . n R H n s r p h o e s h t e f m e u l g e t Q a t o A n s o e , r . e a n f s e a s t t i r P e i u x a d . r d . e t s p p i m A t I o l p q t o r c p l _ o R o p r V n d r u e c a h { a f p e n e n o t A i ( e e _ u w . r g . a : s s r i r e s b s r q m " e e i s g e p i t c t w e e s y _ e x s n e i ( o m l i ( l i ) e n e p a r n e n p A e c A f o : x t c o t i a , s o P D l P , v n p _ u n e a t e r I o e I e . r c t s _ l e s t V c S V q r " e l e e q i d t i u e i u r " s a ( . u z _ a L e m r e e i " s s ) h e e r t i w e i w r d i s i r r e u m n a , y d o . t y _ s s i t l ) e n s s s c p = t , i L : n ( e . e l o 5 O z i q a t t a n 0 f U e m u r o ( s s 0 f s r i e c t r s e ) s e , t r h a e ( ( e r O y ( l s r s t D U f ) ) . p e e P o s f . v o s r a c e s q a n u i g u r e u l s l a i m S t e u e t l n e e P r e , s i a n r a y } , z t t i g ( r e i , a i q h e m r o l n ) i q a . n C i a . t u n d a z t s ( e y a t e i o s s = t e r o r ) t T a g , n t , r ) o ) ( f u r C : o v e y a r i ) D t e o e q w c g u = u o e s m r r e e y y l n S : f t e ) r " i { a q l u i e z r e y r } " ' )
Summary# I hope this serves as a useful reference for connecting Django with AWS OpenSearch.