1
0
mirror of synced 2025-12-21 19:06:49 -05:00
Files
docs/lib/search/algolia-search.js
Kevin Heis 2e07070fb9 Add filters params to search endpoint (#18289)
* Add filters params to search endpoint

* Update search.md

* Update middleware/search.js

Co-authored-by: Rachael Sewell <rachmari@github.com>

Co-authored-by: Rachael Sewell <rachmari@github.com>
2021-03-17 21:02:47 +00:00

32 lines
1.2 KiB
JavaScript

const algoliasearch = require('algoliasearch')
const { get } = require('lodash')
const { namePrefix } = require('./config')
// https://www.algolia.com/apps/ZI5KPY1HBE/dashboard
// This API key is public. There's also a private API key for writing to the Algolia API
const searchClient = algoliasearch('ZI5KPY1HBE', '685df617246c3a10abba589b4599288f')
module.exports = async function loadAlgoliaResults ({ version, language, query, filters, limit }) {
const indexName = `${namePrefix}-${version}-${language}`
const index = searchClient.initIndex(indexName)
// allows "phrase queries" and "prohibit operator"
// https://www.algolia.com/doc/api-reference/api-parameters/advancedSyntax/
const { hits } = await index.search(query, {
hitsPerPage: limit,
advancedSyntax: true,
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
filters
})
return hits.map(hit => ({
url: hit.objectID,
breadcrumbs: get(hit, '_highlightResult.breadcrumbs.value'),
heading: get(hit, '_highlightResult.heading.value'),
title: get(hit, '_highlightResult.title.value'),
content: get(hit, '_highlightResult.content.value'),
topics: hit.topics
}))
}