Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

esq

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

esq - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

.npmignore

13

examples/filtered.js

@@ -0,11 +1,8 @@

//http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html
var ESQ = require('../lib/esq');
var esq = new ESQ();
var query = null;
query = esq.bool('filtered', 'query', 'bool', 'must', esq.match('foo', 'bar'));
query = esq.bool('filtered', 'query', 'bool', 'must', esq.match('date', '2014-02-01'));
query = esq.bool('filtered', 'query', 'bool', 'should', esq.range('time', '12:00', '13:00'));
query = esq.bool('filtered', 'query', 'bool', 'minimum_should_match', 1);
console.log(JSON.stringify(query, null, 2));
esq.query('filtered', 'query', 'term', { tag: 'wow' });
esq.query('filtered', 'filter', 'range', { from: 10, to: 20 });
console.log(JSON.stringify(esq.getQuery(), null, 2));

@@ -1,3 +0,1 @@

var utils = require('./utils');
function ESQ() {

@@ -20,60 +18,2 @@ this._query = { };

ESQ.prototype.bool = function() {
var args = this._getArgsAsArray(arguments);
var value = args[(args.length-1)];
if (((value instanceof Object) && !(value instanceof Array)) || (typeof value == 'number')) {
value = args.pop();
} else {
return { };
}
var operator = args[(args.length-1)];
if (operator == 'must' || operator == 'must_not' || operator == 'should') {
if (!(value instanceof Array)) value = [value];
}
this._createNestedObject(this._query, args, value);
return this._query;
};
ESQ.prototype.match = function(key, value) {
if (!(key || value)) return null;
var match = { match: { } };
match.match[key] = value || null;
return match;
};
ESQ.prototype.term = function(key, value) {
if (!(key || value)) return null;
var term = { term: { } };
term.term[key] = value || null;
return term;
};
ESQ.prototype.range = function(key, gte, lte) {
if (!(key || gte || lte)) return null;
var range = {
range: {}
};
range.range[''+key] = { };
if (gte) range.range[''+key].gte = gte || null;
if (lte) range.range[''+key].lte = lte || null;
return range;
};
ESQ.prototype.wildcard = function(key, value) {
if (!(key || value)) return null;
var wildcard = {
wildcard: { }
};
wildcard.wildcard[''+key] = { };
wildcard.wildcard[''+key].value = value ? (''+value).toLowerCase() : null;
return wildcard;
};
ESQ.prototype._createNestedObject = function(base, names, value) {

@@ -87,6 +27,8 @@ var lastName = arguments.length === 3 ? names.pop() : false;

if (lastName) {
if (lastName instanceof Array) value = [value];
if (value instanceof Array) {
base[lastName] = base[lastName] ? base[lastName].concat(value) : value;
} else if (value instanceof Object) {
base[lastName] ? utils.extend(base[lastName], value) : base[lastName] = value;
base[lastName] ? this._extend(base[lastName], value) : base[lastName] = value;
}

@@ -105,1 +47,13 @@ base = base[lastName] = base[lastName] || value || { };

};
ESQ.prototype._extend = function(obj) {
var args = Array.prototype.slice.call(arguments, 1);
args.forEach(function(source) {
if (source) {
for (var prop in source) {
obj[prop] = source[prop];
}
}
});
return obj;
};
{
"name": "esq",
"version": "0.0.1",
"version": "0.0.2",
"description": "Easily build elasticsearch queries",
"keywords": [
"elasticsearch",
"es",
"query",

@@ -18,3 +17,3 @@ "querying"

"scripts": {
"test": "mocha test/tests.js"
"test": "mocha"
},

@@ -21,0 +20,0 @@ "author": "Rahul Patel <rahul.patel@holidayextras.com>",

# ESQ
ESQ is a helper module for elasticsearch. It aims to provide an easy way of creating elasticsearch queries.
ESQ is a helper module for elasticsearch. It aims to provide an easy way of creating elasticsearch queries. Run `node examples` to see all of the examples from the [elasticsearch docs](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-queries.html) generated using esq!

@@ -9,33 +9,46 @@ ## Quick Examples

var ESQ = require('esq');
var esq = new ESQ();
var query = null;
query = esq.bool('filtered', 'query', 'bool', 'must', esq.match('foo', 'bar'));
query = esq.bool('filtered', 'query', 'bool', 'must', esq.match('date', '2014-02-01'));
query = esq.bool('filtered', 'query', 'bool', 'should', esq.range('time', '12:00', '13:00'));
query = esq.bool('filtered', 'query', 'bool', 'minimum_should_match', 1);
esq.query('bool', ['must'], { match: { user: 'kimchy' } });
esq.query('bool', ['must_not'], { range: { age: { from: 10, to: 20 } } });
esq.query('bool', ['should'], { term: { tag: 'wow' } });
esq.query('bool', ['should'], { term: { tag: 'elasticsearch' } });
esq.query('bool', 'minimum_should_match', 1);
esq.query('bool', 'boost', 1.0);
// query == {
// "filtered": {
// "query": {
// "bool": {
// "must": {
// "match": {
// "date": "2014-02-01"
// }
// },
// "should": {
// "range": {
// "time": {
// "gte": "12:00",
// "lte": "13:00"
// }
// }
// },
// "minimum_should_match": 1
// }
// }
// }
//}
// "bool": {
// "must": [
// {
// "match": {
// "user": "kimchy"
// }
// }
// ],
// "must_not": [
// {
// "range": {
// "age": {
// "from": 10,
// "to": 20
// }
// }
// }
// ],
// "should": [
// {
// "term": {
// "tag": "wow"
// }
// },
// {
// "term": {
// "tag": "elasticsearch"
// }
// }
// ],
// "minimum_should_match": 1,
// "boost": 1
// }
// }
```

@@ -45,8 +58,7 @@ ---

var ESQ = require('esq');
var esq = new ESQ();
esq.query('query', esq.match('foo', 'bar'));
esq.query('query', esq.range('x', '1', '5'));
esq.query('query', esq.wildcard('test', 'what'));
esq.query('query', 'match', 'foo', 'bar');
esq.query('query', 'range', 'x', { gte: 1, lte: 5 });
esq.query('query', 'wildcard', 'test', 'value', 'what*');

@@ -56,19 +68,19 @@ var query = esq.getQuery();

// query == {
// "query": {
// "match": {
// "foo": "bar"
// },
// "range": {
// "x": {
// "gte": "1",
// "lte": "5"
// }
// },
// "wildcard": {
// "test": {
// "value": "what"
// }
// }
// }
//}
// "query": {
// "match": {
// "foo": "bar"
// },
// "range": {
// "x": {
// "gte": "1",
// "lte": "5"
// }
// },
// "wildcard": {
// "test": {
// "value": "what*"
// }
// }
// }
// }
```

@@ -88,11 +100,30 @@

### esq.getQuery();
This will return the query at the current stage.
## Queries
__Example__
```javascript
esq.query('query', 'match', 'foo', 'bar');
var query = esq.getQuery();
```
__Generates__
```javascript
{
query: {
match: {
foo: 'bar'
}
}
}
```
---
### esq.query(str, ..., str, value);
You can pass the function as many strings as you want and you'll receive a nested object with the arguments as keys. The final value (query component) will be assigned to the final object.
You can pass this function as many strings as you want and you'll receive a nested object with the arguments as keys. The final argument should always be the value you want to assign to the second to last argument. This function will always return the query at its current state.
__Example__
```
esq.query('filtered', 'query', esq.match('foo', 'bar'));
esq.query('filtered', 'query', 'match', 'foo', 'bar');
```

@@ -113,10 +144,8 @@

---
### esq.bool(str, ..., str, operator, value);
Same as above, however the second to last parameter must be one of the following operators: must, must_not, should or minimum_should_match. This will ensure that the final value is assigned correctly or pushed into an array.
The function also allows you to pass in an argument as an array (this array should only be one element). This tells the function that you want that key to be an array and so it'll push the following arguments into the array.
__Example__
```javascript
esq.bool('filtered', 'filter', 'bool', 'must', esq.term('foo', 'bar'));
```
esq.query('filtered', 'query', 'bool', ['must'], 'match', 'foo', 'bar');
```

@@ -127,7 +156,9 @@ __Generates__

filtered: {
filter: {
query: {
bool: {
must: [
{
term: { foo: 'bar' }
match: {
foo: 'bar'
}
}

@@ -140,79 +171,1 @@ ]

```
## Query Components
### esq.match(key, value);
Build a match query component using the key and value provided.
__Example__
```javascript
esq.match('foo', 'bar');
```
__Generates__
```javascript
{
match: {
foo: 'bar'
}
}
```
---
### esq.term(key, value);
Build a term query component using the key and value provided.
__Example__
```javascript
esq.term('foo', 'bar');
```
__Generates__
```javascript
{
term: {
foo: 'bar'
}
}
```
---
### esq.range(key, gte, lte);
Build a range query using the key, grater than equal and/or less than equal provided.
__Example__
```javascript
esq.range('foo', 1, 5);
```
__Generates__
```javascript
{
range: {
foo: {
gte: 1,
lte: 5
}
}
}
```
---
### esq.wildcard(key, value);
Build a wildcard query component using the key and value provided.
__Example__
```javascript
esq.wildcard('foo', 'bar*');
```
__Generates__
```javascript
{
wildcard: {
foo: {
value: 'bar*'
}
}
}
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc