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