Type Alias PrimitiveCustomMappingDefinition<T, R>

PrimitiveCustomMappingDefinition<T, R>: BaseCustomMappingDefinition & {
    graphQlType: T;
    isDefaultSortingField?: T extends PrimitiveTypeVariations<"Int" | "Float">
        ? boolean
        : never;
    outputFn?: ((values: any[], groupByProduct: boolean) => R);
    searchable?: T extends PrimitiveTypeVariations<"String" | "Geopoint">
        ? boolean
        : never;
    sort?: T extends PrimitiveTypeVariations<"String">
        ? boolean
        : never;
    valueFn: ((args: ValueFunctionArgs) => R | Promise<R>);
}

Defines a new CustomMapping which uses a primitive (scalar) GraphQL type as its return type. This type of customMapping is the simplest to define, and is suitable for most use-cases.

Type Parameters

  • T extends GraphQlPermittedReturnType
  • R

Type declaration

  • graphQlType: T

    The GraphQL data type that will be returned for this field in the customMappings object of a SearchResult.

  • OptionalisDefaultSortingField?: T extends PrimitiveTypeVariations<"Int" | "Float">
        ? boolean
        : never

    When set to true, this will set this field to be the default_sorting_field in the Typesense index, which means that it determines the order in which the search results are ranked when a sort_by clause is not provided during searching.

    Note that this can only be applied to numeric types, and only a single customMapping should be set as the default sorting field. If more than one is defined, the first is used.

  • OptionaloutputFn?: ((values: any[], groupByProduct: boolean) => R)

    This function defines how the stored value is converted to the GraphQL type when returned from the search query. By default, the value is returned as-is, and when grouping by product, the first value is returned.

    This function is especially useful when you need to have control over which value is selected when grouping by product. For example, if you have a customMapping which returns an array of values, you might want to return the largest value, or the average value, or the sum of the values.

    AdvancedSearchPlugin.init({
    // ...
    customMappings: {
    discount: {
    graphQlType: 'Float',
    valueFn: ({ variant }) => variant.product.customFields.discount,
    outputFn: (values: number[], groupByProduct: boolean) => {
    if (groupByProduct) {
    return Math.max(...values);
    } else {
    return values[0];
    }
    },
    },
    },
    });

    1.2.0

      • (values, groupByProduct): R
      • Parameters

        • values: any[]
        • groupByProduct: boolean

        Returns R

  • Optionalsearchable?: T extends PrimitiveTypeVariations<"String" | "Geopoint">
        ? boolean
        : never

    Defines whether this field should be searchable. If set to true, the field will be matched against the search term when performing a search, and will be available for configuration in the Admin UI search config view.

    May only be set to true for string types (i.e. where graphQlType is String or [String!].

    false
    
  • Optionalsort?: T extends PrimitiveTypeVariations<"String">
        ? boolean
        : never

    When set to true on a string type, allows results to be sorted by this string. This corresponds to the sort property on the Typesense field definition, available since Typesense v0.23.0.

    Number types are already sortable, so in those cases this setting is not applicable.

    Note that adding a sort on a string field requires the creation of a new index inside Typesense, so it should be used with care as sorting long string fields (like a description) or on very large data sets may significantly increase memory consumption.

    If this customMapping is defined as a sortableField via the sortableFields array, then this property will automatically get set to true, and can be omitted.

  • valueFn: ((args: ValueFunctionArgs) => R | Promise<R>)

    This function defines how the custom mapping data is obtained based on the ProductVariant and LanguageCode that get passed in.

      • (args): R | Promise<R>
      • Parameters

        • args: ValueFunctionArgs

        Returns R | Promise<R>