npm package: @haus-tech/elastic-extended-filters-plugin
Description: Adds a filter to the ElasticSearch plugin to allow for NOT filters
Author: Haus Tech
Latest version: 3.0.3

---
name: elastic-filter-not-plugin
title: Elastic Filter Not Plugin
description: Vendure plugin that extends the filtering capabilities of your e-commerce platform by adding support for "NOT" filters in Elasticsearch queries.
version: 3.0.0
---

# Elastic Filter Not Plugin

The `Elastic Filter Not Plugin` is a Vendure plugin that extends the filtering capabilities of your e-commerce platform by adding support for "NOT" filters in Elasticsearch queries. This plugin is particularly useful for scenarios where you need to exclude specific products or collections from search results.

## Functionality

This plugin enables the following features:

- Adds "NOT" filtering capabilities to Elasticsearch queries.
- Seamlessly integrates with Vendure's search functionality.
- Customizable and easy to configure.

## Use Cases

The Elastic Filter Not Plugin is ideal for:

- Excluding specific products or collections from search results.
- Enhancing search functionality with advanced filtering options.
- E-commerce stores that require precise control over search results.

## Installation

To install the `Elastic Filter Not Plugin`, follow these steps:

1. Install the plugin package:

   ```bash
   yarn add @haus-tech/elastic-filter-not-plugin
   ```

   Or, if using npm:

   ```bash
   npm install @haus-tech/elastic-filter-not-plugin
   ```

2. Add the plugin to your Vendure configuration in `vendure-config.ts`:

   ```ts
   import { ElasticFilterNotPlugin } from '@haus-tech/elastic-filter-not-plugin';

   export const config = {
     plugins: [
       ElasticFilterNotPlugin,
     ],
   };
   ```

3. Restart your Vendure server.

## Usage

### Admin Configuration

No additional configuration is required in the Admin UI. The plugin works seamlessly with Elasticsearch and extends the existing search functionality.

### Shop API

The plugin extends the shop API to support "NOT" filters in search queries. For example, you can exclude products with specific attributes using the following GraphQL query:

```graphql
query SearchProducts($input: SearchInput!) {
  search(input: $input) {
    items {
      productId
      productName
    }
  }
}
```

Example `SearchInput` with a "NOT" filter applied to `facetValueFilters`:

```json
{
  "term": "shoes",
  "facetValueFilters": [
    {
      "not": ["sports"]
    }
  ]
}
```

### Example Integration

To use the "NOT" filter in your storefront, modify your search queries to include the `not` field in the `facetValueFilters`. For example:

```ts
fetch('/shop-api', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    query: `
      query SearchProducts($input: SearchInput!) {
        search(input: $input) {
          items {
            productId
            productName
          }
        }
      }
    `,
    variables: {
      input: {
        term: 'shoes',
        facetValueFilters: [
          {
            not: ['sports'],
          },
        ],
      },
    },
  }),
})
  .then((res) => res.json())
  .then((data) => {
    console.log(data);
  });
```

## Testing

1. Run `yarn test` to execute the e2e tests.
2. Implement additional tests to cover your specific use cases.

## Publishing to NPM

1. Make sure you are [logged in to NPM](https://docs.npmjs.com/cli/v9/commands/npm-login).
2. Build the plugin:

   ```bash
   yarn build
   ```

3. Publish the plugin:

   ```bash
   yarn publish
   ```

## Resources

- [Vendure Plugin Documentation](https://www.vendure.io/docs/plugins/)
- [GraphQL Code Generator](https://the-guild.dev/graphql/codegen) for generating TypeScript types for custom GraphQL types.