From 269c2d78dd1cfde69146a4ac3da1fea1ebf8adc6 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Wed, 8 Apr 2020 17:59:49 +0200 Subject: [PATCH] searchHashtags added --- .../src/middleware/permissionsMiddleware.js | 1 + backend/src/schema/resolvers/searches.js | 36 +++++++++++++++++++ backend/src/schema/types/type/Search.gql | 6 ++++ 3 files changed, 43 insertions(+) diff --git a/backend/src/middleware/permissionsMiddleware.js b/backend/src/middleware/permissionsMiddleware.js index 28ad8099c..ec7195709 100644 --- a/backend/src/middleware/permissionsMiddleware.js +++ b/backend/src/middleware/permissionsMiddleware.js @@ -89,6 +89,7 @@ export default shield( searchResults: allow, searchPosts: allow, searchUsers: allow, + searchHashtags: allow, embed: allow, Category: allow, Tag: allow, diff --git a/backend/src/schema/resolvers/searches.js b/backend/src/schema/resolvers/searches.js index 873ca7533..fbf296b72 100644 --- a/backend/src/schema/resolvers/searches.js +++ b/backend/src/schema/resolvers/searches.js @@ -91,6 +91,42 @@ export default { session.close() } }, + searchHashtags: async (_parent, args, context, _resolveInfo) => { + const { query, hashtagsOffset, firstHashtags } = args + const { id: userId } = context.user + + const tagCypher = ` + CALL db.index.fulltext.queryNodes('tag_fulltext_search', $query) + YIELD node as tag, score + MATCH (tag) + WHERE score >= 0.0 + AND NOT (tag.deleted = true OR tag.disabled = true) + RETURN { hashtagCount: toString(size(collect(tag))), hashtags: collect( tag {.*, __typename: labels(tag)[0]}) } AS hashtagResult + SKIP $hashtagsOffset + LIMIT $firstHashtags + ` + + const myQuery = queryString(query) + + const session = context.driver.session() + const searchResultPromise = session.readTransaction(async (transaction) => { + const userTransactionResponse = await transaction.run(tagCypher, { + query: myQuery, + hashtagsOffset, + firstHashtags, + userId, + }) + return userTransactionResponse + }) + + try { + const hashtagResults = await searchResultPromise + log(hashtagResults) + return hashtagResults.records[0].get('hashtagResult') + } finally { + session.close() + } + }, searchResults: async (_parent, args, context, _resolveInfo) => { const { query, limit } = args const { id: thisUserId } = context.user diff --git a/backend/src/schema/types/type/Search.gql b/backend/src/schema/types/type/Search.gql index e19ec7611..9537b5a84 100644 --- a/backend/src/schema/types/type/Search.gql +++ b/backend/src/schema/types/type/Search.gql @@ -10,8 +10,14 @@ type userSearchResults { users: [User]! } +type hashtagSearchResults { + hashtagCount: Int + hashtags: [Tag]! +} + type Query { searchPosts(query: String!, firstPosts: Int, postsOffset: Int): postSearchResults! searchUsers(query: String!, firstUsers: Int, usersOffset: Int): userSearchResults! + searchHashtags(query: String!, firstHashtags: Int, hashtagsOffset: Int): hashtagSearchResults! searchResults(query: String!, limit: Int = 5): [SearchResult]! }