Repository: StreetHub/elasto Branch: master Commit: 2f0e680e024f Files: 11 Total size: 36.8 KB Directory structure: gitextract_it2bibih/ ├── .gitignore ├── .jshintrc ├── CHANGELOG.md ├── LICENSE.txt ├── README.md ├── circle.yml ├── index.js ├── lib/ │ └── elasto.js ├── package.json └── test/ ├── elasto.test.js └── mocha.opts ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Logs logs *.log # Runtime data pids *.pid *.seed # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directory # Deployed apps should consider commenting this line out: # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git node_modules ================================================ FILE: .jshintrc ================================================ { // JSHint Default Configuration File (as on JSHint website) // See http://jshint.com/docs/ for more details "maxerr" : 50, // {int} Maximum error before stopping // Enforcing "bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.) "camelcase" : false, // true: Identifiers must be in camelCase "curly" : false, // true: Require {} for every new block or scope "eqeqeq" : true, // true: Require triple equals (===) for comparison "forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty() "freeze" : true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc. "immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());` "indent" : 4, // {int} Number of spaces to use for indentation "latedef" : false, // true: Require variables/functions to be defined before being used "newcap" : false, // true: Require capitalization of all constructor functions e.g. `new F()` "noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee` "noempty" : true, // true: Prohibit use of empty blocks "nonbsp" : true, // true: Prohibit "non-breaking whitespace" characters. "nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment) "plusplus" : false, // true: Prohibit use of `++` & `--` "quotmark" : false, // Quotation mark consistency: // false : do nothing (default) // true : ensure whatever is used is consistent // "single" : require single quotes // "double" : require double quotes "undef" : true, // true: Require all non-global variables to be declared (prevents global leaks) "unused" : true, // Unused variables: // true : all variables, last function parameter // "vars" : all variables only // "strict" : all variables, all function parameters "strict" : false, // true: Requires all functions run in ES5 Strict Mode "maxparams" : false, // {int} Max number of formal params allowed per function "maxdepth" : false, // {int} Max depth of nested blocks (within functions) "maxstatements" : false, // {int} Max number statements per function "maxcomplexity" : false, // {int} Max cyclomatic complexity per function "maxlen" : false, // {int} Max number of characters per line // Relaxing "asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons) "boss" : false, // true: Tolerate assignments where comparisons would be expected "debug" : false, // true: Allow debugger statements e.g. browser breakpoints. "eqnull" : false, // true: Tolerate use of `== null` "es5" : false, // true: Allow ES5 syntax (ex: getters and setters) "esnext" : true, // true: Allow ES.next (ES6) syntax (ex: `const`) "moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features) // (ex: `for each`, multiple try/catch, function expression…) "evil" : false, // true: Tolerate use of `eval` and `new Function()` "expr" : false, // true: Tolerate `ExpressionStatement` as Programs "funcscope" : false, // true: Tolerate defining variables inside control statements "globalstrict" : false, // true: Allow global "use strict" (also enables 'strict') "iterator" : false, // true: Tolerate using the `__iterator__` property "lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block "laxbreak" : false, // true: Tolerate possibly unsafe line breakings "laxcomma" : false, // true: Tolerate comma-first style coding "loopfunc" : false, // true: Tolerate functions being defined in loops "multistr" : false, // true: Tolerate multi-line strings "noyield" : false, // true: Tolerate generator functions with no yield statement in them. "notypeof" : false, // true: Tolerate invalid typeof operator values "proto" : false, // true: Tolerate using the `__proto__` property "scripturl" : false, // true: Tolerate script-targeted URLs "shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;` "sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation "supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;` "validthis" : false, // true: Tolerate using this in a non-constructor function // Environments "node" : true, // Node.js // Custom Globals "globals" : { "it": true, "before": true, "describe": true } // additional predefined global variables } ================================================ FILE: CHANGELOG.md ================================================ ## 3.0.0 (2015-06-11) - Upgrade elasticsearch.js to 5.0.0 ## 2.1.0 (2015-05-27) - Included `.jshintrc` file - Fixed minor semicolons issues - Added `.raw()` method for debugging/editing purposes ## 2.0.0 (2015-04-15) - Fix fields query (https://github.com/elastic/elasticsearch/issues/4888) - Dropped support for Elasticsearch 0.90 because of the fields fix ## 1.1.2 (2015-04-14) - Fix: Count query should not be allowed to have a sort param ## 1.1.1 (2015-04-07) - Added search keywords in package.json ## 1.1.0 (2015-04-07) - Fix fields filtering with only arguments ## 1.0.0 (2015-04-07) - Updated elasticsearch.js to latest version ## 0.2.0 (2015-04-07) - Replaced isarray by npm module - Added count method for a query ## 0.1.2 (2015-01-29) Fixes: - Remove `isPlainObject` helper ## 0.1.1 (2015-01-29) Fixes: - Removed unused dependencies - Put dependencies for testing in devDependencies - Replaced lodash methods with vanilla JS ================================================ FILE: LICENSE.txt ================================================ Copyright (c) 2014 StreetHub MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Elasto [![Circle CI](https://circleci.com/gh/StreetHub/elasto.svg?style=svg)](https://circleci.com/gh/StreetHub/elasto) :warning: This project is currently unmaintained ## Introduction Elasto is a simple library to query Elasticsearch. ## Topics - [Installation](#installation) - [Support](#support) - [Getting started](#getting-started) - [Example](#example) - [API](#api) - [License](#license) - [Contributors](#contributors) ## Installation ```bash npm install elasto ``` ## Support If you want to use this package with Elasticsearch 0.90, you should use `1.1.2`. The versions `>=2.X.X` will only support Elasticsearch `1.X.X`. ## Getting started More infos about the config options [here](http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/configuration.html). ```javascript var Elasto = require('elasto'); Elasto.config({ host: 'localhost:9200', }); ``` Elasto provides a simple query interface for the common usecases. You can have access to the [elasticsearch.js](http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/index.html) client via `Elasto.client`. The client gets instantiated when you set the config with a host. ## Example All-in-one example. Find more options on [API](#api). ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .near({ // documents near this location lat: 51.5, lon: -0.1467912, radius: 3 }) .where('name', 'London') // where name matches London .size(2) // return only 2 documents .from(1) // skip 1 document (searching after 1 document) .fields('name', 'address') // return only name and address fields .exec() .then(function (res) { // execute // done! }); ``` ## API ### Basic query ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .exec(); ``` ### Fields matching - `.where` `.where` accepts two types of arguments. Either an object with the fields to match. ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .where({ username: '@jack'}) .exec(); ``` Or key value pair of arguments ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .where('username', '@jack') .exec(); ``` ### Term - `.term` Search a term. ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .term('#love') .exec(); ``` ### Size - `.size` - `.limit` Limit the size of the query. ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .limit(3) .exec(); ``` ### Sort - `.sort` - `.limit` Sorts the query by a field the size of the query. ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .sort('description', 'asc') .exec(); ``` You can also sort by distance. It will sort based on the `location` field in the document. ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .sort('distance', { lat: 51.5, lon: -0.1467912, }) .exec(); ``` ### Distance - `.near` Finds documents in an area. The radius is in miles. ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .near({ lat: 51.5, lon: -0.1467912, radius: 2 }) .exec(); ``` ### From - `.from` - `.offset` Skips documents in the query. ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .from(3) .exec(); ``` ### Range - `.range` Find documents where the field matches a range. ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .range('characters', [120, 150]) .exec(); ``` You can also query the distance range. It will sort based on the `location` field in the document. All the distances are in miles. ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .range('distance', { lat: 51.5, lon: -0.1467912, from: 2, to: 3 }) .exec(); ``` ### Fields - `.fields` Only return the specific fields. ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .fields(['name', 'id']) .exec(); ``` ### Exclude - `.exclude` - `.not` Excludes documents where the query gets matched (opposite of `.where`). ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .not('username', '@hater666') .exec(); ``` ### Count Count documents based on a query ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .count(); ``` ### Raw ElasticSearch Query Returns the raw ElasticSearch computed by Elasto. You can directly use that object with the ElasticSearch node library (that's how Elasto is designed). Takes `search` or `count` as argument. If empty, the raw query will be `search`. ```javascript Elasto.query({ index: 'development', type: 'tweets' }) .raw(); // Returns Object // -> { index: 'development', // type: 'tweets', // body: { query: { filtered: [Object] } } } ``` ### License `elasto` is released under the MIT license. See `LICENSE.txt` for the complete text. ### Contributors * [Arnaud Benard](https://github.com/arnaudbenard) * [Vadim Demedes](https://github.com/vdemedes) * [Peter Kadlot](https://github.com/daralthus) * [Alex Loizou](https://github.com/alexloi) ================================================ FILE: circle.yml ================================================ machine: node: version: v0.10.26 dependencies: cache_directories: - elasticsearch-1.5.0 # relative to the build directory post: - if [[ ! -e elasticsearch-1.5.0 ]]; then wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.5.0.tar.gz && tar -xvf elasticsearch-1.5.0.tar.gz; fi - elasticsearch-1.5.0/bin/elasticsearch: {background: true} ================================================ FILE: index.js ================================================ module.exports = require('./lib/elasto'); ================================================ FILE: lib/elasto.js ================================================ var elasticsearch = require('elasticsearch'); var isArray = require('isarray'); var Elasto = { config: function(opts) { var self = this; Object.keys(opts).forEach(function(key) { if(key === 'host') self._setupClient(opts); }); }, _setupClient: function(config) { this.client = new elasticsearch.Client(config); }, query: function(opts) { return new Query(this.client, opts.index, opts.type); } }; /** * Query DSL API */ var Query = function(client, index, type) { this.options = { fields: [], sort: [], facets: {}, location: {}, must: [], must_not: [], should: [], }; /** * Set conditions for matching documents * @param {string} key - Key or field name * @param {value} value - Value for a field * @returns Query * @example * query.where('name', 'London') * query.where({ * name: 'London', * address: 'Baker Street' * }); * query.where([{ * name: 'London' * }, * { * name: 'Budapest' * }]); */ this.where = function (key, value) { if (Array.isArray(key)) { // basically an AND query var arr = key; arr.forEach(this.where.bind(this)); return this; } else if(typeof key === 'object') { var obj = key; for (var k in obj) { if (Object.prototype.hasOwnProperty.call(obj, k)){ this.where(k, obj[k]); } } return this; } var condition = {}; if (isArray(value)) { // basically an OR query condition.terms = {}; condition.terms[key] = value; } else { condition.term = {}; condition.term[key] = value; } this.options.must.push(condition); return this; }; /** * Limit the number of documents in response * @param {number} size - number of documents to return * @returns Query * @example * query.size(5); */ this.size = function (size) { this.options.size = size; return this; }; this.limit = this.size; /** * Skip N documents * @param {number} from - number of documents to skip * @returns Query * @example * query.from(10); */ this.from = function (from) { this.options.from = from; return this; }; this.offset = this.from; /** * Apply sorting * @param {string} key - field name * @param {string} opts - opts (asc/desc or location) * @returns Query * @example * query.sort('name', 'desc'); * query.sort('_score'); * query.sort('location', { * lat: 0.1, * lon: 10 * }); */ this.sort = function (key, opts) { if(key === 'distance'){ var location = this.options.location; // take from sort or near var lat = opts && opts.lat ? opts.lat: location.lat; var lon = opts && opts.lon ? opts.lon: location.lon; this.sort('_geo_distance', { location: { lat: lat, lon: lon }, order: 'asc', unit: 'miles' }); } else { if (key && opts) { var obj = {}; obj[key] = opts; this.options.sort.push(obj); } else { this.options.sort.push(key); } } return this; }; /** * Return only specified fields * @param {array} keys - array of keys/fields * @returns Query * @example * query.fields(['name', 'address']); * query.fields('name', 'address'); */ this.fields = function (keys) { var fields = isArray(keys) ? keys : Array.prototype.slice.call(arguments); this.options.fields = fields; return this; }; /** * Search in a range - range is a filter * @return Query * @example * query.range('price', [0, 10]); */ this.range = function(key, interval) { if (key && isArray(interval)) { var range = {}; range.range = {}; range.range[key] = { gte : interval[0], lte : interval[1], }; this.options.must.push(range); } else if (key && key === 'distance' && typeof interval === 'object') { this.options.must.push({ geo_distance_range: { from : interval.from + 'mi', to : interval.to + 'mi', location: { lat: interval.lat, lon: interval.lon } } }); } return this; }; /** * Exclude all the documents containing this field * @return Query * @example * query.not('areas._id', '5441152714aa8e110a000a38'); * query.exclude('price', 0); */ this.not = function(field, value) { var filter = { term: {}}; filter.term[field] = value; this.options.must_not.push(filter); return this; }; this.exclude = this.not; /** * Specify location properties for a query * @param {object} location - Object with lat, lon, radius properties * @returns Query * @example * query.near({ * lat: 41, * lon: 13, * radius: 3 * }); */ this.near = function (location) { var radius = location.radius; var lat = parseFloat(location.lat); var lon = parseFloat(location.lon); // Save to use in sorting later this.options.location = { lat: lat, lon: lon }; this.options.must.push({ geo_distance: { distance: radius + 'mi', distance_type: 'arc', location: { lat: lat, lon: lon } } }); return this; }; /** * Search by term */ this.term = function(value) { this.options.term = value; return this; }; this._buildQuery = function(opts) { var query = { index: index, type: type }; var body = { query: { filtered: { query: { match_all: {} } } } }; var options = this.options; var bool = {}; if (options.must.length) { bool.must = options.must; } if (options.must_not.length) { bool.must_not = options.must_not; } if (options.should.length) { bool.should = options.should; } if (Object.keys(bool).length) { body.query.filtered = { filter: { bool: bool } }; } if (options.term) { body.query.filtered.query = { query_string: { query: options.term } }; } if (Object.keys(options.facets).length) query.facets = options.facets; if (options.fields.length) query._source = options.fields; if (options.sort.length && opts.output === 'search') body.sort = options.sort; if (options.size) query.size = options.size; if (options.from) query.from = options.from; if (options.highlight) query.highlight = options.highlight; query.body = body; return query; }; /** * Perform a search * @returns Promise * @example * query.exec().then(function (documents) { * * }); */ this.exec = function() { var query = this._buildQuery({output: 'search'}); return client.search(query); }; /** * Get the number of documents matching the query * @return Promise * @example * query.count().then(function(res) { * * }); */ this.count = function() { var query = this._buildQuery({output: 'count'}); return client.count(query); }; /** * Returns the raw query that Elasticsearch will execute * Useful methood for debugging or modify raw ElasticSearch queries * @param {String} type Type of queries: search, count (search by default) * @return {Object} ElasticSearch Query */ this.raw = function(type) { return this._buildQuery({output: type || 'search'}); }; }; module.exports = Elasto; ================================================ FILE: package.json ================================================ { "name": "elasto", "version": "3.1.0", "description": "ElasticSearch client", "main": "index", "scripts": { "test": "./node_modules/.bin/mocha -R spec test" }, "keywords": [ "elasticsearch", "elatic", "database", "query", "search" ], "dependencies": { "isarray": "0.0.1", "elasticsearch": "^5.0.0" }, "devDependencies": { "bluebird": "^2.9.1", "chai": "^1.9.2", "chai-as-promised": "^4.1.1", "chance": "^0.5.8", "lodash": "^2.4.1", "mocha": "^1.20.1", "should": "^4.0.1" }, "license": "MIT" } ================================================ FILE: test/elasto.test.js ================================================ var chai = require('chai'); chai.should(); chai.use(require('chai-as-promised')); var _ = require('lodash'); var Bluebird = require('bluebird'); var chance = require('chance')(); var Elasto = require('../'); Elasto.config({ host: 'localhost:9200', // log: 'trace', apiVersion: '1.5' }); var CIRCLECI = process.env.CIRCLECI === "true"; console.log('Executing test on', CIRCLECI ? 'CircleCI' : 'local'); describe('Elasto', function() { var london = { lat: 51.5, lon: -0.1467912, }; before(function cleanTestingIndex(done) { this.timeout(60000); var mapping = { 'properties': { 'location' : { 'type' : 'geo_point' }, 'embeds': { 'properties': { 'url': { 'type': 'string', "index": "not_analyzed" } } } } }; // Circle ci takes a few seconds to start the elasticsearch service Bluebird.resolve().delay(CIRCLECI ? 10000 : 0) .then(function() { return Elasto.client.indices.exists({ index: 'circle_test'}); }) .then(function(exists) { var deleteIndex = Elasto.client.indices.delete({ index: 'circle_test'}); return exists ? deleteIndex : ''; }) .delay(1000) // small delay to set mapping .then(function(){ return Elasto.client.indices.create({ index: 'circle_test' }); }) .then(function(){ return Elasto.client.indices.putMapping({ index: 'circle_test', type: 'tweets', body: { tweets: mapping } }); }) .delay(1000) // small delay to set mapping .should.eventually.notify(done); }); describe('query DSL', function() { var doc = function() { var lat = london.lat * (1 + chance.integer({min: -10, max: 10})/1000); var lon = london.lon * (1 + chance.integer({min: -10, max: 10})/1000); return { id: chance.natural({ min: 100000, max: 200000 }), name: chance.word(), characters: chance.integer({min: 1, max: 150}), location: { lat: lat, lon: lon}, description: 'twitty tweet' //leave static }; }; before(function(done) { var promises = []; _.times(30, function() { var body = doc(); promises.push(Elasto.client.create({ index: 'circle_test', type: 'tweets', refresh: true, id: body.id, body: body })); }); Bluebird.all(promises) .should.eventually.notify(done); }); it('should find a document with a where query', function (done) { var source = doc(); Elasto.client.create({ index: 'circle_test', type: 'tweets', refresh: true, id: source.id, body: source }) .then(function() { return Elasto.query({ index: 'circle_test', type: 'tweets' }) .where({ name: source.name }) .exec(); }) .then(function(res){ var data = res.hits.hits[0]._source; source.id.should.equal(data.id); source.name.should.equal(data.name); }) .should.eventually.notify(done); }); it('should find a document with the alternative where query syntax ("key", "val")', function (done) { var source = doc(); Elasto.client.create({ index: 'circle_test', type: 'tweets', refresh: true, id: source.id, body: source }) .then(function() { return Elasto.query({ index: 'circle_test', type: 'tweets' }) .where('name', source.name) .exec(); }) .then(function(res){ var data = res.hits.hits[0]._source; source.id.should.equal(data.id); source.name.should.equal(data.name); }) .should.eventually.notify(done); }); it('should find a document with the alternative where query syntax ([{"key": "val"}])', function (done) { var source = doc(); source.embeds = [ {url: 'https://media1.giphy.com/media/xig1JXCmS9xpm/200.gif'}, {url: 'https://media1.giphy.com/media/PxFGykZD9QnCw/200.gif'} ]; Elasto.client.create({ index: 'circle_test', type: 'tweets', refresh: true, id: source.id, body: source }) .then(function() { var raw = Elasto.query({ index: 'circle_test', type: 'tweets' }) .where([{'embeds.url': source.embeds[0].url}, {'embeds.url': source.embeds[1].url}]).raw() raw.body.query.filtered.filter.bool.must.should.have.a.lengthOf(2); return Elasto.query({ index: 'circle_test', type: 'tweets' }) .where([{'embeds.url': source.embeds[0].url}, {'embeds.url': source.embeds[1].url}]) .exec(); }) .then(function(res){ var data = res.hits.hits[0]._source; source.id.should.equal(data.id); source.name.should.equal(data.name); }) .should.eventually.notify(done); }); it('should return a specific size of objects', function(done) { var size = 6; Elasto.query({ index: 'circle_test', type: 'tweets' }) .size(size) .exec() .then(function(data){ data.hits.hits.length.should.be.equal(size); }) .should.eventually.notify(done); }); it('should sort documents', function (done) { Elasto.query({ index: 'circle_test', type: 'tweets' }) .sort('characters') .exec() .then(function(res) { var previous = 0; res.hits.hits.map(function(doc){ return doc._source.characters; }) .forEach(function(value){ value.should.not.be.lessThan(previous); previous = value; }); }) .should.eventually.notify(done); }); it('should find documents in a location with a radius', function (done) { Elasto.query({ index: 'circle_test', type: 'tweets' }) .near(_.extend(london, { radius: 10 })) .exec() .then(function(res) { var docs = _.pluck(res.hits.hits, '_source'); docs.forEach(function(doc){ doc.location.lat.should.be.a.Number; doc.location.lon.should.be.a.Number; }); }) .should.eventually.notify(done); }); it('should handle paging', function (done) { var old = []; Elasto.query({ index: 'circle_test', type: 'tweets' }) .from(0) .size(3) .exec() .then(function(res) { var docs = _.pluck(res.hits.hits, '_source'); docs.length.should.be.equal(3); old = docs; return Elasto.query({ index: 'circle_test', type: 'tweets' }) .from(1) .size(1) .exec(); }) .then(function(res) { var docs = _.pluck(res.hits.hits, '_source'); docs[0].name.should.be.equal(old[1].name); }) .should.eventually.notify(done); }); it('should get documents in a range', function (done) { Elasto.query({ index: 'circle_test', type: 'tweets' }) .range('characters', [120, 150]) .exec() .then(function(res) { var docs = _.pluck(res.hits.hits, '_source'); docs.forEach(function(doc){ doc.characters.should.be.lessThan(151); doc.characters.should.be.greaterThan(119); }); }) .should.eventually.notify(done); }); it('should get documents in a geodistance range', function(done) { Elasto.query({ index: 'circle_test', type: 'tweets' }) .range('distance', _.extend(london, {from: 0, to: 100})) .exec() .then(function(res) { var docs = _.pluck(res.hits.hits, '_source'); docs.length.should.be.greaterThan(0); }) .should.eventually.notify(done); }); it('should return objects in a certain location and sort by distance', function(done) { var radius = 5; Elasto.query({ index: 'circle_test', type: 'tweets' }) .near(_.extend(london, { radius: radius })) .sort('distance') .exec() .then(function(res) { var sorts = _.pluck(res.hits.hits, 'sort'); _.flatten(sorts).forEach(function(sort){ sort.should.be.lessThan(radius); }); }) .should.eventually.notify(done); }); it('should query specific fields', function(done) { Elasto.query({ index: 'circle_test', type: 'tweets' }) .fields(['name', 'id']) .exec() .then(function(res) { var docs = _.pluck(res.hits.hits, '_source'); docs.forEach(function(doc){ _.keys(doc).length.should.equal(2); }); }) .should.eventually.notify(done); }); it('should query specific fields (no array)', function(done) { Elasto.query({ index: 'circle_test', type: 'tweets' }) .fields('name', 'id') .exec() .then(function(res) { var docs = _.pluck(res.hits.hits, '_source'); docs.forEach(function(doc){ _.keys(doc).length.should.equal(2); }); }) .should.eventually.notify(done); }); it('should exclude documents', function (done) { var response = {}; Elasto.query({ index: 'circle_test', type: 'tweets' }) .size(10000) .exec() .then(function(res) { var docs = _.pluck(res.hits.hits, '_source'); response.len = docs.length; response.excluded = docs[0].name; return Elasto.query({ index: 'circle_test', type: 'tweets' }) .size(10000) .not('name', response.excluded) .exec(); }) .then(function(res) { var docs = _.pluck(res.hits.hits, '_source'); docs.length.should.be.equal(response.len - 1); docs.forEach(function(doc) { doc.name.should.not.be.equal(response.excluded); }); }) .should.eventually.notify(done); }); it('should find with a term', function(done) { Elasto.query({ index: 'circle_test', type: 'tweets' }) .exec() .then(function(res) { var docs = _.pluck(res.hits.hits, '_source'); return Bluebird.props({ docs: Elasto.query({ index: 'circle_test', type: 'tweets' }) .term(docs[0].name) .exec(), term: Bluebird.resolve(docs[0].name) }); }) .then(function(opts) { var docs = _.pluck(opts.docs.hits.hits, '_source'); docs[0].name.should.be.equal(opts.term); }) .should.eventually.notify(done); }); it('should count documents', function(done) { Elasto.query({ index: 'circle_test', type: 'tweets' }) .count() .then(function(res) { res.count.should.be.greaterThan(0); }) .should.eventually.notify(done); }); it('should return a search query by default when calling .raw()', function() { var raw = Elasto.query({ index: 'circle_test', type: 'tweets' }) .fields('name') .from(10) .raw(); raw.should.be.ok; }); it('should return a search query when calling .raw() with query', function() { var raw = Elasto.query({ index: 'circle_test', type: 'tweets' }) .fields('name') .from(10) .raw('query'); raw.should.be.ok; }); it('should return a search query by default when calling .raw() with count', function() { var raw = Elasto.query({ index: 'circle_test', type: 'tweets' }) .fields('name') .from(10) .raw('count'); raw.should.be.ok; }); }); }); ================================================ FILE: test/mocha.opts ================================================ --timeout 10000