forked from elastic/search-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildState.js
More file actions
74 lines (62 loc) · 2 KB
/
Copy pathbuildState.js
File metadata and controls
74 lines (62 loc) · 2 KB
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
import buildStateFacets from "./buildStateFacets";
function buildTotalPages(resultsPerPage, totalResults) {
if (!resultsPerPage) return 0;
if (totalResults === 0) return 1;
return Math.ceil(totalResults / resultsPerPage);
}
function buildTotalResults(hits) {
return hits.total.value;
}
function getHighlight(hit, fieldName) {
if (hit._source.title === "Rocky Mountain" && fieldName === "title") {
window.hit = hit;
window.fieldName = fieldName;
}
if (
!hit.highlight ||
!hit.highlight[fieldName] ||
hit.highlight[fieldName].length < 1
) {
return;
}
return hit.highlight[fieldName][0];
}
function buildResults(hits) {
const addEachKeyValueToObject = (acc, [key, value]) => ({
...acc,
[key]: value
});
const toObject = (value, snippet) => {
return { raw: value, ...(snippet && { snippet }) };
};
return hits.map(record => {
return Object.entries(record._source)
.map(([fieldName, fieldValue]) => [
fieldName,
toObject(fieldValue, getHighlight(record, fieldName))
])
.reduce(addEachKeyValueToObject, {});
});
}
/*
Converts an Elasticsearch response to new application state
When implementing an onSearch Handler in Search UI, the handler needs to convert
search results into a new application state that Search UI understands.
For instance, Elasticsearch returns "hits" for search results. This maps to
the "results" property in application state, which requires a specific format. So this
file iterates through "hits" and reformats them to "results" that Search UI
understands.
We do similar things for facets and totals.
*/
export default function buildState(response, resultsPerPage) {
const results = buildResults(response.hits.hits);
const totalResults = buildTotalResults(response.hits);
const totalPages = buildTotalPages(resultsPerPage, totalResults);
const facets = buildStateFacets(response.aggregations);
return {
results,
totalPages,
totalResults,
...(facets && { facets })
};
}