From d7308a4994d2859c1c864cc1b6888da8eee834b0 Mon Sep 17 00:00:00 2001 From: akshadpai <70975196+akshadpai@users.noreply.github.com> Date: Sun, 16 Jul 2023 12:58:44 -0500 Subject: [PATCH] Searching follows looks for substring matches (#671) * Searching follows looks for substring matches * handle super-strings --------- Co-authored-by: Stephen Hurwit --- .secrets.baseline | 6 +----- CHANGELOG.md | 4 ++++ driven/storage/adapter.go | 38 ++++++++++++++++++++++++++++++-------- driven/storage/database.go | 8 ++++---- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/.secrets.baseline b/.secrets.baseline index 6860e999d..e614f322c 100644 --- a/.secrets.baseline +++ b/.secrets.baseline @@ -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 @@ -370,5 +366,5 @@ } ] }, - "generated_at": "2023-07-08T02:26:34Z" + "generated_at": "2023-07-11T15:43:42Z" } diff --git a/CHANGELOG.md b/CHANGELOG.md index ef99eee88..0f49b5e3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/driven/storage/adapter.go b/driven/storage/adapter.go index 1a7b7bf0c..a093dc791 100644 --- a/driven/storage/adapter.go +++ b/driven/storage/adapter.go @@ -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}}) diff --git a/driven/storage/database.go b/driven/storage/database.go index 4673d73e3..e2054a4f3 100644 --- a/driven/storage/database.go +++ b/driven/storage/database.go @@ -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