forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-qa-test.js
executable file
·93 lines (80 loc) · 2.54 KB
/
search-qa-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
// [start-readme]
//
// This script is a quality assurance test for the Lunr search configuration.
// This test runs example queries and expects a specific page to land in the top
// 3 results.
//
// The data source used by this script is a JSON file `script/search/search-qa-data.json`,
// which is populated from spreadsheet data here:
// https://docs.google.com/spreadsheets/d/1Dt5JRVcmyAGWKBwGjwmXxi7Ww_vdfYLfZ-EFpu2S2CQ/edit?usp=sharing
//
// [end-readme]
import loadLunrResults from '../../lib/search/lunr-search.js'
import { readFileSync } from 'fs'
import { join } from 'path'
const queryData = JSON.parse(readFileSync(join(process.cwd(), 'script/search/search-qa-data.json')))
const version = 'dotcom'
const language = 'en'
const limit = 10
const TOP_RANK = 3
main()
async function main() {
const rankResults = []
for (const item in queryData) {
const { query, href } = queryData[item]
try {
const results = await loadLunrResults({
version,
language,
query,
limit,
})
const hrefs = results.map((result) => result.url.replace('/en', ''))
let rank = hrefs.indexOf(href)
// this allows us to sort the results by rank, including total misses
if (rank === -1) {
rank = limit
}
rankResults.push({ query, href, rank })
} catch (err) {
console.error(err)
}
}
logResults(rankResults)
}
async function logResults(results) {
results.sort((a, b) => a.rank - b.rank)
let first = 0
let top = 0
let low = 0
let miss = 0
results.forEach((result) => {
const { query, href, rank } = result
if (rank === limit) {
miss++
console.log(`🔴 query: ${query} - Expected href: ${href}\n`)
return
}
if (rank === 0) {
first++
console.log(`⭐ Query: ${query} - Expected href: ${href}`)
return
}
if (rank < TOP_RANK) {
top++
console.log(`🟢 Query: ${query} - Expected href: ${href}`)
return
}
low++
console.log(`🟡 Query: ${query} - Expected href: ${href}`)
})
const firstPercentage = ((first / queryData.length) * 100).toFixed(1)
const topPercentage = ((top / queryData.length) * 100).toFixed(1)
const lowPercentage = ((low / queryData.length) * 100).toFixed(1)
const missPercentage = ((miss / queryData.length) * 100).toFixed(1)
console.log(`\n⭐ First hit ${firstPercentage}%`)
console.log(`\n🟢 Top ${TOP_RANK} hit ${topPercentage}%`)
console.log(`\n🟡 Top ${limit} hit ${lowPercentage}%`)
console.log(`\n🔴 Miss ${missPercentage}%`)
}