How to store info regarding which notifications have been read by user?

梦想与她 提交于 2021-01-29 11:55:30

问题


I have a set of notification or information items stored in elasticsearch. Once a user has seen a notification I need to mark it as seen by that user. A user can filter documents by read/unread status. Notifications will be viewed by lot of users and seen status will constantly get updated. What is the best way to store this data. Shall I store the list of users which have seen that notification in same document itself or shall I create parent child relationship.


回答1:


For sure you should avoid parent-child or nested type because they are computationial costful. the best way to achieve the relationship with a lot of data is to denormalize your data and put them in different indices. Please read here and here .Example:

PUT notification
{"mappings": {
    "properties": {
          "content": {
               "type": "text"},
           "id_notification":{
                "type":"keyword" }{
                   }}
}
}

Then user index:

PUT user
{"mappings": {
    "properties": {
          "general_information": {
               "type": "text"},
           "id_user":{
                "type":"keyword" }{
                   }}
}
}

another index for the relationship:

 PUT seen
{"mappings": {
    "properties": {
          "seen": {
               "notification_id":{
               "type": "keyword",
                "fields":{
                   "user_id":{
                "type":"keyword"}}},
           "unseen":{
               "notification_id":{
               "type": "keyword",
                "fields":{
                   "user_id":{
                "type":"keyword"}}}}
}
}

Sorry for the text format, i haven't kibana now. You should pay attention that to pass from information indices - user, notification - to the support index - seen - you should make a multi-index query - doc here. it will works because the name and the values of the fields - user_id , notification_id - are the same in different indices. The subfields user_id in seen index are array of keywords. However you could make user_id a single keyword and parent of notification_id keyword array field. In every case they keep the one to many realtionship, the best choice depends from your data



来源:https://stackoverflow.com/questions/60867705/how-to-store-info-regarding-which-notifications-have-been-read-by-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!