| 351 | } |
| 352 | |
| 353 | function getHits(hits, { indexName, debug, includeTopics, highlightFields }) { |
| 354 | return hits.map((hit) => { |
| 355 | // Return `hit.highlights[...]` based on the highlight fields requested. |
| 356 | // So if you searched with `&highlights=headings&highlights=content` |
| 357 | // this will become: |
| 358 | // { |
| 359 | // content: [...], |
| 360 | // headings: [...] |
| 361 | // } |
| 362 | // even if there was a match on 'title'. |
| 363 | const hitHighlights = Object.fromEntries( |
| 364 | highlightFields.map((key) => [key, (hit.highlight && hit.highlight[key]) || []]) |
| 365 | ) |
| 366 | |
| 367 | const result = { |
| 368 | id: hit._id, |
| 369 | url: hit._source.url, |
| 370 | title: hit._source.title, |
| 371 | breadcrumbs: hit._source.breadcrumbs, |
| 372 | highlights: hitHighlights, |
| 373 | } |
| 374 | if (includeTopics) { |
| 375 | result.topics = hit._source.topics || [] |
| 376 | } |
| 377 | if (debug) { |
| 378 | result.score = hit._score || 0.0 |
| 379 | result.popularity = hit._source.popularity || 0.0 |
| 380 | if (isDevMode) { |
| 381 | result.es_url = `http://localhost:9200/${indexName}/_doc/${hit._id}` |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | return result |
| 386 | }) |
| 387 | } |
| 388 | |
| 389 | // The highlight configuration is dependent on how we use the content |
| 390 | // in the UI. For example, we feel we need about 3 lines (max) |