feat(client, server) #3: paginate search results before snippet enrichment

pull/7968/head
Tayeb Chlyah 3 months ago
parent 5807b1784b
commit df0720a61c

@ -99,8 +99,7 @@ export default {
), term => -term.length)
},
results() {
const currentIndex = (this.pagination - 1) * this.perPage
return this.response.results ? _.slice(this.response.results, currentIndex, currentIndex + this.perPage) : []
return this.response.results ? this.response.results : []
},
hits() {
return this.response.totalHits ? this.response.totalHits : 0
@ -115,6 +114,7 @@ export default {
watch: {
search(newValue, oldValue) {
this.cursor = 0
this.pagination = 1
if (!newValue || (newValue && newValue.length < 2)) {
this.searchIsLoading = false
} else {
@ -225,7 +225,9 @@ export default {
query: searchPagesQuery,
variables() {
return {
query: this.search
query: this.search,
page: this.pagination,
limit: this.perPage
}
},
fetchPolicy: 'network-only',
@ -234,9 +236,6 @@ export default {
skip() {
return !this.search || this.search.length < 2
},
result() {
this.pagination = 1
},
update: (data) => _.get(data, 'pages.search', {}),
watchLoading (isLoading) {
this.searchIsLoading = isLoading

@ -1,6 +1,6 @@
query ($query: String!) {
query ($query: String!, $page: Int, $limit: Int) {
pages {
search(query:$query) {
search(query:$query, page:$page, limit:$limit) {
results {
id
title

@ -92,13 +92,38 @@ module.exports = {
tags: r.tags // Tags are needed since access permissions can be limited by page tags too
})
})
const limit = _.clamp(_.toSafeInteger(args.limit) || 10, 1, 50)
const page = Math.max(_.toSafeInteger(args.page) || 1, 1)
const currentIndex = (page - 1) * limit
const pagedResults = filteredResults.slice(currentIndex, currentIndex + limit)
let pageContentByKey = {}
if (pagedResults.length > 0) {
const pages = await WIKI.models.pages.query()
.select('path', { locale: 'localeCode' }, 'render', 'description', 'title')
.where(builder => {
pagedResults.forEach((result, idx) => {
builder[idx === 0 ? 'where' : 'orWhere'](qb => {
qb.where('path', result.path).andWhere('localeCode', result.locale)
})
})
})
pageContentByKey = _.fromPairs(pages.map(pageResult => ([
`${pageResult.locale}:${pageResult.path}`,
WIKI.models.pages.buildSearchContent(pageResult.render) || pageResult.description || pageResult.title || ''
])))
}
return {
...resp,
results: filteredResults.map(r => {
results: pagedResults.map(r => {
return {
...r,
snippet: buildSearchSnippet(r.snippet || r.content || r.description || r.title || r.path || '', args.query)
snippet: buildSearchSnippet(
pageContentByKey[`${r.locale}:${r.path}`] || r.snippet || r.content || r.description || r.title || r.path || '',
args.query
)
}
}),
totalHits: filteredResults.length

@ -30,6 +30,8 @@ type PageQuery {
query: String!
path: String
locale: String
page: Int
limit: Int
): PageSearchResponse! @auth(requires: ["manage:system", "read:pages"])
list(

Loading…
Cancel
Save