Skip to content

Commit

Permalink
Engines Connector (#919)
Browse files Browse the repository at this point in the history
* engines-connector
  • Loading branch information
joemcelroy authored Jan 27, 2023
1 parent fd66977 commit a3f818a
Show file tree
Hide file tree
Showing 34 changed files with 2,663 additions and 20 deletions.
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@
},
"console": "integratedTerminal",
"sourceMaps": true
},
{
"name": "Jest engines-connector",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
"stopOnEntry": false,
"args": ["--runInBand", "--forceExit", "--detectOpenHandles", "--watch"],
"cwd": "${workspaceRoot}/packages/search-ui-engines-connector",
"preLaunchTask": null,
"runtimeExecutable": null,
"env": {
"NODE_ENV": "test"
},
"console": "integratedTerminal",
"sourceMaps": true
}
]
}
1 change: 1 addition & 0 deletions examples/sandbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@elastic/search-ui-elasticsearch-connector": "1.18.3",
"@elastic/search-ui-site-search-connector": "1.18.3",
"@elastic/search-ui-workplace-search-connector": "1.18.3",
"@elastic/search-ui-engines-connector": "1.18.3",
"@emotion/react": "^11.8.1",
"autoprefixer": "^10.4.7",
"moment": "^2.24.0",
Expand Down
2 changes: 2 additions & 0 deletions examples/sandbox/src/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from "react";
import { Switch } from "react-router-dom";
import Root from "./pages/root";
import Elasticsearch from "./pages/elasticsearch";
import Engines from "./pages/engines";
import AppSearch from "./pages/app-search";
import SiteSearch from "./pages/site-search";
import WorkplaceSearch from "./pages/workplace-search";
Expand All @@ -26,6 +27,7 @@ export default function Router() {
<ApmRoute exact path="/app-search" component={AppSearch} />
<ApmRoute exact path="/site-search" component={SiteSearch} />
<ApmRoute exact path="/workplace-search" component={WorkplaceSearch} />
<ApmRoute exact path="/engines" component={Engines} />

{/* Examples */}
<ApmRoute
Expand Down
163 changes: 163 additions & 0 deletions examples/sandbox/src/pages/engines/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import React from "react";
import "@elastic/eui/dist/eui_theme_light.css";

import EnginesAPIConnector from "@elastic/search-ui-engines-connector";

import {
ErrorBoundary,
Facet,
SearchProvider,
SearchBox,
Results,
PagingInfo,
ResultsPerPage,
Paging,
Sorting,
WithSearch
} from "@elastic/react-search-ui";
import { Layout } from "@elastic/react-search-ui-views";
import "@elastic/react-search-ui-views/lib/styles/styles.css";

const connector = new EnginesAPIConnector({
host: "http://localhost:3002",
engineName: "puggles",
apiKey: "QTJDMHhJVUJENHg5WEJWcXlrWVQ6dXFlcF9QRWdRWHl2Rm9iSndka1Rndw=="
});

const config = {
debug: true,
alwaysSearchOnInitialLoad: true,
apiConnector: connector,
hasA11yNotifications: true,
autocompleteQuery: {
results: {
search_fields: {
name: {}
},
resultsPerPage: 5,
result_fields: {
name: {
snippet: {
size: 100,
fallback: true
}
}
}
}
},
searchQuery: {
filters: [],
search_fields: {
name: {
weight: 3
}
},
result_fields: {
id: { raw: {} },
name: { raw: {}, snippet: { size: 100, fallback: true } },
color: { raw: {} },
age: { raw: {} }
},
disjunctiveFacets: ["color", "age"],
facets: {
color: { type: "value" },
age: {
type: "range",
ranges: [
{ to: 2, name: "New born" },
{ from: 2, to: 3, name: "Puppy" },
{ from: 3, to: 13, name: "Mid" },
{ from: 14, name: "senior" }
]
}
}
}
};

const SORT_OPTIONS = [
{
name: "Relevance",
value: []
},
{
name: "Youngest",
value: [
{
field: "age",
direction: "asc"
}
]
},
{
name: "Oldest",
value: [
{
field: "age",
direction: "desc"
}
]
}
];

export default function App() {
return (
<SearchProvider config={config}>
<WithSearch
mapContextToProps={({ wasSearched }) => ({
wasSearched
})}
>
{({ wasSearched }) => {
return (
<div className="App">
<ErrorBoundary>
<Layout
header={
<SearchBox
debounceLength={0}
autocompleteResults={{
linkTarget: "_blank",
sectionTitle: "Results",
titleField: "name",
urlField: "name"
}}
/>
}
sideContent={
<div>
{wasSearched && (
<Sorting label={"Sort by"} sortOptions={SORT_OPTIONS} />
)}
<Facet
field="color"
label="Color"
filterType="any"
isFilterable={true}
/>
<Facet
field="age"
label="Age"
filterType="any"
isFilterable={true}
/>
</div>
}
bodyContent={
<Results titleField="name" shouldTrackClickThrough={true} />
}
bodyHeader={
<React.Fragment>
{wasSearched && <PagingInfo />}
{wasSearched && <ResultsPerPage />}
</React.Fragment>
}
bodyFooter={<Paging />}
/>
</ErrorBoundary>
</div>
);
}}
</WithSearch>
</SearchProvider>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,21 @@ export default async function handleRequest(
);

const results: AutocompleteResponseState = response.reduce(
(sum, suggestion) => {
(acc, suggestion) => {
const { identifier } = suggestion;

if (identifier === "hits-suggestions") {
return {
...sum,
...acc,
autocompletedResults: suggestion.hits.map(fieldResponseMapper)
};
} else if (identifier.startsWith("suggestions-completion-")) {
const name = identifier.replace("suggestions-completion-", "");

return {
...sum,
...acc,
autocompletedSuggestions: {
...sum.autocompletedSuggestions,
...acc.autocompletedSuggestions,
[name]: suggestion.suggestions.map((suggestion) => {
return {
suggestion: suggestion
Expand All @@ -135,9 +135,9 @@ export default async function handleRequest(
name
] as ResultSuggestionConfiguration;
return {
...sum,
...acc,
autocompletedSuggestions: {
...sum.autocompletedSuggestions,
...acc.autocompletedSuggestions,
[name]: suggestion.hits.map((hit) => ({
queryType: config.queryType,
result: fieldResponseMapper(hit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export function getResultFields(
} {
const hitFields = Object.keys(resultFields);

const highlightFields = Object.keys(resultFields).reduce((sum, fieldKey) => {
const highlightFields = Object.keys(resultFields).reduce((acc, fieldKey) => {
const fieldConfiguration = resultFields[fieldKey];
if (fieldConfiguration.snippet) {
sum.push(fieldKey);
acc.push(fieldKey);
}
return sum;
return acc;
}, []);

return { hitFields, highlightFields };
Expand Down Expand Up @@ -67,14 +67,14 @@ export function isRangeFilter(
}

export function buildBaseFilters(baseFilters: Filter[]): BaseFilters {
const filters = (baseFilters || []).reduce((sum, filter) => {
const filters = (baseFilters || []).reduce((acc, filter) => {
const boolType = {
all: "filter",
any: "should",
none: "must_not"
}[filter.type];
return [
...sum,
...acc,
{
bool: {
[boolType]: filter.values.map((value: FilterValue) => {
Expand Down Expand Up @@ -144,9 +144,9 @@ function buildConfiguration({
const filtersConfig: BaseFilter[] = Object.values(
(state.filters || [])
.filter((f) => !queryConfig.facets[f.field]) //exclude all filters that are defined as facets
.reduce((sum, f) => {
.reduce((acc, f) => {
return {
...sum,
...acc,
[f.field]: new SKFilter({
field: f.field,
identifier: f.field,
Expand All @@ -157,11 +157,11 @@ function buildConfiguration({
);

const facets = Object.keys(queryConfig.facets || {}).reduce(
(sum, facetKey) => {
(acc, facetKey) => {
const facetConfiguration = queryConfig.facets[facetKey];
const isDisJunctive = queryConfig.disjunctiveFacets?.includes(facetKey);
if (facetConfiguration.type === "value") {
sum.push(
acc.push(
new RefinementSelectFacet({
identifier: facetKey,
field: facetKey,
Expand All @@ -175,7 +175,7 @@ function buildConfiguration({
facetConfiguration.type === "range" &&
!facetConfiguration.center
) {
sum.push(
acc.push(
new MultiQueryOptionsFacet({
identifier: facetKey,
field: facetKey,
Expand All @@ -200,7 +200,7 @@ function buildConfiguration({
facetConfiguration.type === "range" &&
facetConfiguration.center
) {
sum.push(
acc.push(
new GeoDistanceOptionsFacet({
identifier: facetKey,
field: facetKey,
Expand All @@ -219,7 +219,7 @@ function buildConfiguration({
);
}

return sum;
return acc;
},
[]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function getFilters(
baseFilters: Filter[] = []
): MixedFilter[] {
return filters.reduce((acc, f) => {
const isBaseFilter = baseFilters.find((bf) => bf === f);
const isBaseFilter = baseFilters.includes(f);
if (isBaseFilter) return acc;

const subFilters = f.values.map((v) => {
Expand Down
Loading

0 comments on commit a3f818a

Please sign in to comment.