Skip to content

Commit

Permalink
Searching follows looks for substring matches (rokwire#671)
Browse files Browse the repository at this point in the history
* Searching follows looks for substring matches

* handle super-strings

---------

Co-authored-by: Stephen Hurwit <[email protected]>
  • Loading branch information
akshadpai and shurwit authored Jul 16, 2023
1 parent d37e81e commit d7308a4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
6 changes: 1 addition & 5 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@
{
"path": "detect_secrets.filters.allowlist.is_line_allowlisted"
},
{
"path": "detect_secrets.filters.common.is_baseline_file",
"filename": ".secrets.baseline"
},
{
"path": "detect_secrets.filters.common.is_ignored_due_to_verification_policies",
"min_level": 2
Expand Down Expand Up @@ -370,5 +366,5 @@
}
]
},
"generated_at": "2023-07-08T02:26:34Z"
"generated_at": "2023-07-11T15:43:42Z"
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added
- Searching follows looks for substring matches [#670](https://github.com/rokwire/core-building-block/issues/670)

### Added
- Support following accounts [#667](https://github.com/rokwire/core-building-block/issues/667)
- Device ID not nullable [#672](https://github.com/rokwire/core-building-block/issues/672)
Expand Down
38 changes: 30 additions & 8 deletions driven/storage/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1337,16 +1337,38 @@ func (sa *Adapter) FindPublicAccounts(context TransactionContext, appID string,
pipeline := []bson.M{}

var searchStr, firstNameStr, lastNameStr, usernameStr, followingIDStr, followerIDStr string

// search for matching using text search. No substring matches
// if search != nil {
// searchStr = *search
// pipeline = append(pipeline,
// bson.M{
// "$match": bson.M{
// "$text": bson.M{
// "$search": search,
// // "$caseSensitive": false,
// }},
// })
// }

if search != nil {
searchStr = *search
pipeline = append(pipeline,
bson.M{
"$match": bson.M{
"$text": bson.M{
"$search": search,
// "$caseSensitive": false,
}},
})
searchStrParts := strings.Split(searchStr, " ")
searchStr = ""
for _, part := range searchStrParts {
if searchStr != "" {
searchStr += "|"
}
searchStr += "(" + part + ")"
}
regexFilter := bson.M{
"$or": []bson.M{
{"username": primitive.Regex{Pattern: searchStr, Options: "i"}},
{"profile.first_name": primitive.Regex{Pattern: searchStr, Options: "i"}},
{"profile.last_name": primitive.Regex{Pattern: searchStr, Options: "i"}},
},
}
pipeline = append(pipeline, bson.M{"$match": regexFilter})
}

pipeline = append(pipeline, bson.M{"$match": bson.M{"app_org_id": appOrg.ID, "privacy.public": true}})
Expand Down
8 changes: 4 additions & 4 deletions driven/storage/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,10 @@ func (m *database) applyAccountsChecks(accounts *collectionWrapper) error {
return err
}

err = accounts.AddIndex(bson.D{primitive.E{Key: "username", Value: "text"}, primitive.E{Key: "profile.first_name", Value: "text"}, primitive.E{Key: "profile.last_name", Value: "text"}}, false)
if err != nil {
return err
}
// err = accounts.AddIndex(bson.D{primitive.E{Key: "username", Value: "text"}, primitive.E{Key: "profile.first_name", Value: "text"}, primitive.E{Key: "profile.last_name", Value: "text"}}, false)
// if err != nil {
// return err
// }

m.logger.Info("accounts check passed")
return nil
Expand Down

0 comments on commit d7308a4

Please sign in to comment.