diff --git a/i2es/documents.go b/i2es/documents.go new file mode 100644 index 0000000..107f094 --- /dev/null +++ b/i2es/documents.go @@ -0,0 +1,47 @@ +package i2es + +// ESDoc Elasticsearch document structure +type ESDoc struct { + Echo string `json:"echo"` + Subg string `json:"subg"` + To string `json:"to"` + Author string `json:"author"` + Message string `json:"message"` + Date string `json:"date"` + MsgID string `json:"msgid"` + Tags string `json:"tags"` + Repto string `json:"repto"` + Address string `json:"address"` + TopicID string `json:"topicid"` + Misplaced string `json:"misplaced"` +} + +// ESConf ES connection settings +type ESConf struct { + Host string + Index string + Type string +} + +// ESRes ES response structure +type ESRes struct { + Took int `json:"took"` + TimedOut bool `json:"timed_out"` + Hits Hits `json:"hits"` +} + +// Hits Founded documents +type Hits struct { + Total int `json:"total"` + MaxScore float32 `json:"max_score"` + Hits []Hit `json:"hits"` +} + +// ES Document +type Hit struct { + Index string `json:"_index"` + Type string `json:"_type"` + ID string `json:"_id"` + Score float32 `json:"_score"` + Source ESDoc `json:"_source"` +} diff --git a/i2es/elastic.go b/i2es/elastic.go index cc6d39a..7a7a683 100644 --- a/i2es/elastic.go +++ b/i2es/elastic.go @@ -12,28 +12,6 @@ import ( "github.com/google/uuid" ) -// ESDoc Elasticsearch document structure -type ESDoc struct { - Echo string `json:"echo"` - Subg string `json:"subg"` - To string `json:"to"` - Author string `json:"author"` - Message string `json:"message"` - Date string `json:"date"` - MsgID string `json:"msgid"` - Tags string `json:"tags"` - Repto string `json:"repto"` - Address string `json:"address"` - TopicID string `json:"topicid"` -} - -// ESConf ES connection settings -type ESConf struct { - Host string - Index string - Type string -} - type topicid struct { es ESConf } diff --git a/i2es/topics.go b/i2es/topics.go new file mode 100644 index 0000000..153781c --- /dev/null +++ b/i2es/topics.go @@ -0,0 +1,49 @@ +package i2es + +import ( + log "github.com/Sirupsen/logrus" + "github.com/google/uuid" +) + +// AssignTopics ... +func (es ESConf) AssignTopics(messages []ESDoc) error { + for _, message := range messages { + // generate new topicid + if message.Repto == "" && message.TopicID == "" { + message.TopicID = uuid.New().String() + err := es.PutToIndex(message) + if err != nil { + log.Error(err.Error()) + continue + } + } + + // skip message with assigned topicid + if message.TopicID != "" { + continue + } + + // find top message of the thread and get they topicid + if message.Repto != "" { + t := topicid{es} + topicid, err := t.findTopicID(message.MsgID, 0) + if err != nil { + message.Misplaced = "yes" + err := es.PutToIndex(message) + if err != nil { + log.Error(err.Error()) + continue + } + continue + } + + message.TopicID = topicid + err = es.PutToIndex(message) + if err != nil { + log.Error(err.Error()) + continue + } + } + } + return nil +}