elastic search

Elastic search, yay. Install elastic search with brew, yay. Start with elasticsearch, yay.

One of the critical steps is loading data into your cluster. Kinda like a boss…

'use strict';

const csv = require('csvtojson');
const ELASTICSEARCH = require('elasticsearch');
const Episodes = `${process.env.PWD}/dataset/simpsons_episodes.csv`;
const ESCLUSTER = 'http://localhost:9200';
const INDEX = 'simpsons';
const TYPE = 'episode';
const BULK = [];
const CLIENT = new ELASTICSEARCH.Client({
    host: ESCLUSTER,
    apiVersion: '5.0'
});

console.log('Bulk import into Elasticsearch');
csv()
    .fromFile(Episodes)
    .on('json',(obj) => {
        BULK.push(
            {index: {_index: INDEX, _type: TYPE, _id: obj.id } },
            obj
        );
        console.log(`Adding ${obj.id} to array`);
    })
    .on('end',() => {
        CLIENT.bulk({
            body: BULK
        }, (err) => {
            if (err) {
                console.log(err);
            } 
        })
        console.log('Processing complete');


curl -s http://localhost:9200/simpsons/episode/250/_source\?_source_exclude\=video_url,views | jq .

Can exclude stuff.

curl -s http://localhost:9200/simpsons/episode/250/_source\?_source_include\=video_url,views | jq .

Can include params.

So you hate curl, do a good ole npm install elasticsearch --save, then make a client.js file with the following contents…

const ELASTICSEARCH = require('elasticsearch');
const CLIENT = new ELASTICSEARCH.Client({
  host: 'localhost:9200',
  apiVersion: '5.0'
});

CLIENT.get({
  index: 'simpsons',
  type: 'episode',
  id: 1,
  _sourceExclude: [
    'video_url'
  ]
}, function(err, resp) {
  if(err) {
    console.log(err);
  } else {
    console.log(resp);
  }
})

From here, you can run node client.js and it will run like a charmer.

Let’s say you fucking HATE callbacks and want promises…

Post Content