├── README.md ├── client.js ├── ping.js ├── package.json ├── createIndex.js ├── insertData.js ├── searchAsYouType.js ├── search.js ├── aggregation.js ├── addMappingToIndex.js └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # elasticsearch-nodejs-beginners -------------------------------------------------------------------------------- /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; -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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(); -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /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(); -------------------------------------------------------------------------------- /.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 | --------------------------------------------------------------------------------