Best way to save chat messages in elasticsearch

We are currently using the instant messaging system on our platform. We need to provide our users with chat history and show the last 5 conversations that the user had (preview, for example, on facebook).

ipso facto we definitely need to think about how we can store all this data .

We use Elasticsearch , and we believe that it can be a reliable solution for storing chat messages and making them readable.

Our question is: what would be the best data structure in Elasticsearch so that our read operations are fast and not too heavy.

We were thinking of a big decision, and that might be the best thing we've come up with.

Our message may be:

{ 
   "ID" : 1,
   "sender" : "john",
   "receiver" : "doe",
   "content" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
   "date" : "timestamp"
}

We could use a nested object to store messages during a conversation:

 {
     "ID" : 317,
     "participants" : "john, doe",
     "date" : "timestamp of the last received message",
     "messages": [
         {
            "ID": "49753",
            "sender" : "john", 
            "receiver" : "doe",
            "content" : " Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
            "date" : "timestamp" 
         },
         {
            "ID": "49754",
            "sender" : "doe", 
            "receiver" :"john",
            "content" : " Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
            "date" : "timestamp" 
         },....
               ]
}

We would like to receive your feedback on this decision, as well as your decisions, if you succeed.

Thanks in advance

+4
source share
1 answer

. ( OP), . , "" .

Facebook ( - Elasticsearch)

enter image description here

: ( Messages )

, :

  • .
  • , > 3

: ( )

  • , , .

:

  • Typeahead: ( )
  • : ( )

, , , conversation ( ). , . ( doc).

conversation ( , , , ).

 {
     "ID" : 317,
     "participant_ids": [123456789, 987654321],
     "participant_names: ["John Doe", "Jane Doe"],
     "last_message_snippet" : " Lorem ipsum dolor sit amet, consectetur adipiscing elit...",
     "last_message_timestamp" : "timestamp of the last received message",
 }

b/c, , .

, , [ ] participant_ids last_message_timestamp.

typeahead, Elasureearch Term Suggestester participant_names.

conversation (vs message) , .

Index Per Timeframe ( , , , ).


conversation message, , conversation

 {
     "ID" : 4828274,
     "conversation_id": 317,
     "conversation_participant_ids": [123456789, 987654321],
     "sender_id": 123456789,
     "sender_name: "John Doe",
     "message" : " Lorem ipsum dolor sit amet, consectetur adipiscing elit",
     "message_timestamp" : <timestamp>,
 }

, , conversation_id message_timestamp.

message. ( Facebook).

, [ ] conversation_participant_ids message_timestamp.

, , Elasticsearch routing ( ), , conversation_id routing .


. Elasticsearch , . conversation message , "" Elasticsearch.

Elasticearch , , Elasticsearch - .

Messaging , , - , , , - " ", , "" , .

+9

Source: https://habr.com/ru/post/1654559/


All Articles