Skip to content

Commit b836041

Browse files
authored
Merge pull request github#20435 from github/repo-sync
repo sync
2 parents c17b467 + 6331664 commit b836041

File tree

6 files changed

+130
-503
lines changed

6 files changed

+130
-503
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
if: ${{ matrix.test-group == 'content' }}
5353
uses: getong/elasticsearch-action@95b501ab0c83dee0aac7c39b7cea3723bef14954
5454
with:
55-
elasticsearch version: '7.17.5'
55+
elasticsearch version: '7.11.1'
5656
host port: 9200
5757
container port: 9200
5858
host node port: 9300

middleware/api/es-search.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,24 @@ export async function getSearchResults({
4848
const highlight = getHighlightConfiguration(query)
4949

5050
const searchQuery = {
51-
index: indexName,
5251
highlight,
5352
from,
5453
size,
55-
// Since we know exactly which fields from the source we're going
56-
// need we can specify that here. It's an inclusion list.
57-
// We can save precious network by not having to transmit fields
58-
// stored in Elasticsearch to here if it's not going to be needed
59-
// anyway.
60-
_source_includes: [
61-
'title',
62-
'url',
63-
'breadcrumbs',
64-
// 'headings'
65-
'popularity',
66-
],
54+
55+
// COMMENTED out because of ES 7.11.
56+
// Once we're on ES >7.11 we can add this option in.
57+
// // Since we know exactly which fields from the source we're going
58+
// // need we can specify that here. It's an inclusion list.
59+
// // We can save precious network by not having to transmit fields
60+
// // stored in Elasticsearch to here if it's not going to be needed
61+
// // anyway.
62+
// _source_includes: [
63+
// 'title',
64+
// 'url',
65+
// 'breadcrumbs',
66+
// // 'headings'
67+
// 'popularity',
68+
// ],
6769
}
6870

6971
if (includeTopics) {
@@ -107,15 +109,17 @@ export async function getSearchResults({
107109
throw new Error(`Unrecognized sort enum '${sort}'`)
108110
}
109111

110-
const result = await client.search(searchQuery)
112+
const result = await client.search({ index: indexName, body: searchQuery })
111113

112-
const hits = getHits(result.hits.hits, { indexName, debug, includeTopics })
114+
// const hitsAll = result.hits // ES >7.11
115+
const hitsAll = result.body // ES <=7.11
116+
const hits = getHits(hitsAll.hits.hits, { indexName, debug, includeTopics })
113117
const t1 = new Date()
114118

115119
const meta = {
116-
found: result.hits.total,
120+
found: hitsAll.hits.total,
117121
took: {
118-
query_msec: result.took,
122+
query_msec: hitsAll.took,
119123
total_msec: t1.getTime() - t0.getTime(),
120124
},
121125
page,

middleware/api/search.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import express from 'express'
22

33
import searchVersions from '../../lib/search/versions.js'
4+
import FailBot from '../../lib/failbot.js'
45
import languages from '../../lib/languages.js'
56
import { allVersions } from '../../lib/all-versions.js'
67
import { defaultCacheControl } from '../cache-control.js'
@@ -234,20 +235,42 @@ router.get(
234235
notConfiguredMiddleware,
235236
catchMiddlewareError(async function search(req, res, next) {
236237
const { indexName, query, page, size, debug, sort } = req.search
237-
const { meta, hits } = await getSearchResults({ indexName, query, page, size, debug, sort })
238+
try {
239+
const { meta, hits } = await getSearchResults({ indexName, query, page, size, debug, sort })
238240

239-
if (process.env.NODE_ENV !== 'development') {
240-
// The assumption, at the moment is that searches are never distinguished
241-
// differently depending on a cookie or a request header.
242-
// So the only distinguishing key is the request URL.
243-
// Because of that, it's safe to allow the reverse proxy (a.k.a the CDN)
244-
// cache and hold on to this.
245-
defaultCacheControl(res)
246-
}
241+
if (process.env.NODE_ENV !== 'development') {
242+
// The assumption, at the moment is that searches are never distinguished
243+
// differently depending on a cookie or a request header.
244+
// So the only distinguishing key is the request URL.
245+
// Because of that, it's safe to allow the reverse proxy (a.k.a the CDN)
246+
// cache and hold on to this.
247+
defaultCacheControl(res)
248+
}
247249

248-
// The v1 version of the output matches perfectly what comes out
249-
// of the getSearchResults() function.
250-
res.status(200).json({ meta, hits })
250+
// The v1 version of the output matches perfectly what comes out
251+
// of the getSearchResults() function.
252+
res.status(200).json({ meta, hits })
253+
} catch (error) {
254+
// If getSearchResult() throws an error that might be 404 inside
255+
// elasticsearch, if we don't capture that here, it will propgate
256+
// to the next middleware.
257+
if (process.env.NODE_ENV === 'development') {
258+
console.error('Error calling getSearchResults()', error)
259+
} else {
260+
await Promise.all(
261+
FailBot.report(error, {
262+
url: req.url,
263+
indexName,
264+
query,
265+
page,
266+
size,
267+
debug,
268+
sort,
269+
})
270+
)
271+
}
272+
res.status(500).send(error.message)
273+
}
251274
})
252275
)
253276

0 commit comments

Comments
 (0)