ElasticsearchOptions

ElasticsearchOptions

Configuration options for the ElasticsearchPlugin.

Signature

interface ElasticsearchOptions {
  host?: string;
  port?: number;
  connectionAttempts?: number;
  connectionAttemptInterval?: number;
  clientOptions?: ClientOptions;
  indexPrefix?: string;
  indexSettings?: object;
  indexMappingProperties?: object;
  batchSize?: number;
  searchConfig?: SearchConfig;
  customProductMappings?: {
        [fieldName: string]: CustomMapping<[Product, ProductVariant[], LanguageCode]>;
    };
  customProductVariantMappings?: {
        [fieldName: string]: CustomMapping<[ProductVariant, LanguageCode]>;
    };
}

Members

host

property
type:
string
default:
'http://localhost'
The host of the Elasticsearch server. May also be specified in clientOptions.node.

port

property
type:
number
default:
9200
The port of the Elasticsearch server. May also be specified in clientOptions.node.

connectionAttempts

property
type:
number
default:
10
Maximum amount of attempts made to connect to the ElasticSearch server on startup.

connectionAttemptInterval

property
type:
number
default:
5000
Interval in milliseconds between attempts to connect to the ElasticSearch server on startup.

clientOptions

property
type:
ClientOptions
Options to pass directly to the Elasticsearch Node.js client. For example, to set authentication or other more advanced options. Note that if the node or nodes option is specified, it will override the values provided in the host and port options.

indexPrefix

property
type:
string
default:
'vendure-'
Prefix for the indices created by the plugin.

indexSettings

property
v1.2.0
type:
object
default:
{}

These options are directly passed to index settings. To apply some settings indices will be recreated.

Example

// Configuring an English stemmer
indexSettings: {
  analysis: {
    analyzer: {
      custom_analyzer: {
        tokenizer: 'standard',
        filter: [
          'lowercase',
          'english_stemmer'
        ]
      }
    },
    filter : {
      english_stemmer : {
        type : 'stemmer',
        name : 'english'
      }
    }
  }
},

indexMappingProperties

property
v1.2.0
type:
object
default:
{}

This option allow to redefine or define new properties in mapping. More about elastic mapping After changing this option indices will be recreated.

Example

// Configuring custom analyzer for the `productName` field.
indexMappingProperties: {
  productName: {
    type: 'text',
    analyzer:'custom_analyzer',
    fields: {
      keyword: {
        type: 'keyword',
        ignore_above: 256,
      }
    }
  }
}

batchSize

property
type:
number
default:
2000
Batch size for bulk operations (e.g. when rebuilding the indices).

searchConfig

property
type:
SearchConfig
Configuration of the internal Elasticseach query.

customProductMappings

property
type:
{ [fieldName: string]: CustomMapping<[Product, ProductVariant[], LanguageCode]>; }

Custom mappings may be defined which will add the defined data to the Elasticsearch index and expose that data via the SearchResult GraphQL type, adding a new customMappings field.

The graphQlType property may be one of String, Int, Float, Boolean and can be appended with a ! to indicate non-nullable fields.

This config option defines custom mappings which are accessible when the “groupByProduct” input options is set to true.

Example

customProductMappings: {
   variantCount: {
       graphQlType: 'Int!',
       valueFn: (product, variants) => variants.length,
   },
   reviewRating: {
       graphQlType: 'Float',
       valueFn: product => (product.customFields as any).reviewRating,
   },
}

Example

query SearchProducts($input: SearchInput!) {
    search(input: $input) {
        totalItems
        items {
            productId
            productName
            customMappings {
                ...on CustomProductMappings {
                    variantCount
                    reviewRating
                }
            }
        }
    }
}

customProductVariantMappings

property
type:
{ [fieldName: string]: CustomMapping<[ProductVariant, LanguageCode]>; }

This config option defines custom mappings which are accessible when the “groupByProduct” input options is set to false.

Example

query SearchProducts($input: SearchInput!) {
    search(input: $input) {
        totalItems
        items {
            productId
            productName
            customMappings {
                ...on CustomProductVariantMappings {
                    weight
                }
            }
        }
    }
}

SearchConfig

Configuration options for the internal Elasticsearch query which is generated when performing a search.

Signature

interface SearchConfig {
  facetValueMaxSize?: number;
  collectionMaxSize?: number;
  totalItemsMaxSize?: number | boolean;
  multiMatchType?: 'best_fields' | 'most_fields' | 'cross_fields' | 'phrase' | 'phrase_prefix' | 'bool_prefix';
  boostFields?: BoostFieldsConfig;
  priceRangeBucketInterval?: number;
  mapQuery?: (
        query: any,
        input: ElasticSearchInput,
        searchConfig: DeepRequired<SearchConfig>,
        channelId: ID,
        enabledOnly: boolean,
    ) => any;
}

Members

facetValueMaxSize

property
type:
number
default:
50
The maximum number of FacetValues to return from the search query. Internally, this value sets the “size” property of an Elasticsearch aggregation.

collectionMaxSize

property
v1.1.0
type:
number
default:
50
The maximum number of Collections to return from the search query. Internally, this value sets the “size” property of an Elasticsearch aggregation.

totalItemsMaxSize

property
v1.2.0
type:
number | boolean
default:
10000
The maximum number of totalItems to return from the search query. Internally, this value sets the “track_total_hits” property of an Elasticsearch query. If this parameter is set to “True”, accurate count of totalItems will be returned. If this parameter is set to “False”, totalItems will be returned as 0. If this parameter is set to integer, accurate count of totalItems will be returned not bigger than integer.

multiMatchType

property
type:
'best_fields' | 'most_fields' | 'cross_fields' | 'phrase' | 'phrase_prefix' | 'bool_prefix'
default:
'best_fields'
Defines the multi match type used when matching against a search term.

boostFields

property
Set custom boost values for particular fields when matching against a search term.

priceRangeBucketInterval

property
type:
number

The interval used to group search results into buckets according to price range. For example, setting this to 2000 will group into buckets every $20.00:

{
  "data": {
    "search": {
      "totalItems": 32,
      "priceRange": {
        "buckets": [
          {
            "to": 2000,
            "count": 21
          },
          {
            "to": 4000,
            "count": 7
          },
          {
            "to": 6000,
            "count": 3
          },
          {
            "to": 12000,
            "count": 1
          }
        ]
      }
    }
  }
}

mapQuery

property
type:
( query: any, input: ElasticSearchInput, searchConfig: DeepRequired<SearchConfig>, channelId: ID, enabledOnly: boolean, ) => any

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

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;
}

BoostFieldsConfig

Configuration for boosting the scores of given fields when performing a search against a term.

Boosting a field acts as a score multiplier for matches against that field.

Signature

interface BoostFieldsConfig {
  productName?: number;
  productVariantName?: number;
  description?: number;
  sku?: number;
}

Members

productName

property
type:
number
default:
1
Defines the boost factor for the productName field.

productVariantName

property
type:
number
default:
1
Defines the boost factor for the productVariantName field.

description

property
type:
number
default:
1
Defines the boost factor for the description field.

sku

property
type:
number
default:
1
Defines the boost factor for the sku field.