in packages/search-ui-engines-connector/src/handlers/search/Configuration.ts [106:234]
function buildConfiguration({
state,
queryConfig
}: BuildConfigurationOptions): SearchkitConfig {
const { hitFields, highlightFields } = getResultFields(
queryConfig.result_fields
);
const filtersConfig: BaseFilter[] = Object.values(
(state.filters || [])
.filter((f) => !queryConfig.facets[f.field]) //exclude all filters that are defined as facets
.reduce((acc, f) => {
return {
...acc,
[f.field]: new SKFilter({
field: f.field,
identifier: f.field,
label: f.field
})
};
}, {})
);
const facets = Object.keys(queryConfig.facets || {}).reduce(
(acc, facetKey) => {
const facetConfiguration = queryConfig.facets[facetKey];
const isDisJunctive = queryConfig.disjunctiveFacets?.includes(facetKey);
if (facetConfiguration.type === "value") {
acc.push(
new RefinementSelectFacet({
identifier: facetKey,
field: facetKey,
label: facetKey,
size: facetConfiguration.size || 20,
multipleSelect: isDisJunctive,
order: facetConfiguration.sort || "count"
})
);
} else if (
facetConfiguration.type === "range" &&
!facetConfiguration.center
) {
acc.push(
new MultiQueryOptionsFacet({
identifier: facetKey,
field: facetKey,
label: facetKey,
multipleSelect: isDisJunctive,
options: facetConfiguration.ranges.map((range) => {
return {
label: range.name,
...(typeof range.from === "number" ? { min: range.from } : {}),
...(typeof range.to === "number" ? { max: range.to } : {}),
...(isValidDateString(range.from)
? { dateMin: range.from.toString() }
: {}),
...(isValidDateString(range.to)
? { dateMax: range.to.toString() }
: {})
};
})
})
);
} else if (
facetConfiguration.type === "range" &&
facetConfiguration.center
) {
acc.push(
new GeoDistanceOptionsFacet({
identifier: facetKey,
field: facetKey,
label: facetKey,
multipleSelect: isDisJunctive,
origin: facetConfiguration.center,
unit: facetConfiguration.unit,
ranges: facetConfiguration.ranges.map((range) => {
return {
label: range.name,
...(range.from ? { from: Number(range.from) } : {}),
...(range.to ? { to: Number(range.to) } : {})
};
})
})
);
}
return acc;
},
[]
);
const sortOption =
state.sortList?.length > 0
? {
id: "selectedOption",
label: "selectedOption",
field: state.sortList.reduce((acc, s) => {
acc.push({
[s.field]: s.direction
});
return acc;
}, [])
}
: { id: "selectedOption", label: "selectedOption", field: "_score" };
const jsVersion = typeof window !== "undefined" ? "browser" : process.version;
const metaHeader = `ent=${LIB_VERSION}-engines-connector,js=${jsVersion},t=${LIB_VERSION}-engines-connector,ft=universal`;
const configuration: SearchkitConfig = {
// overriding the transport and providing the host, index and apiKey to the transport
host: "host",
index: "engineName",
connectionOptions: {
headers: {
"x-elastic-client-meta": metaHeader
}
},
hits: {
fields: hitFields,
highlightedFields: highlightFields
},
query: EngineQuery(),
sortOptions: [sortOption],
facets,
filters: filtersConfig
};
return configuration;
}