elasticsearch-helper
Advanced tools
Comparing version 1.6.1 to 1.7.0
@@ -93,2 +93,3 @@ 'use strict'; | ||
this.arrsFields = []; | ||
this.arroSorts = []; | ||
this.oBody = false; | ||
@@ -214,2 +215,3 @@ this.oDoc = false; | ||
oQuery.body._source = self.arrsFields; | ||
oQuery.body.sort = self.arroSorts; | ||
} | ||
@@ -339,2 +341,6 @@ | ||
}, | ||
sort: function(p_arroSorts){ | ||
this.arroSorts = p_arroSorts | ||
return this; | ||
}, | ||
id: function(p_sID) { | ||
@@ -341,0 +347,0 @@ this.sID = p_sID; |
{ | ||
"name": "elasticsearch-helper", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "A Nodejs module facilitating querying Elasticsearch clusters.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
194
README.md
# elasticsearch-helper [![npm version](https://badge.fury.io/js/elasticsearch-helper.svg)](https://badge.fury.io/js/elasticsearch-helper) [![NSP Status](https://nodesecurity.io/orgs/jacques-sirot/projects/60dd35a8-0efd-415e-9f72-2e7300f888ef/badge)](https://nodesecurity.io/orgs/jacques-sirot/projects/60dd35a8-0efd-415e-9f72-2e7300f888ef) | ||
# elasticsearch-helper [![npm version](https://badge.fury.io/js/elasticsearch-helper.svg)](https://badge.fury.io/js/elasticsearch-helper) | ||
@@ -10,12 +10,44 @@ A Nodejs module facilitating querying Elasticsearch clusters. | ||
# table of contents | ||
* [disclaimer](#disclaimer) | ||
* [installation](#installation) | ||
* [usage](#usage) | ||
* [Add client](#add-client) | ||
* [Use client](#use-client) | ||
* [Indexes](#indexes) | ||
* [copyTo](#copyto) | ||
* [deleteIndex](#deleteindex) | ||
* [exists](#exists) | ||
* [error handling](#error-handling) | ||
* [Documents](#documents) | ||
* [Single Document](#single-document) | ||
* [Retrieve](#retrieve) | ||
* [Delete](#delete) | ||
* [Create/Overwrite](#createoverwrite) | ||
* [Update](#update) | ||
* [Upsert](#upsert) | ||
* [Multiple Documents](#multiple-documents) | ||
* [Types & search options](#types--search-options) | ||
* [Filter types](#filter-types) | ||
* [Search types](#search-types) | ||
* [Retrieve](#retrieve-1) | ||
* [Delete](#delete-1) | ||
* [Count](#count) | ||
* [aggregations [BETA]](#aggregations-beta) | ||
* [Aggregation types](#aggregation-types) | ||
* [Other options](#other-options) | ||
* [Examples](#examples) | ||
* [Query](#query) | ||
* [Query with aggregation](#query-with-aggregation) | ||
# disclaimer | ||
I experienced a lot of issues in the past due to the way Elasticsearch handles the queries. I decided to create this helper which we currently use on production level at https://headhunterportal.com and some other projects and had helped us to drastically improve the readability of our code. | ||
After experiencing a lot of issues due to the way Elasticsearch handles the queries, I decided to create this helper currently used on production level that had helped us to drastically improve the readability and flexibility of our code. | ||
With this helper you will be able to query your elasticsearch clusters very easily. Everything is chainable and the query always returns a promise. | ||
NOTE: Even if we use this on production level, we still find bugs and add improvements to the module codebase. Feel free to fork it and modify it for your own needs. | ||
# installation | ||
@@ -27,18 +59,19 @@ | ||
## Add client | ||
```javascript | ||
let esH = require("elasticsearch-helper") | ||
const ES = require("elasticsearch-helper") | ||
// Will create a default client | ||
esH.addClient("127.0.0.1:9200"); | ||
ES.addClient("127.0.0.1:9200"); | ||
// Will create a client with name "client1" | ||
esH.addClient("client1","127.0.0.1:9200"); | ||
ES.addClient("client1","127.0.0.1:9200"); | ||
// Will create a client with name "client1" and will be used as default | ||
esH.addClient("client1","127.0.0.1:9200",true); | ||
ES.addClient("client1","127.0.0.1:9200",true); | ||
// Alias: | ||
esH.AddClient(...) | ||
ES.AddClient(...) | ||
``` | ||
@@ -54,12 +87,12 @@ | ||
// Querying on index "Index1" | ||
esH.query("Index1"); | ||
ES.query("Index1"); | ||
// Querying on all indexes starting with "Index" | ||
esH.query("Index*"); | ||
ES.query("Index*"); | ||
// Querying on index "Index1" and type "Type1" | ||
esH.query("Index1","Type1"); | ||
ES.query("Index1","Type1"); | ||
// Querying on index "Index1" and type "Type1" using the client "Client1" | ||
esH.query("Index1","Type1)".use("Client1") | ||
ES.query("Index1","Type1)".use("Client1") | ||
``` | ||
@@ -88,23 +121,23 @@ | ||
//Copy from index1 to index2 | ||
esH.query("Index1") | ||
.copyTo(esH.query("Index2")); | ||
ES.query("Index1") | ||
.copyTo(ES.query("Index2")); | ||
//Copy from index1 to index2 on client2 | ||
esH.query("Index1") | ||
.copyTo(esH.query("Index2").use("client2")); | ||
ES.query("Index1") | ||
.copyTo(ES.query("Index2").use("client2")); | ||
//Copy from index1, type1 to index2, type1 | ||
esH.query("Index1","Type1") | ||
.copyTo(esH.query("Index2")); | ||
ES.query("Index1","Type1") | ||
.copyTo(ES.query("Index2")); | ||
//Copy from index1, type1 to index2, type2 | ||
esH.query("Index1","Type1") | ||
.copyTo(esH.query("Index2","Type2")); | ||
ES.query("Index1","Type1") | ||
.copyTo(ES.query("Index2","Type2")); | ||
//Copy documents with first name is Josh from index1 to index2 | ||
esH.query("Index1") | ||
ES.query("Index1") | ||
.must( | ||
esH.type.term("first_name","Josh"), | ||
ES.type.term("first_name","Josh"), | ||
) | ||
.copyTo(esH.query("Index2")); | ||
.copyTo(ES.query("Index2")); | ||
``` | ||
@@ -123,7 +156,7 @@ | ||
//Delete index1 | ||
esH.query("Index1") | ||
ES.query("Index1") | ||
.deleteIndex(); | ||
//Delete index1 from client2 | ||
esH.query("Index1") | ||
ES.query("Index1") | ||
.use("client2") | ||
@@ -139,6 +172,6 @@ .deleteIndex(); | ||
esH.query("Index1") | ||
ES.query("Index1") | ||
.exists(); | ||
esH.query("Index1") | ||
ES.query("Index1") | ||
.use("client2") | ||
@@ -150,3 +183,3 @@ .exists(); | ||
A method can be created to handle errors (like logging or formating), This error method is part of a Promise and should return something if it needs to keep processing. | ||
A method can be created to handle errors (like logging or formatting), This error method is part of a Promise and should return something if it needs to keep processing. | ||
@@ -158,3 +191,3 @@ **Errors are always processed as Promise rejection** | ||
// Global error handling for all queries | ||
esH.onError(function(err){ | ||
ES.onError(function(err){ | ||
console.log("This message will appear after every error") | ||
@@ -165,3 +198,3 @@ return err; | ||
// Query specific error handling | ||
esH.query("Index1","Type1") | ||
ES.query("Index1","Type1") | ||
.onError(function(err){ | ||
@@ -183,3 +216,3 @@ //This onError will overwrite the global onError method for this query. | ||
// initialise query | ||
var q = esH.query("Index1","Type1"); | ||
var q = ES.query("Index1","Type1"); | ||
``` | ||
@@ -257,6 +290,6 @@ | ||
// Term type | ||
esH.type.term("fieldname","fieldvalue"), | ||
ES.type.term("fieldname","fieldvalue"), | ||
// Add a sub filter in the query | ||
esH.filter.should( | ||
esH.type.terms("fieldname2","fieldvalues") | ||
ES.filter.should( | ||
ES.type.terms("fieldname2","fieldvalues") | ||
) | ||
@@ -269,3 +302,3 @@ ) | ||
```javascript | ||
esH.filter.must(/* search types as arguments */); | ||
ES.filter.must(/* search types as arguments */); | ||
``` | ||
@@ -275,3 +308,3 @@ * must_not | ||
```javascript | ||
esH.filter.must_not(/* search types as arguments */); | ||
ES.filter.must_not(/* search types as arguments */); | ||
``` | ||
@@ -281,3 +314,3 @@ * should | ||
```javascript | ||
esH.filter.should(/* search types as arguments */); | ||
ES.filter.should(/* search types as arguments */); | ||
``` | ||
@@ -287,3 +320,3 @@ * filter | ||
```javascript | ||
esH.filter.filter(/* search types as arguments */); | ||
ES.filter.filter(/* search types as arguments */); | ||
``` | ||
@@ -298,5 +331,5 @@ | ||
```javascript | ||
esH.type.term("fieldkey","fieldvalue"); | ||
ES.type.term("fieldkey","fieldvalue"); | ||
// ex: | ||
esH.type.term("name.first_name","josh"); | ||
ES.type.term("name.first_name","josh"); | ||
``` | ||
@@ -306,5 +339,5 @@ * terms | ||
```javascript | ||
esH.type.terms("fieldkey","fieldvalues as array"); | ||
ES.type.terms("fieldkey","fieldvalues as array"); | ||
// ex: | ||
esH.type.terms("name.first_name",["josh","alan","jack"]); | ||
ES.type.terms("name.first_name",["josh","alan","jack"]); | ||
``` | ||
@@ -314,5 +347,5 @@ * exists | ||
```javascript | ||
esH.type.exists("fieldkey"); | ||
ES.type.exists("fieldkey"); | ||
// ex: | ||
esH.type.exists("name.first_name"); | ||
ES.type.exists("name.first_name"); | ||
``` | ||
@@ -322,5 +355,5 @@ * range | ||
```javascript | ||
esH.type.range("fieldkey","range object options"); | ||
ES.type.range("fieldkey","range object options"); | ||
// ex: | ||
esH.type.range("age",{ | ||
ES.type.range("age",{ | ||
gte: 10, | ||
@@ -334,5 +367,5 @@ lte: 30 | ||
```javascript | ||
esH.type.wildcard("fieldkey","fieldvalue"); | ||
ES.type.wildcard("fieldkey","fieldvalue"); | ||
// ex: | ||
esH.type.wildcard("name.first_name","josh*"); | ||
ES.type.wildcard("name.first_name","josh*"); | ||
``` | ||
@@ -343,5 +376,5 @@ | ||
```javascript | ||
esH.type.prefix("fieldkey","fieldvalue"); | ||
ES.type.prefix("fieldkey","fieldvalue"); | ||
// ex: | ||
esH.type.prefix("name.first_name","josh"); | ||
ES.type.prefix("name.first_name","josh"); | ||
``` | ||
@@ -358,7 +391,7 @@ | ||
```javascript | ||
esH.type.nested("parent","filter object"); | ||
ES.type.nested("parent","filter object"); | ||
// ex: | ||
esH.type.nested("name",esH.filter.must( | ||
esH.type.term("name.first", "josh"), | ||
esH.type.term("name.last", "wake") | ||
ES.type.nested("name",ES.filter.must( | ||
ES.type.term("name.first", "josh"), | ||
ES.type.term("name.last", "wake") | ||
)); | ||
@@ -505,14 +538,29 @@ ``` | ||
* size | ||
```javascript | ||
// will retrieve 1000 results maximum | ||
// all queries with a size over 500 will be converted into a scroll. | ||
q.size(1000) | ||
``` | ||
// will retrieve the documents values for specific keys | ||
* fields | ||
```javascript | ||
q.fields(["name","id"]) | ||
``` | ||
* type | ||
```javascript | ||
// will change/retrieve the type | ||
q.type("type1") | ||
``` | ||
* sorting | ||
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html) | ||
```javascript | ||
q.sort([{ "post_date" : {"order" : "asc"}}, ...]) | ||
``` | ||
@@ -525,15 +573,15 @@ | ||
```javascript | ||
let esH = require("elasticsearch-helper") | ||
const ES = require("elasticsearch-helper") | ||
esH.AddClient("client1","127.0.0.1:9200"); | ||
ES.AddClient("client1","127.0.0.1:9200"); | ||
esH.query("Index1","Type1") | ||
ES.query("Index1","Type1") | ||
.use("client1") | ||
.size(10) | ||
.must( | ||
esH.addType().term("name","John"), | ||
esH.addType().terms("lastname",["Smith","Wake"]) | ||
ES.addType().term("name","John"), | ||
ES.addType().terms("lastname",["Smith","Wake"]) | ||
) | ||
.must_not( | ||
esH.addType().range("age",{ | ||
ES.addType().range("age",{ | ||
lte:20, | ||
@@ -552,21 +600,21 @@ gte:30 | ||
```javascript | ||
let esH = require("elasticsearch-helper") | ||
const ES = require("elasticsearch-helper") | ||
esH.AddClient("client1","127.0.0.1:9200"); | ||
ES.AddClient("client1","127.0.0.1:9200"); | ||
esH.Query("user") | ||
ES.Query("user") | ||
.size(1001) // when an aggregation is set, size is set to 0. | ||
.must( | ||
esH.type.term("name","jacques"), | ||
esH.type.range("age",{gt:20,lte:40}), | ||
esH.filter.should( | ||
esH.type.term("color","blue"), | ||
esH.type.term("vehicle","car") | ||
ES.type.term("name","jacques"), | ||
ES.type.range("age",{gt:20,lte:40}), | ||
ES.filter.should( | ||
ES.type.term("color","blue"), | ||
ES.type.term("vehicle","car") | ||
) | ||
) | ||
.aggs( | ||
esH.agg.date_histogram("created_date")("date_created","1d") | ||
ES.agg.date_histogram("created_date")("date_created","1d") | ||
// Child aggregation to the "created_date" aggregation | ||
.aggs( | ||
esH.agg.terms("first_name")("data.first_name.raw") | ||
ES.agg.terms("first_name")("data.first_name.raw") | ||
) | ||
@@ -573,0 +621,0 @@ ) |
44151
1014
622