Skip to content

Commit

Permalink
Adds usage in README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
bompi88 committed Apr 26, 2017
1 parent ee2347f commit fec9bc5
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion packages/easysearch:elasticsearch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The configuration options that can be passed to `EasSearch.ElasticSearch` as an
* __getElasticSearchDoc(doc, fields)__: Function that returns the document to index, fieldsToIndex by default
* __body(body)__: Function that returns the ElasticSearch body to send when searching

## Mapping, Analyzers and so on
## Mapping

To make changes to the mapping you can use the mapping setting which will set the mapping when creating a new index.

Expand All @@ -53,6 +53,76 @@ const PlayersIndex = new Index({
})
```

## Aggregations
To define aggregations, inject them inside a `body` helper function like in the example below:

```javascript
var index = new EasySearch.Index({
...
engine: new EasySearch.ElasticSearch({
body: function(body, opts) {
body.aggs = {
tags:{
filter: {},
aggs: {
tags: {
terms: { field: 'tags.raw' }
}
}
}
};
return body;
}
...
}
});
```
The aggregations will be available on the cursor returned by the `search` method:
```javascript
var cursor = index.search('test');

// get all aggregations
cursor.getAggregations();

// get aggregation by name
cursor.getAggregation('tags');
```
Example filter component populated by ES aggregations:
__filter.html:__
```html
<template name="filter">
<select class="{{class}}">
<option value="">Choose a tag</option>
{{ #each tags }}
<option value="{{key}}">{{key}}</option>
{{ else }}
<option value="" disabled>No tags available</option>
{{/each}}
</select>
</template>
```
__filter.js:__
```javascript
import './filter.html';

import { DocumentIndex } from '../path/to/document_index.js';

Template.filter.helpers({

tags() {
const agg = DocumentIndex.getComponentMethods().getCursor().getAggregation('tags');
return agg ? agg.tags.buckets : null;
}

});
```
## How to run ElasticSearch
```sh
Expand Down

0 comments on commit fec9bc5

Please sign in to comment.