Skip to content

Commit

Permalink
refactor: replace regex full-text search
Browse files Browse the repository at this point in the history
  • Loading branch information
bounoable committed Feb 1, 2022
1 parent c070ec9 commit 25afa14
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 229 deletions.
44 changes: 22 additions & 22 deletions plugin/archive/memory/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,35 @@ func filter(pm archive.Mail, q query.Query) bool {
}
}

if len(q.RFC) > 0 {
if !containsAnySubstring(m.RFC(), q.RFC) {
return false
}
}

if len(q.Texts) > 0 {
if !containsAnySubstring(m.Text(), q.Texts) {
return false
}
}

if len(q.HTML) > 0 {
if !containsAnySubstring(m.HTML(), q.HTML) {
return false
}
}
// if len(q.RFC) > 0 {
// if !containsAnySubstring(m.RFC(), q.RFC) {
// return false
// }
// }

// if len(q.Texts) > 0 {
// if !containsAnySubstring(m.Text(), q.Texts) {
// return false
// }
// }

// if len(q.HTML) > 0 {
// if !containsAnySubstring(m.HTML(), q.HTML) {
// return false
// }
// }

if len(q.Subjects) > 0 {
if !containsAnySubstring(m.Subject(), q.Subjects) {
return false
}
}

if len(q.SendErrors) > 0 {
if !containsAnySubstring(m.SendError(), q.SendErrors) {
return false
}
}
// if len(q.SendErrors) > 0 {
// if !containsAnySubstring(m.SendError(), q.SendErrors) {
// return false
// }
// }

attachments := m.Attachments()
if len(q.Attachment.Filenames) > 0 {
Expand Down
70 changes: 38 additions & 32 deletions plugin/archive/mongo/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/textproto"
"time"

"github.com/bounoable/mongoutil/index"
"github.com/bounoable/postdog/letter"
"github.com/bounoable/postdog/plugin/archive"
"github.com/bounoable/postdog/plugin/archive/query"
Expand Down Expand Up @@ -192,26 +191,26 @@ func (s *Store) createIndexes(ctx context.Context) error {
ctx, cancel = context.WithTimeout(ctx, 15*time.Second)
defer cancel()
}
_, err := index.CreateFromConfig(ctx, s.col.Database(), index.Config{
s.collectionName: []mongo.IndexModel{
{Keys: bson.D{{Key: "id", Value: 1}}, Options: options.Index().SetUnique(true)},
{Keys: bson.D{{Key: "sentAt", Value: 1}}},
{Keys: bson.D{{Key: "subject", Value: 1}}},
{Keys: bson.D{{Key: "from.name", Value: 1}}},
{Keys: bson.D{{Key: "from.address", Value: 1}}},
{Keys: bson.D{{Key: "recipients.name", Value: 1}}},
{Keys: bson.D{{Key: "recipients.address", Value: 1}}},
{Keys: bson.D{{Key: "to.name", Value: 1}}},
{Keys: bson.D{{Key: "to.address", Value: 1}}},
{Keys: bson.D{{Key: "cc.name", Value: 1}}},
{Keys: bson.D{{Key: "cc.address", Value: 1}}},
{Keys: bson.D{{Key: "bcc.name", Value: 1}}},
{Keys: bson.D{{Key: "bcc.address", Value: 1}}},
{Keys: bson.D{{Key: "attachments.filename", Value: 1}}},
{Keys: bson.D{{Key: "attachments.contentType", Value: 1}}},
{Keys: bson.D{{Key: "attachments.content", Value: 1}}},
{Keys: bson.D{{Key: "attachments.size", Value: 1}}},
},
_, err := s.col.Indexes().CreateMany(ctx, []mongo.IndexModel{
{Keys: bson.D{{Key: "id", Value: 1}}, Options: options.Index().SetUnique(true)},
{Keys: bson.D{{Key: "sentAt", Value: 1}}},
{Keys: bson.D{{Key: "subject", Value: 1}}},
{Keys: bson.D{{Key: "from.name", Value: 1}}},
{Keys: bson.D{{Key: "from.address", Value: 1}}},
{Keys: bson.D{{Key: "recipients.name", Value: 1}}},
{Keys: bson.D{{Key: "recipients.address", Value: 1}}},
{Keys: bson.D{{Key: "to.name", Value: 1}}},
{Keys: bson.D{{Key: "to.address", Value: 1}}},
{Keys: bson.D{{Key: "cc.name", Value: 1}}},
{Keys: bson.D{{Key: "cc.address", Value: 1}}},
{Keys: bson.D{{Key: "bcc.name", Value: 1}}},
{Keys: bson.D{{Key: "bcc.address", Value: 1}}},
{Keys: bson.D{{Key: "attachments.filename", Value: 1}}},
{Keys: bson.D{{Key: "attachments.contentType", Value: 1}}},
{Keys: bson.D{{Key: "attachments.content", Value: 1}}},
{Keys: bson.D{{Key: "attachments.size", Value: 1}}},
{Keys: bson.D{{Key: "text", Value: "text"}}},
{Keys: bson.D{{Key: "html", Value: "text"}}},
})
return err
}
Expand Down Expand Up @@ -352,16 +351,23 @@ func newFilter(q query.Query) bson.D {
filter = withFilter(filter, []string{"subject"}, regexInValues(q.Subjects))
}

if len(q.Texts) > 0 {
filter = withFilter(filter, []string{"text"}, regexInValues(q.Texts))
}
// if len(q.Texts) > 0 {
// filter = withFilter(filter, []string{"text"}, regexInValues(q.Texts))
// }

if len(q.HTML) > 0 {
filter = withFilter(filter, []string{"html"}, regexInValues(q.HTML))
}
// if len(q.HTML) > 0 {
// filter = withFilter(filter, []string{"html"}, regexInValues(q.HTML))
// }

if len(q.RFC) > 0 {
filter = withFilter(filter, []string{"rfc"}, regexInValues(q.RFC))
// if len(q.RFC) > 0 {
// filter = withFilter(filter, []string{"rfc"}, regexInValues(q.RFC))
// }

if q.Input != "" {
filter = append(filter, bson.E{
Key: "$text",
Value: bson.D{{Key: "$search", Value: fmt.Sprintf("%q", q.Input)}},
})
}

if len(q.From) > 0 {
Expand Down Expand Up @@ -441,9 +447,9 @@ func newFilter(q query.Query) bson.D {
filter = append(filter, bson.E{Key: "$or", Value: or})
}

if len(q.SendErrors) > 0 {
filter = append(filter, bson.E{Key: "sendError", Value: regexInValues(q.SendErrors)})
}
// if len(q.SendErrors) > 0 {
// filter = append(filter, bson.E{Key: "sendError", Value: regexInValues(q.SendErrors)})
// }

return filter
}
Expand Down
82 changes: 45 additions & 37 deletions plugin/archive/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@ const (

// Query provides filters, sorting & pagination for querying mails.
type Query struct {
From []mail.Address
To []mail.Address
CC []mail.Address
BCC []mail.Address
Recipients []mail.Address
Subjects []string
Texts []string
HTML []string
RFC []string
SendErrors []string
From []mail.Address
To []mail.Address
CC []mail.Address
BCC []mail.Address
Recipients []mail.Address
Subjects []string
// Texts []string
// HTML []string
// RFC []string
// SendErrors []string
SendTime SendTimeFilter
Attachment AttachmentFilter
Sorting Sorting
SortDirection SortDirection
Pagination Pagination
Input string
}

// SendTimeFilter is the query filter for the send date.
Expand Down Expand Up @@ -128,33 +129,33 @@ func Subject(subjects ...string) Option {
}
}

// Text returns an Option that adds a `Text` filter to a Query.
func Text(texts ...string) Option {
return func(q *Query) {
q.Texts = append(q.Texts, texts...)
}
}

// HTML returns an Option that adds an `HTML` filter to a Query.
func HTML(html ...string) Option {
return func(q *Query) {
q.HTML = append(q.HTML, html...)
}
}

// RFC returns an Option that adds an `RFC` filter to a Query.
func RFC(rfc ...string) Option {
return func(q *Query) {
q.RFC = append(q.RFC, rfc...)
}
}

// SendError returns an Option that adds a `send error` filter to a Query.
func SendError(errs ...string) Option {
return func(q *Query) {
q.SendErrors = append(q.SendErrors, errs...)
}
}
// // Text returns an Option that adds a `Text` filter to a Query.
// func Text(texts ...string) Option {
// return func(q *Query) {
// q.Texts = append(q.Texts, texts...)
// }
// }

// // HTML returns an Option that adds an `HTML` filter to a Query.
// func HTML(html ...string) Option {
// return func(q *Query) {
// q.HTML = append(q.HTML, html...)
// }
// }

// // RFC returns an Option that adds an `RFC` filter to a Query.
// func RFC(rfc ...string) Option {
// return func(q *Query) {
// q.RFC = append(q.RFC, rfc...)
// }
// }

// // SendError returns an Option that adds a `send error` filter to a Query.
// func SendError(errs ...string) Option {
// return func(q *Query) {
// q.SendErrors = append(q.SendErrors, errs...)
// }
// }

// SentAt returns an Option that filters mails by their send time.
// The send time of a mail must be one of t.
Expand Down Expand Up @@ -230,6 +231,13 @@ func AttachmentContent(content ...[]byte) Option {
}
}

// Input returns an Option that sets the search input for a Query.
func Input(input string) Option {
return func(q *Query) {
q.Input = input
}
}

// Sort returns an Option that configures the Sorting of a Query.
func Sort(by Sorting, dir SortDirection) Option {
return func(q *Query) {
Expand Down
80 changes: 40 additions & 40 deletions plugin/archive/query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,36 +90,36 @@ func TestNew(t *testing.T) {
Subjects: []string{"Subject 1", "Subject 2", "Subject 3", "Subject 4"},
},
},
{
name: "Text()",
opts: []query.Option{
query.Text("text body 1", "text body 2"),
query.Text("text body 3", "text body 4"),
},
want: query.Query{
Texts: []string{"text body 1", "text body 2", "text body 3", "text body 4"},
},
},
{
name: "HTML()",
opts: []query.Option{
query.HTML("html body 1", "html body 2"),
query.HTML("html body 3", "html body 4"),
},
want: query.Query{
HTML: []string{"html body 1", "html body 2", "html body 3", "html body 4"},
},
},
{
name: "RFC()",
opts: []query.Option{
query.RFC("rfc body 1", "rfc body 2"),
query.RFC("rfc body 3", "rfc body 4"),
},
want: query.Query{
RFC: []string{"rfc body 1", "rfc body 2", "rfc body 3", "rfc body 4"},
},
},
// {
// name: "Text()",
// opts: []query.Option{
// query.Text("text body 1", "text body 2"),
// query.Text("text body 3", "text body 4"),
// },
// want: query.Query{
// Texts: []string{"text body 1", "text body 2", "text body 3", "text body 4"},
// },
// },
// {
// name: "HTML()",
// opts: []query.Option{
// query.HTML("html body 1", "html body 2"),
// query.HTML("html body 3", "html body 4"),
// },
// want: query.Query{
// HTML: []string{"html body 1", "html body 2", "html body 3", "html body 4"},
// },
// },
// {
// name: "RFC()",
// opts: []query.Option{
// query.RFC("rfc body 1", "rfc body 2"),
// query.RFC("rfc body 3", "rfc body 4"),
// },
// want: query.Query{
// RFC: []string{"rfc body 1", "rfc body 2", "rfc body 3", "rfc body 4"},
// },
// },
{
name: "AttachmentFilename()",
opts: []query.Option{
Expand Down Expand Up @@ -187,16 +187,16 @@ func TestNew(t *testing.T) {
},
},
},
{
name: "SendError()",
opts: []query.Option{
query.SendError("send error 1", "send error 2"),
query.SendError("send error 3", "send error 4"),
},
want: query.Query{
SendErrors: []string{"send error 1", "send error 2", "send error 3", "send error 4"},
},
},
// {
// name: "SendError()",
// opts: []query.Option{
// query.SendError("send error 1", "send error 2"),
// query.SendError("send error 3", "send error 4"),
// },
// want: query.Query{
// SendErrors: []string{"send error 1", "send error 2", "send error 3", "send error 4"},
// },
// },
{
name: "SentAt()",
opts: []query.Option{
Expand Down
Loading

0 comments on commit 25afa14

Please sign in to comment.