Refactor further search

- in the end, the error was due to not asking for the id of the author
back of the Post.. grrr.... why couldn't we get better error messages!!

- Co-authored-by: Moriz Wahl <moriz.wahl@gmx.de>
This commit is contained in:
mattwr18 2019-12-13 15:56:18 +01:00
parent 2f4125b5d1
commit 4cd43b68a4
5 changed files with 38 additions and 58 deletions

View File

@ -1,64 +1,56 @@
import uuid from 'uuid/v4'
const transformReturnType = record => {
return {
id: uuid(),
searchResults: {
__typename: record.get('type'),
...record.get('resource').properties,
},
__typename: record.get('type'),
...record.get('resource').properties,
}
}
export default {
Query: {
findResources: async (_parent, args, context, _resolveInfo) => {
const query = args.query
const { query, limit } = args
const filter = {}
const limit = args.limit
const { user } = context
const thisUserId = user.id
const postQuery = query.replace(/\s/g, '~ ') + '~'
const userQuery = query.replace(/\s/g, '~ ') + '~'
const { id: thisUserId } = context.user
// const postQuery = query.replace(/\s/g, '~ ') + '~'
// const userQuery = query.replace(/\s/g, '~ ') + '~'
const postCypher = `
CALL db.index.fulltext.queryNodes('post_fulltext_search', $query)
YIELD node as resource, score
MATCH (resource)<-[:WROTE]-(user:User)
WHERE score >= 0.2
WHERE score >= 0.5
AND NOT user.deleted = true AND NOT user.disabled = true
AND NOT resource.deleted = true AND NOT resource.disabled = true
AND NOT user.id in COALESCE($filter.author_not.id_in, [])
AND NOT (:User { id: $thisUserId })-[:BLOCKED]->(user)
AND NOT (:User { id: $thisUserId })-[:BLOCKED]-(user)
RETURN resource, labels(resource)[0] AS type
LIMIT $limit
`
const session = context.driver.session()
const postResults = await session.run(postCypher, {
query: postQuery,
filter: filter,
limit: limit,
thisUserId: thisUserId,
query,
filter,
limit,
thisUserId,
})
session.close()
const userCypher = `
CALL db.index.fulltext.queryNodes('user_fulltext_search', $query)
YIELD node as resource, score
MATCH (resource)
WHERE score >= 0.2
WHERE score >= 0.5
AND NOT resource.deleted = true AND NOT resource.disabled = true
AND NOT (:User { id: $thisUserId })-[:BLOCKED]->(resource)
AND NOT (:User { id: $thisUserId })-[:BLOCKED]-(resource)
RETURN resource, labels(resource)[0] AS type
LIMIT $limit
`
const userResults = await session.run(userCypher, {
query: userQuery,
filter: filter,
limit: limit,
thisUserId: thisUserId,
query,
filter,
limit,
thisUserId,
})
session.close()
const result = postResults.records.concat(userResults.records).map(transformReturnType)
let result = [...postResults.records, ...userResults.records]
result = result.map(transformReturnType)
return result
},
},

View File

@ -1,9 +1,4 @@
type SearchResult {
id: ID!
searchResults: ResourceResults
}
union ResourceResults = Post | User
union SearchResult = Post | User
type Query {
findResources(query: String!, limit: Int = 5): [SearchResult]!

View File

@ -150,8 +150,7 @@ export default {
query: value,
},
})
const searchResults = findResources.map(searchResult => searchResult.searchResults)
this.searchResults = searchResults
this.searchResults = findResources
} catch (error) {
this.searchResults = []
} finally {

View File

@ -3,27 +3,25 @@ import gql from 'graphql-tag'
export const findResourcesQuery = gql`
query($query: String!) {
findResources(query: $query, limit: 5) {
id
searchResults {
__typename
... on Post {
id
title
slug
commentsCount
shoutedCount
createdAt
author {
name
}
}
... on User {
__typename
... on Post {
id
title
slug
commentsCount
shoutedCount
createdAt
author {
id
name
slug
avatar
}
}
... on User {
id
name
slug
avatar
}
}
}
`

View File

@ -82,7 +82,7 @@
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
import { mapGetters } from 'vuex'
import LocaleSwitch from '~/components/LocaleSwitch/LocaleSwitch'
import SearchInput from '~/components/SearchInput/SearchInput.vue'
import Modal from '~/components/Modal'
@ -126,10 +126,6 @@ export default {
},
},
methods: {
...mapActions({
quickSearchClear: 'search/quickClear',
quickSearch: 'search/quickSearch',
}),
goToResource(item) {
this.$nextTick(() => {
switch (item.__typename) {