Skip to content

Commit

Permalink
Merge pull request #10 from afosto/Feature/combine-multiple-search-re…
Browse files Browse the repository at this point in the history
…quests

Feature/combine-multiple-search-requests
  • Loading branch information
Rapid0o authored Mar 10, 2022
2 parents 575426e + f1ba191 commit 06f99a8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@afosto/instant-search-client",
"version": "1.0.2",
"version": "1.0.3",
"private": false,
"description": "The Afosto InstantSearch client",
"main": "./dist/afosto-instant-search.min.js",
Expand Down Expand Up @@ -53,6 +53,9 @@
"webpack-cli": "^4.9.1",
"webpack-dev-server": "^4.6.0"
},
"dependencies": {
"uuid": "^8.3.2"
},
"browserslist": [
">0.2%",
"not dead",
Expand Down
7 changes: 1 addition & 6 deletions playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ <h2>Products</h2>
});

search.addWidgets([
instantsearch.widgets.configure({
facets: ['main_genre', 'release_year_range'],
}),

instantsearch.widgets.searchBox({
container: '#searchbox',
}),
Expand All @@ -84,9 +80,8 @@ <h2>Products</h2>

instantsearch.widgets.dynamicWidgets({
container: '#dynamic-list',
facets: [],
transformItems(_, { results }) {
return results.facets.map(facet => facet.name);
return Object.keys(results._rawResults[0].facets);
},
widgets: filterKeys.map((attribute) => (container) =>
instantsearch.widgets.panel({
Expand Down
1 change: 1 addition & 0 deletions src/adapters/SearchRequestAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const SearchRequestAdapter = () => {
indices: [request.indexName],
q: query,
threshold: options.threshold || DEFAULT_OPTIONS.threshold,
__queryID: request.__queryID,
...pagination,
}];
}, []);
Expand Down
3 changes: 2 additions & 1 deletion src/adapters/SearchResponseAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const SearchResponseAdapter = () => {
const nbHits = results.count ?? 0;
const nbPages = Math.ceil(nbHits / hitsPerPage);
const index = results.id ?? request?.indexName;
const queryID = results.__queryID ?? index;

return {
facets,
Expand All @@ -33,7 +34,7 @@ const SearchResponseAdapter = () => {
nbPages,
page,
query,
queryID: index,
queryID,
};
};

Expand Down
27 changes: 14 additions & 13 deletions src/afostoInstantSearch.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { v4 as uuid } from 'uuid';
import { SearchResponseAdapter, SearchRequestAdapter } from './adapters';
import { DEFAULT_OPTIONS } from './constants';

Expand Down Expand Up @@ -33,11 +34,11 @@ const afostoInstantSearch = (searchEngineKey, options = {}) => {
}
};

const searchRequest = async context => {
const searchRequest = async contexts => {
const requestOptions = clientOptions.requestOptions || {};
const hasContextFormatter = clientOptions.transformContext && typeof clientOptions.transformContext === 'function';
const hasResponseFormatter = clientOptions.transformResponse && typeof clientOptions.transformResponse === 'function';
const payload = hasContextFormatter ? clientOptions.transformContext(context) : context;
const payload = hasContextFormatter ? contexts.map(context => clientOptions.transformContext(context)) : contexts;
const searchResponse = await fetch(url, {
...requestOptions,
method: 'POST',
Expand All @@ -47,27 +48,27 @@ const afostoInstantSearch = (searchEngineKey, options = {}) => {
},
body: JSON.stringify({ data: payload }),
});
const response = await searchResponse.json();
const responses = await searchResponse.json();
const formattedResponses = Array.isArray(responses) ? responses: [responses];

return hasResponseFormatter ? clientOptions.transformResponse(response) : response;
return hasResponseFormatter ? formattedResponses.map(response => clientOptions.transformResponse(response)) : formattedResponses;
}

const search = async requests => {
try {
const searchContexts = searchRequestAdapter.transform(requests, clientOptions);
const searchRequests = requests.map(request => ({ ...request, __queryID: uuid() }));
const searchContexts = searchRequestAdapter.transform(searchRequests, clientOptions);
const [searchRequestContext] = searchContexts;

if (!clientOptions.allowEmptyQuery && !searchRequestContext?.q) {
return searchResponseAdapter.transform({}, requests, clientOptions);
return searchResponseAdapter.transform({}, searchRequests, clientOptions);
}

const promises = searchContexts.map(context => searchRequest(context));
const responses = await Promise.allSettled(promises);
const responseValues = responses.map(response => response.value || {});

const results = requests.reduce((acc, request, idx) => {
const response = searchResponseAdapter.transform(responseValues[idx], request, clientOptions);
return [...acc, response];
const responses = await searchRequest(searchContexts);
const results = searchRequests.reduce((acc, request) => {
const response = responses.find(value => value.__queryID === request.__queryID);
const result = searchResponseAdapter.transform(response, request, clientOptions);
return [...acc, result];
}, []);

return { results };
Expand Down
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const DEFAULT_OPTIONS = {
allowEmptyQuery: true,
baseUrl: 'https://afosto.io/api/instant/search/{key}',
baseUrl: 'https://afosto.app/api/instant/search/{key}',
hitsPerPage: 10,
requestOptions: {},
settingsRequestOptions: {},
Expand Down

0 comments on commit 06f99a8

Please sign in to comment.