1 | ## Installation
|
2 |
|
3 | ```bash
|
4 | $ npm install geopipes-elasticsearch-backend
|
5 | ```
|
6 |
|
7 | [![NPM](https://nodei.co/npm/geopipes-elasticsearch-backend.png?downloads=true&stars=true)](https://nodei.co/npm/geopipes-elasticsearch-backend)
|
8 |
|
9 | Note: you will need `node` and `npm` installed first.
|
10 |
|
11 | The easiest way to install `node.js` is with [nave.sh](https://github.com/isaacs/nave) by executing `[sudo] ./nave.sh usemain stable`
|
12 |
|
13 | ## Interface
|
14 |
|
15 | ```javascript
|
16 | // Get a single record from elasticsearch
|
17 | Backend.prototype.get = function( String key, Object opts, Function cb )
|
18 |
|
19 | // Get a multiple records from elasticsearch
|
20 | Backend.prototype.mget = function( Array ids, Object opts, Function cb )
|
21 |
|
22 | // Index a new document in elasticsearch
|
23 | Backend.prototype.put = function( String key, Object val, Object opts, Function cb )
|
24 |
|
25 | // Perform an arbitrary search against elasticsearch
|
26 | Backend.prototype.search = function( Object query, Object opts, Function cb )
|
27 |
|
28 | // Create a bulk indexing stream which you can pipe index operations to
|
29 | Backend.prototype.createPullStream = function()
|
30 |
|
31 | // Find the nearest document to the supplied centroid
|
32 | Backend.prototype.reverseGeo = function( Object centroid, Object opts, Function cb )
|
33 |
|
34 | // Perform a fields only reverse geocode to retrieve the admin heirachy
|
35 | Backend.prototype.findAdminHeirachy = function( Object centroid, Object opts, Function cb )
|
36 | ```
|
37 |
|
38 | ## Basic Usage
|
39 |
|
40 | You will need a little knowledge of elasticsearch schemas to build more advanced indexers; however this example should be enough to get you started.
|
41 |
|
42 | ```javascript
|
43 | var esclient = require('pelias-esclient')();
|
44 | var Backend = require('geopipes-elasticsearch-backend');
|
45 |
|
46 | var elasticsearch = new Backend( esclient, 'example1', 'type1' );
|
47 |
|
48 | // Create a basic geo schema
|
49 | var schema = {
|
50 | mappings: {
|
51 | type1: {
|
52 | properties: {
|
53 | name: { type : 'string' },
|
54 | center_point: { type: 'geo_point', lat_lon: true }
|
55 | }
|
56 | }
|
57 | }
|
58 | }
|
59 |
|
60 | // Create the schema
|
61 | esclient.indices.create( { index: 'example1', body: schema }, function( err, res ){
|
62 |
|
63 | var opts = null;
|
64 | var centroid = {
|
65 | 'lat': 50.1,
|
66 | 'lon': 100.45
|
67 | };
|
68 | var doc = {
|
69 | 'name': 'My POI',
|
70 | 'center_point': centroid
|
71 | };
|
72 |
|
73 | elasticsearch.put( 'myid', doc, opts, function( err, res ){
|
74 | console.log( 'put', err, res );
|
75 | elasticsearch.reverseGeo( centroid, opts, function( err, res ){
|
76 | console.log( 'reverse geosearch', err, res );
|
77 | });
|
78 | });
|
79 |
|
80 | });
|
81 | ```
|
82 |
|
83 | You can view the indexed document here: [http://localhost:9200/example1/type1/myid](http://localhost:9200/example1/type1/myid)
|
84 |
|
85 | ## Streaming Indexing
|
86 |
|
87 | Note: the streaming library flushes in batches so you may need to wait
|
88 | a few seconds for the batch to be flushed.
|
89 |
|
90 | ```javascript
|
91 | var esclient = require('pelias-esclient')();
|
92 | var Backend = require('geopipes-elasticsearch-backend');
|
93 |
|
94 | var elasticsearch = new Backend( esclient, 'example2', 'type1' );
|
95 | var stream = elasticsearch.createPullStream();
|
96 |
|
97 | stream.write({
|
98 | 'id': 'myid',
|
99 | 'name': 'My POI',
|
100 | 'center_point': {
|
101 | 'lat': 50.1,
|
102 | 'lon': 100.45
|
103 | }
|
104 | });
|
105 | ```
|
106 |
|
107 | You can view the indexed document here: [http://localhost:9200/example2/type1/myid](http://localhost:9200/example2/type1/myid)
|
108 |
|
109 | ## NPM Module
|
110 |
|
111 | The `geopipes-elasticsearch-backend` npm module can be found here:
|
112 |
|
113 | [https://npmjs.org/package/geopipes-elasticsearch-backend](https://npmjs.org/package/geopipes-elasticsearch-backend)
|
114 |
|
115 | ## Contributing
|
116 |
|
117 | Please fork and pull request against upstream master on a feature branch.
|
118 |
|
119 | Pretty please; provide unit tests and script fixtures in the `test` and `test/fixtures` directories.
|
120 |
|
121 | ### Running Unit Tests
|
122 |
|
123 | ```bash
|
124 | $ npm test
|
125 | ```
|
126 |
|
127 | ### Continuous Integration
|
128 |
|
129 | Travis tests every release against node version `0.10`
|
130 |
|
131 | [![Build Status](https://travis-ci.org/geopipes/elasticsearch-backend.png?branch=master)](https://travis-ci.org/geopipes/elasticsearch-backend) |
\ | No newline at end of file |