Crystallize Elasticsearch JavaScript Example
An example service for integrating Elasticsearch with Crystallize in JavaScript.
https://github.com/crystallizeapi/crystallize-elasticsearch-example-js
Clone the repository into your workspace and install the necessary dependencies.
git clone https://github.com/CrystallizeAPI/crystallize-elasticsearch-example-js.git
cd crystallize-elasticsearch-example-js
npm install
# or
yarn install
Now we need to set up our Elasticsearch instance. You can found out how to do that here.
Create a .env file in the root your project and add your Elasticsearch environment variables. If you are using the local Elasticsearch cluster provided by the docker-compose file, you only need to specify the node.
ES_NODE=http://localhost:9200
If you are using Elastic Cloud or have authentication on your Elasticsearch cluster you will need to specify the username and password as well.
ES_NODE=<your-elastic-deployment-url>
ES_USER=<your-elastic-deployment-user>
ES_PASS=<your-elastic-deployment-pass>
Being an example, this project exposes both queries for searching as well as mutations for indexing a tenant via GraphQL. You may wish to remove mutations from a publicly exposed endpoint.
You can run the server with either yarn start for production, or yarn dev for local development.
This will provide you with a playground available at http://localhost:4000/graphql. You can also call this endpoint via a `POST` request with a GraphQL body.
You can search your catalogue using the productVariants query. You can view the full schema via the GraphQL playground with all the fields you can query, search and filter. For example:
query {
productVariants(
filter: { searchTerm: "kiwi" }
orderBy: { field: PRICE, direction: DESC }
) {
totalCount
productVariants {
variant {
name
price
stock
}
product {
name
}
}
}
}