Assign topics after messages in db

This commit is contained in:
Denis Zheleztsov 2018-01-08 08:54:18 +03:00
parent c88d5fcc4a
commit e302295e6c
3 changed files with 96 additions and 22 deletions

47
i2es/documents.go Normal file
View File

@ -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"`
}

View File

@ -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
}

49
i2es/topics.go Normal file
View File

@ -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
}