mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2026-04-27 00:08:45 +00:00
refactoring of search by types
This commit is contained in:
parent
269c2d78dd
commit
ee1569d5d9
@ -3,51 +3,86 @@ import { queryString } from './searches/queryString'
|
|||||||
|
|
||||||
// see http://lucene.apache.org/core/8_3_1/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#package.description
|
// see http://lucene.apache.org/core/8_3_1/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#package.description
|
||||||
|
|
||||||
|
const createCypher = (setup) => `
|
||||||
|
CALL db.index.fulltext.queryNodes('${setup.fulltextIndex}', $query)
|
||||||
|
YIELD node as resource, score
|
||||||
|
${setup.match}
|
||||||
|
WHERE score >= 0.0
|
||||||
|
${setup.notClause}
|
||||||
|
${setup.withClause}
|
||||||
|
RETURN
|
||||||
|
{
|
||||||
|
${setup.countKeyName}: toString(size(collect(resource))),
|
||||||
|
${setup.resultKeyName}: collect(resource { .*, __typename: labels(resource)[0]${setup.additionalMapping} })
|
||||||
|
}
|
||||||
|
AS ${setup.resultName}
|
||||||
|
SKIP $skip
|
||||||
|
LIMIT $limit
|
||||||
|
`
|
||||||
|
|
||||||
|
const simpleNotClause = 'AND NOT (resource.deleted = true OR resource.disabled = true)'
|
||||||
|
|
||||||
|
const postNotClause = `AND NOT (
|
||||||
|
author.deleted = true OR author.disabled = true
|
||||||
|
OR resource.deleted = true OR resource.disabled = true
|
||||||
|
OR (:User {id: $userId})-[:MUTED]->(author)
|
||||||
|
)`
|
||||||
|
|
||||||
|
const searchPostsSetup = {
|
||||||
|
fulltextIndex: 'post_fulltext_search',
|
||||||
|
match: 'MATCH (resource)<-[:WROTE]-(author:User)',
|
||||||
|
notClause: postNotClause,
|
||||||
|
withClause: `WITH resource, author,
|
||||||
|
[(resource)<-[:COMMENTS]-(comment:Comment) | comment] AS comments,
|
||||||
|
[(resource)<-[:SHOUTED]-(user:User) | user] AS shouter`,
|
||||||
|
additionalMapping: `, author: properties(author), commentsCount: toString(size(comments)), shoutedCount: toString(size(shouter))`,
|
||||||
|
countKeyName: 'postCount',
|
||||||
|
resultKeyName: 'posts',
|
||||||
|
resultName: 'postResult',
|
||||||
|
}
|
||||||
|
|
||||||
|
const searchUsersSetup = {
|
||||||
|
fulltextIndex: 'user_fulltext_search',
|
||||||
|
match: 'MATCH (resource)',
|
||||||
|
notClause: simpleNotClause,
|
||||||
|
withClause: '',
|
||||||
|
additionalMapping: '',
|
||||||
|
countKeyName: 'userCount',
|
||||||
|
resultKeyName: 'users',
|
||||||
|
resultName: 'userResult',
|
||||||
|
}
|
||||||
|
|
||||||
|
const searchHashtagsSetup = {
|
||||||
|
fulltextIndex: 'tag_fulltext_search',
|
||||||
|
match: 'MATCH (resource)',
|
||||||
|
notClause: simpleNotClause,
|
||||||
|
withClause: '',
|
||||||
|
additionalMapping: '',
|
||||||
|
countKeyName: 'hashtagCount',
|
||||||
|
resultKeyName: 'hashtags',
|
||||||
|
resultName: 'hashtagResult',
|
||||||
|
}
|
||||||
|
|
||||||
|
const searchResultPromise = async (session, setup, params) => {
|
||||||
|
return session.readTransaction(async (transaction) => {
|
||||||
|
return transaction.run(createCypher(setup), params)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
Query: {
|
Query: {
|
||||||
searchPosts: async (_parent, args, context, _resolveInfo) => {
|
searchPosts: async (_parent, args, context, _resolveInfo) => {
|
||||||
const { query, postsOffset, firstPosts } = args
|
const { query, postsOffset, firstPosts } = args
|
||||||
const { id: userId } = context.user
|
const { id: userId } = context.user
|
||||||
|
|
||||||
const postCypher = `
|
|
||||||
CALL db.index.fulltext.queryNodes('post_fulltext_search', $query)
|
|
||||||
YIELD node AS post, score
|
|
||||||
MATCH (post)<-[:WROTE]-(author:User)
|
|
||||||
WHERE score >= 0.0
|
|
||||||
AND NOT (
|
|
||||||
author.deleted = true OR author.disabled = true
|
|
||||||
OR post.deleted = true OR post.disabled = true
|
|
||||||
OR (:User {id: $userId})-[:MUTED]->(author)
|
|
||||||
)
|
|
||||||
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(post)[0],
|
|
||||||
author: properties(author),
|
|
||||||
commentsCount: toString(size(comments)),
|
|
||||||
shoutedCount: toString(size(shouter))
|
|
||||||
})} AS postResult
|
|
||||||
SKIP $postsOffset
|
|
||||||
LIMIT $firstPosts
|
|
||||||
`
|
|
||||||
|
|
||||||
const myQuery = queryString(query)
|
|
||||||
|
|
||||||
const session = context.driver.session()
|
const session = context.driver.session()
|
||||||
const searchResultPromise = session.readTransaction(async (transaction) => {
|
try {
|
||||||
const postTransactionResponse = await transaction.run(postCypher, {
|
const postResults = await searchResultPromise(session, searchPostsSetup, {
|
||||||
query: myQuery,
|
query: queryString(query),
|
||||||
postsOffset,
|
skip: postsOffset,
|
||||||
firstPosts,
|
limit: firstPosts,
|
||||||
userId,
|
userId,
|
||||||
})
|
})
|
||||||
return postTransactionResponse
|
|
||||||
})
|
|
||||||
try {
|
|
||||||
const postResults = await searchResultPromise
|
|
||||||
log(postResults)
|
log(postResults)
|
||||||
return postResults.records[0].get('postResult')
|
return postResults.records[0].get('postResult')
|
||||||
} finally {
|
} finally {
|
||||||
@ -58,33 +93,14 @@ export default {
|
|||||||
const { query, usersOffset, firstUsers } = args
|
const { query, usersOffset, firstUsers } = args
|
||||||
const { id: userId } = context.user
|
const { id: userId } = context.user
|
||||||
|
|
||||||
const userCypher = `
|
|
||||||
CALL db.index.fulltext.queryNodes('user_fulltext_search', $query)
|
|
||||||
YIELD node as user, score
|
|
||||||
MATCH (user)
|
|
||||||
WHERE score >= 0.0
|
|
||||||
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 session = context.driver.session()
|
||||||
const searchResultPromise = session.readTransaction(async (transaction) => {
|
try {
|
||||||
const userTransactionResponse = await transaction.run(userCypher, {
|
const userResults = await searchResultPromise(session, searchUsersSetup, {
|
||||||
query: myQuery,
|
query: queryString(query),
|
||||||
usersOffset,
|
skip: usersOffset,
|
||||||
firstUsers,
|
limit: firstUsers,
|
||||||
userId,
|
userId,
|
||||||
})
|
})
|
||||||
return userTransactionResponse
|
|
||||||
})
|
|
||||||
|
|
||||||
try {
|
|
||||||
const userResults = await searchResultPromise
|
|
||||||
log(userResults)
|
log(userResults)
|
||||||
return userResults.records[0].get('userResult')
|
return userResults.records[0].get('userResult')
|
||||||
} finally {
|
} finally {
|
||||||
@ -95,32 +111,14 @@ export default {
|
|||||||
const { query, hashtagsOffset, firstHashtags } = args
|
const { query, hashtagsOffset, firstHashtags } = args
|
||||||
const { id: userId } = context.user
|
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 session = context.driver.session()
|
||||||
const searchResultPromise = session.readTransaction(async (transaction) => {
|
try {
|
||||||
const userTransactionResponse = await transaction.run(tagCypher, {
|
const hashtagResults = await searchResultPromise(session, searchHashtagsSetup, {
|
||||||
query: myQuery,
|
query: queryString(query),
|
||||||
hashtagsOffset,
|
skip: hashtagsOffset,
|
||||||
firstHashtags,
|
limit: firstHashtags,
|
||||||
userId,
|
userId,
|
||||||
})
|
})
|
||||||
return userTransactionResponse
|
|
||||||
})
|
|
||||||
|
|
||||||
try {
|
|
||||||
const hashtagResults = await searchResultPromise
|
|
||||||
log(hashtagResults)
|
log(hashtagResults)
|
||||||
return hashtagResults.records[0].get('hashtagResult')
|
return hashtagResults.records[0].get('hashtagResult')
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user