├── .gitignore ├── README.md ├── addMappingToIndex.js ├── aggregation.js ├── client.js ├── createIndex.js ├── insertData.js ├── package-lock.json ├── package.json ├── ping.js ├── search.js └── searchAsYouType.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # elasticsearch-nodejs-beginners -------------------------------------------------------------------------------- /addMappingToIndex.js: -------------------------------------------------------------------------------- 1 | const esClient = require('./client'); 2 | const addmappingToIndex = async function(indexName, mappingType, mapping){ 3 | console.log(mapping); 4 | return await esClient.indices.putMapping({ 5 | index: indexName, 6 | type: mappingType, 7 | body: mapping 8 | }); 9 | } 10 | 11 | module.exports = addmappingToIndex; 12 | 13 | 14 | // test function to explain how to invoke. 15 | /** 16 | * Example 17 | */ 18 | async function test(){ 19 | const mapping = { 20 | properties: { 21 | title: { 22 | type: "text" 23 | }, 24 | tags: { 25 | type: "keyword" 26 | }, 27 | body: { 28 | type: "text" 29 | }, 30 | timestamp: { 31 | type: "date", 32 | format: "epoch_millis" 33 | } 34 | } 35 | } 36 | try { 37 | const resp = await addmappingToIndex('blog', 'ciphertrick', mapping); 38 | console.log(resp); 39 | } catch (e) { 40 | console.log(e); 41 | } 42 | } 43 | 44 | 45 | // test(); -------------------------------------------------------------------------------- /aggregation.js: -------------------------------------------------------------------------------- 1 | const esClient = require('./client'); 2 | const searchDoc = async function(indexName, mappingType, payload){ 3 | return await esClient.search({ 4 | index: indexName, 5 | type: mappingType, 6 | body: payload 7 | }); 8 | } 9 | 10 | module.exports = searchDoc; 11 | 12 | 13 | /** 14 | * Example 15 | */ 16 | async function test(){ 17 | const body = { 18 | query: { 19 | match: { 20 | "title": "Learn" 21 | } 22 | }, 23 | aggs: { 24 | tags: { 25 | terms: { 26 | field: 'tags' 27 | } 28 | } 29 | } 30 | } 31 | try { 32 | const resp = await searchDoc('blog', 'test', body); 33 | console.log(JSON.stringify(resp)); 34 | } catch (e) { 35 | console.log(e); 36 | } 37 | } 38 | 39 | 40 | // test(); 41 | 42 | // More details here https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html -------------------------------------------------------------------------------- /client.js: -------------------------------------------------------------------------------- 1 | const es = require('elasticsearch'); 2 | const esClient = new es.Client({ 3 | host: 'localhost:9200', 4 | log: 'trace' 5 | }); 6 | 7 | module.exports = esClient; -------------------------------------------------------------------------------- /createIndex.js: -------------------------------------------------------------------------------- 1 | const esClient = require('./client'); 2 | const createIndex = async function(indexName){ 3 | return await esClient.indices.create({ 4 | index: indexName 5 | }); 6 | } 7 | 8 | module.exports = createIndex; 9 | 10 | /** 11 | * Example 12 | */ 13 | async function test(){ 14 | try { 15 | const resp = await createIndex('blog'); 16 | console.log(resp); 17 | } catch (e) { 18 | console.log(e); 19 | } 20 | } 21 | // test(); -------------------------------------------------------------------------------- /insertData.js: -------------------------------------------------------------------------------- 1 | const esClient = require('./client'); 2 | const insertDoc = async function(indexName, _id, mappingType, data){ 3 | return await esClient.index({ 4 | index: indexName, 5 | type: mappingType, 6 | id: _id, 7 | body: data 8 | }); 9 | } 10 | 11 | module.exports = insertDoc; 12 | 13 | 14 | async function test(){ 15 | const data = { 16 | title: "Learn elastic search", 17 | tags: ['NodeJS', 'Programming'], 18 | body: `Lot of content here... 19 | .... article` 20 | } 21 | try { 22 | const resp = await insertDoc('blog', 2, 'ciphertrick', data); 23 | console.log(resp); 24 | } catch (e) { 25 | console.log(e); 26 | } 27 | } 28 | 29 | 30 | //test(); -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "elasticsearch-nodejs", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "agentkeepalive": { 8 | "version": "3.5.2", 9 | "resolved": "https://registry.npmjs.org/agentkeepalive/-/agentkeepalive-3.5.2.tgz", 10 | "integrity": "sha512-e0L/HNe6qkQ7H19kTlRRqUibEAwDK5AFk6y3PtMsuut2VAH6+Q4xZml1tNDJD7kSAyqmbG/K08K5WEJYtUrSlQ==", 11 | "requires": { 12 | "humanize-ms": "1.2.1" 13 | } 14 | }, 15 | "ansi-regex": { 16 | "version": "2.1.1", 17 | "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", 18 | "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" 19 | }, 20 | "ansi-styles": { 21 | "version": "2.2.1", 22 | "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", 23 | "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=" 24 | }, 25 | "chalk": { 26 | "version": "1.1.3", 27 | "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", 28 | "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", 29 | "requires": { 30 | "ansi-styles": "2.2.1", 31 | "escape-string-regexp": "1.0.5", 32 | "has-ansi": "2.0.0", 33 | "strip-ansi": "3.0.1", 34 | "supports-color": "2.0.0" 35 | } 36 | }, 37 | "elasticsearch": { 38 | "version": "15.3.1", 39 | "resolved": "https://registry.npmjs.org/elasticsearch/-/elasticsearch-15.3.1.tgz", 40 | "integrity": "sha512-+fdf2yRlcJChMYq+sw+RyhXidzjdILq+zV25aK/bn8k4Elwa6Stlbua0I2nM7swVeTBFxTpsMZIOolWRcx59hA==", 41 | "requires": { 42 | "agentkeepalive": "3.5.2", 43 | "chalk": "1.1.3", 44 | "lodash": "4.17.11" 45 | } 46 | }, 47 | "escape-string-regexp": { 48 | "version": "1.0.5", 49 | "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", 50 | "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" 51 | }, 52 | "has-ansi": { 53 | "version": "2.0.0", 54 | "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", 55 | "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=", 56 | "requires": { 57 | "ansi-regex": "2.1.1" 58 | } 59 | }, 60 | "humanize-ms": { 61 | "version": "1.2.1", 62 | "resolved": "https://registry.npmjs.org/humanize-ms/-/humanize-ms-1.2.1.tgz", 63 | "integrity": "sha1-xG4xWaKT9riW2ikxbYtv6Lt5u+0=", 64 | "requires": { 65 | "ms": "2.1.1" 66 | } 67 | }, 68 | "lodash": { 69 | "version": "4.17.11", 70 | "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", 71 | "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" 72 | }, 73 | "ms": { 74 | "version": "2.1.1", 75 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", 76 | "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==" 77 | }, 78 | "strip-ansi": { 79 | "version": "3.0.1", 80 | "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", 81 | "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", 82 | "requires": { 83 | "ansi-regex": "2.1.1" 84 | } 85 | }, 86 | "supports-color": { 87 | "version": "2.0.0", 88 | "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", 89 | "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=" 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "elasticsearch-nodejs", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "elasticsearch": "^15.3.1" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ping.js: -------------------------------------------------------------------------------- 1 | const esClient = require('./client'); 2 | 3 | esClient.ping({ 4 | // ping usually has a 3000ms timeout 5 | requestTimeout: 1000 6 | }, function (error) { 7 | if (error) { 8 | console.trace('elasticsearch cluster is down!'); 9 | } else { 10 | console.log('All is well'); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /search.js: -------------------------------------------------------------------------------- 1 | const esClient = require('./client'); 2 | const searchDoc = async function(indexName, mappingType, payload){ 3 | return await esClient.search({ 4 | index: indexName, 5 | type: mappingType, 6 | body: payload 7 | }); 8 | } 9 | 10 | module.exports = searchDoc; 11 | 12 | /** 13 | * Example 14 | */ 15 | async function test(){ 16 | const body = { 17 | query: { 18 | match: { 19 | "title": "Learn" 20 | } 21 | } 22 | } 23 | try { 24 | const resp = await searchDoc('blog', 'ciphertrick', body); 25 | console.log(resp); 26 | } catch (e) { 27 | console.log(e); 28 | } 29 | } 30 | 31 | 32 | //test(); 33 | 34 | // More details here https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html -------------------------------------------------------------------------------- /searchAsYouType.js: -------------------------------------------------------------------------------- 1 | const esClient = require('./client'); 2 | const searchDoc = async function(indexName, mappingType, payload){ 3 | return await esClient.search({ 4 | index: indexName, 5 | type: mappingType, 6 | body: payload 7 | }); 8 | } 9 | 10 | module.exports = searchDoc; 11 | 12 | 13 | async function test(){ 14 | const body = { 15 | query: { 16 | match_phrase_prefix: { 17 | "title": "Lea" 18 | } 19 | } 20 | } 21 | try { 22 | const resp = await searchDoc('blog', 'ciphertrick', body); 23 | console.log(resp); 24 | } catch (e) { 25 | console.log(e); 26 | } 27 | } 28 | 29 | 30 | //test(); 31 | 32 | // More details here https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html --------------------------------------------------------------------------------