|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +This guide will help you get started with feathers-elasticsearch, a Feathers database adapter for Elasticsearch. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install feathers-elasticsearch @elastic/elasticsearch --save |
| 9 | +``` |
| 10 | + |
| 11 | +## Compatibility |
| 12 | + |
| 13 | +- **Feathers v5** (Dove) |
| 14 | +- **Elasticsearch 8.x and 9.x** |
| 15 | +- **Node.js 18+** |
| 16 | + |
| 17 | +> **Important:** `feathers-elasticsearch` implements the [Feathers Common database adapter API](https://docs.feathersjs.com/api/databases/common.html) and [querying syntax](https://docs.feathersjs.com/api/databases/querying.html). |
| 18 | +
|
| 19 | +## Supported Elasticsearch Versions |
| 20 | + |
| 21 | +feathers-elasticsearch is currently tested on Elasticsearch 5.0, 5.6, 6.6, 6.7, 6.8, 7.0, 7.1, 8.x, and 9.x. |
| 22 | + |
| 23 | +> **Note:** We have recently dropped support for version 2.4, as its life ended quite a while back. If you are still running Elasticsearch 2.4 and want to take advantage of feathers-elasticsearch, please use version 2.x of this package. |
| 24 | +
|
| 25 | +## Basic Example |
| 26 | + |
| 27 | +The following bare-bones example will create a `messages` endpoint and connect to a local `messages` type in the `test` index in your Elasticsearch database: |
| 28 | + |
| 29 | +```js |
| 30 | +const feathers = require('@feathersjs/feathers'); |
| 31 | +const elasticsearch = require('@elastic/elasticsearch'); |
| 32 | +const service = require('feathers-elasticsearch'); |
| 33 | + |
| 34 | +const app = feathers(); |
| 35 | + |
| 36 | +app.use('/messages', service({ |
| 37 | + Model: new elasticsearch.Client({ |
| 38 | + node: 'http://localhost:9200' |
| 39 | + }), |
| 40 | + elasticsearch: { |
| 41 | + index: 'test', |
| 42 | + type: 'messages' |
| 43 | + } |
| 44 | +})); |
| 45 | +``` |
| 46 | + |
| 47 | +## Complete Example |
| 48 | + |
| 49 | +Here's a complete example of a Feathers server with REST API using `feathers-elasticsearch`: |
| 50 | + |
| 51 | +```js |
| 52 | +const feathers = require('@feathersjs/feathers'); |
| 53 | +const express = require('@feathersjs/express'); |
| 54 | +const service = require('feathers-elasticsearch'); |
| 55 | +const elasticsearch = require('@elastic/elasticsearch'); |
| 56 | + |
| 57 | +// Create the Elasticsearch client |
| 58 | +const esClient = new elasticsearch.Client({ |
| 59 | + node: 'http://localhost:9200' |
| 60 | +}); |
| 61 | + |
| 62 | +// Create the message service |
| 63 | +const messageService = service({ |
| 64 | + Model: esClient, |
| 65 | + paginate: { |
| 66 | + default: 10, |
| 67 | + max: 50 |
| 68 | + }, |
| 69 | + elasticsearch: { |
| 70 | + index: 'test', |
| 71 | + type: 'messages', |
| 72 | + refresh: true // Make changes immediately visible (not recommended for production) |
| 73 | + }, |
| 74 | + esVersion: '8.0' |
| 75 | +}); |
| 76 | + |
| 77 | +// Initialize the application |
| 78 | +const app = express(feathers()); |
| 79 | + |
| 80 | +// Enable JSON parsing |
| 81 | +app.use(express.json()); |
| 82 | +app.use(express.urlencoded({ extended: true })); |
| 83 | + |
| 84 | +// Enable REST services |
| 85 | +app.configure(express.rest()); |
| 86 | + |
| 87 | +// Register the message service |
| 88 | +app.use('/messages', messageService); |
| 89 | + |
| 90 | +// Enable error handling |
| 91 | +app.use(express.errorHandler()); |
| 92 | + |
| 93 | +// Start the server |
| 94 | +app.listen(3030); |
| 95 | + |
| 96 | +console.log('Feathers app started on http://127.0.0.1:3030'); |
| 97 | +``` |
| 98 | + |
| 99 | +### Testing Your Setup |
| 100 | + |
| 101 | +You can run this example and test it: |
| 102 | + |
| 103 | +1. Start your Elasticsearch server (ensure it's running on `localhost:9200`) |
| 104 | +2. Run the example code above |
| 105 | +3. Visit [http://localhost:3030/messages](http://localhost:3030/messages) |
| 106 | + |
| 107 | +You should see an empty array `[]`. That's because you don't have any messages yet, but you now have full CRUD for your new message service! |
| 108 | + |
| 109 | +### Creating Your First Document |
| 110 | + |
| 111 | +Using curl: |
| 112 | + |
| 113 | +```bash |
| 114 | +curl -X POST http://localhost:3030/messages \ |
| 115 | + -H "Content-Type: application/json" \ |
| 116 | + -d '{"text": "Hello Feathers!"}' |
| 117 | +``` |
| 118 | + |
| 119 | +Or using JavaScript: |
| 120 | + |
| 121 | +```js |
| 122 | +// Using the Feathers client |
| 123 | +const message = await app.service('messages').create({ |
| 124 | + text: 'Hello Feathers!' |
| 125 | +}); |
| 126 | + |
| 127 | +console.log(message); |
| 128 | +``` |
| 129 | + |
| 130 | +### Querying Documents |
| 131 | + |
| 132 | +```js |
| 133 | +// Find all messages |
| 134 | +const messages = await app.service('messages').find(); |
| 135 | + |
| 136 | +// Find with query |
| 137 | +const results = await app.service('messages').find({ |
| 138 | + query: { |
| 139 | + text: 'Hello' |
| 140 | + } |
| 141 | +}); |
| 142 | + |
| 143 | +// Find with pagination |
| 144 | +const page = await app.service('messages').find({ |
| 145 | + query: { |
| 146 | + $limit: 10, |
| 147 | + $skip: 20 |
| 148 | + } |
| 149 | +}); |
| 150 | +``` |
| 151 | + |
| 152 | +## What's Next? |
| 153 | + |
| 154 | +Now that you have a basic setup working, you can: |
| 155 | + |
| 156 | +- **Configure your service** - See [Configuration](./configuration.md) for all available options |
| 157 | +- **Learn about queries** - See [Querying](./querying.md) for Elasticsearch-specific query syntax |
| 158 | +- **Optimize performance** - See [Performance Features](./PERFORMANCE_FEATURES.md) |
| 159 | +- **Secure your service** - See [Security](./SECURITY.md) for security best practices |
| 160 | +- **Work with relationships** - See [Parent-Child Relationships](./parent-child.md) |
| 161 | + |
| 162 | +## Common First Steps |
| 163 | + |
| 164 | +### Enable Pagination |
| 165 | + |
| 166 | +```js |
| 167 | +app.use('/messages', service({ |
| 168 | + Model: esClient, |
| 169 | + elasticsearch: { index: 'test', type: 'messages' }, |
| 170 | + paginate: { |
| 171 | + default: 10, // Return 10 items by default |
| 172 | + max: 100 // Allow up to 100 items per request |
| 173 | + } |
| 174 | +})); |
| 175 | +``` |
| 176 | + |
| 177 | +### Add Security Limits |
| 178 | + |
| 179 | +```js |
| 180 | +app.use('/messages', service({ |
| 181 | + Model: esClient, |
| 182 | + elasticsearch: { index: 'test', type: 'messages' }, |
| 183 | + security: { |
| 184 | + maxQueryDepth: 50, |
| 185 | + maxBulkOperations: 10000, |
| 186 | + allowedRawMethods: [] // Disable raw() method for security |
| 187 | + } |
| 188 | +})); |
| 189 | +``` |
| 190 | + |
| 191 | +### Configure Refresh Strategy |
| 192 | + |
| 193 | +```js |
| 194 | +app.use('/messages', service({ |
| 195 | + Model: esClient, |
| 196 | + elasticsearch: { |
| 197 | + index: 'test', |
| 198 | + type: 'messages', |
| 199 | + refresh: false // Don't wait for refresh (better performance) |
| 200 | + // refresh: true // Wait for refresh (immediate visibility) |
| 201 | + // refresh: 'wait_for' // Wait for refresh to complete |
| 202 | + } |
| 203 | +})); |
| 204 | +``` |
| 205 | + |
| 206 | +See the [Configuration Guide](./configuration.md) for all available options and detailed explanations. |
0 commit comments