ElasticsearchPlugin

ElasticsearchPlugin

ElasticsearchPlugin

This plugin allows your product search to be powered by Elasticsearch - a powerful Open Source search engine. This is a drop-in replacement for the DefaultSearchPlugin.

Installation

yarn add @vendure/elasticsearch-plugin

or

npm install @vendure/elasticsearch-plugin

Make sure to remove the DefaultSearchPlugin if it is still in the VendureConfig plugins array.

Then add the ElasticsearchPlugin, calling the .init() method with ElasticsearchOptions:

Example

import { ElasticsearchPlugin } from '@vendure/elasticsearch-plugin';

const config: VendureConfig = {
  // Add an instance of the plugin to the plugins array
  plugins: [
    ElasticsearchPlugin.init({
      host: 'http://localhost',
      port: 9200,
    }),
  ],
};

Search API Extensions

This plugin extends the default search query of the Shop API, allowing richer querying of your product data.

The SearchResponse type is extended with information about price ranges in the result set:

extend type SearchResponse {
    prices: SearchResponsePriceData!
}

type SearchResponsePriceData {
    range: PriceRange!
    rangeWithTax: PriceRange!
    buckets: [PriceRangeBucket!]!
    bucketsWithTax: [PriceRangeBucket!]!
}

type PriceRangeBucket {
    to: Int!
    count: Int!
}

extend input SearchInput {
    priceRange: PriceRangeInput
    priceRangeWithTax: PriceRangeInput
}

input PriceRangeInput {
    min: Int!
    max: Int!
}

This SearchResponsePriceData type allows you to query data about the range of prices in the result set.

Example Request & Response

{
  search (input: {
    term: "table easel"
    groupByProduct: true
    priceRange: {
      min: 500
      max: 7000
    }
  }) {
    totalItems
    prices {
      range {
        min
        max
      }
      buckets {
        to
        count
      }
    }
    items {
      productName
      score
      price {
        ...on PriceRange {
          min
          max
        }
      }
    }
  }
}
{
 "data": {
   "search": {
     "totalItems": 9,
     "prices": {
       "range": {
         "min": 999,
         "max": 6396,
       },
       "buckets": [
         {
           "to": 1000,
           "count": 1
         },
         {
           "to": 2000,
           "count": 2
         },
         {
           "to": 3000,
           "count": 3
         },
         {
           "to": 4000,
           "count": 1
         },
         {
           "to": 5000,
           "count": 1
         },
         {
           "to": 7000,
           "count": 1
         }
       ]
     },
     "items": [
       {
         "productName": "Loxley Yorkshire Table Easel",
         "score": 30.58831,
         "price": {
           "min": 4984,
           "max": 4984
         }
       },
       // ... truncated
     ]
   }
 }
}

Signature

class ElasticsearchPlugin implements OnVendureBootstrap, OnVendureClose {
  static init(options: ElasticsearchOptions) => Type<ElasticsearchPlugin>;
}

Members

init

static method
type:
(options: ElasticsearchOptions) => Type<ElasticsearchPlugin>

Contents: