diff --git a/client/components/common/search-results.vue b/client/components/common/search-results.vue index 7f1b891e..3c72945c 100644 --- a/client/components/common/search-results.vue +++ b/client/components/common/search-results.vue @@ -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 diff --git a/client/graph/common/common-pages-query-search.gql b/client/graph/common/common-pages-query-search.gql index 27232ff9..27b5e372 100644 --- a/client/graph/common/common-pages-query-search.gql +++ b/client/graph/common/common-pages-query-search.gql @@ -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 diff --git a/server/graph/resolvers/page.js b/server/graph/resolvers/page.js index c1041708..54e56ad0 100644 --- a/server/graph/resolvers/page.js +++ b/server/graph/resolvers/page.js @@ -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 diff --git a/server/graph/schemas/page.graphql b/server/graph/schemas/page.graphql index 2bb2adf6..65147b9d 100644 --- a/server/graph/schemas/page.graphql +++ b/server/graph/schemas/page.graphql @@ -30,6 +30,8 @@ type PageQuery { query: String! path: String locale: String + page: Int + limit: Int ): PageSearchResponse! @auth(requires: ["manage:system", "read:pages"]) list(