diff --git a/backend/src/db/seed.js b/backend/src/db/seed.js index b1212b1e8..3334285d3 100644 --- a/backend/src/db/seed.js +++ b/backend/src/db/seed.js @@ -930,7 +930,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl'] await Promise.all([...Array(30).keys()].map(() => Factory.build('user'))) await Promise.all( - [...Array(30).keys()].map(index => Factory.build('user', { name: `Jenny${index}` })), + [...Array(30).keys()].map((index) => Factory.build('user', { name: `Jenny${index}` })), ) await Promise.all( [...Array(30).keys()].map(() => diff --git a/backend/src/schema/resolvers/searches.js b/backend/src/schema/resolvers/searches.js index 1c72220ef..873ca7533 100644 --- a/backend/src/schema/resolvers/searches.js +++ b/backend/src/schema/resolvers/searches.js @@ -11,24 +11,25 @@ export default { const postCypher = ` CALL db.index.fulltext.queryNodes('post_fulltext_search', $query) - YIELD node as posts, score - MATCH (posts)<-[:WROTE]-(author:User) + YIELD node AS post, score + MATCH (post)<-[:WROTE]-(author:User) WHERE score >= 0.0 AND NOT ( author.deleted = true OR author.disabled = true - OR posts.deleted = true OR posts.disabled = true + OR post.deleted = true OR post.disabled = true OR (:User {id: $userId})-[:MUTED]->(author) ) - WITH posts, author, - [(posts)<-[:COMMENTS]-(comment:Comment) | comment] as comments, - [(posts)<-[:SHOUTED]-(user:User) | user] as shouter - RETURN posts { + WITH post, author, + [(post)<-[:COMMENTS]-(comment:Comment) | comment] AS comments, + [(post)<-[:SHOUTED]-(user:User) | user] AS shouter + RETURN + { postCount: toString(size(collect(post))), posts: collect(post { .*, - __typename: labels(posts)[0], + __typename: labels(post)[0], author: properties(author), commentsCount: toString(size(comments)), shoutedCount: toString(size(shouter)) - } + })} AS postResult SKIP $postsOffset LIMIT $firstPosts ` @@ -36,7 +37,7 @@ export default { const myQuery = queryString(query) const session = context.driver.session() - const searchResultPromise = session.readTransaction(async transaction => { + const searchResultPromise = session.readTransaction(async (transaction) => { const postTransactionResponse = await transaction.run(postCypher, { query: myQuery, postsOffset, @@ -48,7 +49,7 @@ export default { try { const postResults = await searchResultPromise log(postResults) - return postResults.records.map(record => record.get('posts')) + return postResults.records[0].get('postResult') } finally { session.close() } @@ -59,18 +60,20 @@ export default { const userCypher = ` CALL db.index.fulltext.queryNodes('user_fulltext_search', $query) - YIELD node as users, score - MATCH (users) + YIELD node as user, score + MATCH (user) WHERE score >= 0.0 - AND NOT (users.deleted = true OR users.disabled = true) - RETURN users {.*, __typename: labels(users)[0]} - SKIP $usersOffset + AND NOT (user.deleted = true OR user.disabled = true) + RETURN + { userCount: toString(size(collect(user))), users: collect(user {.*, __typename: labels(user)[0]}) } + AS userResult + SKIP $usersOffset LIMIT $firstUsers ` const myQuery = queryString(query) const session = context.driver.session() - const searchResultPromise = session.readTransaction(async transaction => { + const searchResultPromise = session.readTransaction(async (transaction) => { const userTransactionResponse = await transaction.run(userCypher, { query: myQuery, usersOffset, @@ -83,7 +86,7 @@ export default { try { const userResults = await searchResultPromise log(userResults) - return userResults.records.map(record => record.get('users')) + return userResults.records[0].get('userResult') } finally { session.close() } diff --git a/backend/src/schema/types/type/Search.gql b/backend/src/schema/types/type/Search.gql index 3e04655cd..e19ec7611 100644 --- a/backend/src/schema/types/type/Search.gql +++ b/backend/src/schema/types/type/Search.gql @@ -1,7 +1,17 @@ union SearchResult = Post | User | Tag +type postSearchResults { + postCount: Int + posts: [Post]! +} + +type userSearchResults { + userCount: Int + users: [User]! +} + type Query { - searchPosts(query: String!, firstPosts: Int, postsOffset: Int): [Post]! - searchUsers(query: String!, firstUsers: Int, usersOffset: Int): [User]! + searchPosts(query: String!, firstPosts: Int, postsOffset: Int): postSearchResults! + searchUsers(query: String!, firstUsers: Int, usersOffset: Int): userSearchResults! searchResults(query: String!, limit: Int = 5): [SearchResult]! }