Skip to content

Commit

Permalink
feat(elasticsearch-plugin): Added mapQuery option
Browse files Browse the repository at this point in the history
The discussion for the feature took place in vendure-ecommerce#364. This commit closes vendure-ecommerce#364.
  • Loading branch information
Tyratox committed Jun 8, 2020
1 parent 96ea811 commit e2c443c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
6 changes: 4 additions & 2 deletions packages/elasticsearch-plugin/src/build-elastic-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function buildElasticBody(
const operator = facetValueOperator === LogicalOperator.AND ? 'must' : 'should';
query.bool.filter = query.bool.filter.concat([
{
bool: { [operator]: facetValueIds.map(id => ({ term: { facetValueIds: id } })) },
bool: { [operator]: facetValueIds.map((id) => ({ term: { facetValueIds: id } })) },
},
]);
}
Expand Down Expand Up @@ -88,7 +88,9 @@ export function buildElasticBody(
}
}
return {
query,
query: searchConfig.mapQuery
? searchConfig.mapQuery(query, input, searchConfig, channelId, enabledOnly)
: query,
sort: sortArray,
from: skip || 0,
size: take || 10,
Expand Down
49 changes: 47 additions & 2 deletions packages/elasticsearch-plugin/src/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DeepRequired, Product, ProductVariant } from '@vendure/core';
import { DeepRequired, ID, Product, ProductVariant } from '@vendure/core';
import deepmerge from 'deepmerge';

import { CustomMapping } from './types';
import { CustomMapping, ElasticSearchInput } from './types';

/**
* @description
Expand Down Expand Up @@ -188,6 +188,50 @@ export interface SearchConfig {
* ```
*/
priceRangeBucketInterval?: number;
/**
* @description
* This config option allows the the modification of the whole (already built) search query. This allows
* for e.g. wildcard / fuzzy searches on the index.
*
* @example
* ```TypeScript
* mapQuery: (query, input, searchConfig, channelId, enabledOnly){
* if(query.bool.must){
* delete query.bool.must;
* }
* query.bool.should = [
* {
* query_string: {
* query: "*" + term + "*",
* fields: [
* `productName^${searchConfig.boostFields.productName}`,
* `productVariantName^${searchConfig.boostFields.productVariantName}`,
* ]
* }
* },
* {
* multi_match: {
* query: term,
* type: searchConfig.multiMatchType,
* fields: [
* `description^${searchConfig.boostFields.description}`,
* `sku^${searchConfig.boostFields.sku}`,
* ],
* },
* },
* ];
*
* return query;
* }
* ```
*/
mapQuery?: (
query: any,
input: ElasticSearchInput,
searchConfig: DeepRequired<SearchConfig>,
channelId: ID,
enabledOnly: boolean,
) => any;
}

/**
Expand Down Expand Up @@ -246,6 +290,7 @@ export const defaultOptions: DeepRequired<ElasticsearchOptions> = {
sku: 1,
},
priceRangeBucketInterval: 1000,
mapQuery: (query) => query,
},
customProductMappings: {},
customProductVariantMappings: {},
Expand Down

0 comments on commit e2c443c

Please sign in to comment.