├── .gitignore ├── README.md ├── docker-compose.yml ├── dropIndex.js ├── indexData.js ├── package-lock.json ├── package.json └── searchIndex.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | 3 | # Logs 4 | logs 5 | *.log 6 | npm-debug.log* 7 | yarn-debug.log* 8 | yarn-error.log* 9 | lerna-debug.log* 10 | .pnpm-debug.log* 11 | 12 | # Diagnostic reports (https://nodejs.org/api/report.html) 13 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 14 | 15 | # Runtime data 16 | pids 17 | *.pid 18 | *.seed 19 | *.pid.lock 20 | 21 | # Directory for instrumented libs generated by jscoverage/JSCover 22 | lib-cov 23 | 24 | # Coverage directory used by tools like istanbul 25 | coverage 26 | *.lcov 27 | 28 | # nyc test coverage 29 | .nyc_output 30 | 31 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 32 | .grunt 33 | 34 | # Bower dependency directory (https://bower.io/) 35 | bower_components 36 | 37 | # node-waf configuration 38 | .lock-wscript 39 | 40 | # Compiled binary addons (https://nodejs.org/api/addons.html) 41 | build/Release 42 | 43 | # Dependency directories 44 | node_modules/ 45 | jspm_packages/ 46 | 47 | # Snowpack dependency directory (https://snowpack.dev/) 48 | web_modules/ 49 | 50 | # TypeScript cache 51 | *.tsbuildinfo 52 | 53 | # Optional npm cache directory 54 | .npm 55 | 56 | # Optional eslint cache 57 | .eslintcache 58 | 59 | # Optional stylelint cache 60 | .stylelintcache 61 | 62 | # Microbundle cache 63 | .rpt2_cache/ 64 | .rts2_cache_cjs/ 65 | .rts2_cache_es/ 66 | .rts2_cache_umd/ 67 | 68 | # Optional REPL history 69 | .node_repl_history 70 | 71 | # Output of 'npm pack' 72 | *.tgz 73 | 74 | # Yarn Integrity file 75 | .yarn-integrity 76 | 77 | # dotenv environment variable files 78 | .env 79 | .env.development.local 80 | .env.test.local 81 | .env.production.local 82 | .env.local 83 | 84 | # parcel-bundler cache (https://parceljs.org/) 85 | .cache 86 | .parcel-cache 87 | 88 | # Next.js build output 89 | .next 90 | out 91 | 92 | # Nuxt.js build / generate output 93 | .nuxt 94 | dist 95 | 96 | # Gatsby files 97 | .cache/ 98 | # Comment in the public line in if your project uses Gatsby and not Next.js 99 | # https://nextjs.org/blog/next-9-1#public-directory-support 100 | # public 101 | 102 | # vuepress build output 103 | .vuepress/dist 104 | 105 | # vuepress v2.x temp and cache directory 106 | .temp 107 | .cache 108 | 109 | # Docusaurus cache and generated files 110 | .docusaurus 111 | 112 | # Serverless directories 113 | .serverless/ 114 | 115 | # FuseBox cache 116 | .fusebox/ 117 | 118 | # DynamoDB Local files 119 | .dynamodb/ 120 | 121 | # TernJS port file 122 | .tern-port 123 | 124 | # Stores VSCode versions used for testing VSCode extensions 125 | .vscode-test 126 | 127 | # yarn v2 128 | .yarn/cache 129 | .yarn/unplugged 130 | .yarn/build-state.yml 131 | .yarn/install-state.gz 132 | .pnp.* 133 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Install dependencies 2 | ```npm install``` 3 | 4 | ### Run the service 5 | ```npm run dev``` 6 | 7 | ### Create dummy index (using Faker) 8 | ```npm run index:create``` 9 | 10 | ### Search in the created dummy index (change the search term in the code) 11 | ```npm run index:search``` 12 | 13 | ### Drop the whole dummy index 14 | ```npm run index:drop``` 15 | 16 | ### Kill the service 17 | ```npm run dev:down``` 18 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.0" 2 | services: 3 | elasticsearch: 4 | container_name: es-container 5 | image: docker.elastic.co/elasticsearch/elasticsearch:8.0.1 6 | environment: 7 | - xpack.security.enabled=false 8 | - "discovery.type=single-node" 9 | networks: 10 | - es-net 11 | ports: 12 | - 9200:9200 13 | kibana: 14 | container_name: kb-container 15 | image: docker.elastic.co/kibana/kibana:8.0.1 16 | environment: 17 | - ELASTICSEARCH_HOSTS=http://es-container:9200 18 | networks: 19 | - es-net 20 | depends_on: 21 | - elasticsearch 22 | ports: 23 | - 5601:5601 24 | networks: 25 | es-net: 26 | driver: bridge 27 | -------------------------------------------------------------------------------- /dropIndex.js: -------------------------------------------------------------------------------- 1 | const { Client } = require('@elastic/elasticsearch') 2 | const client = new Client({ node: 'http://localhost:9200' }) 3 | 4 | async function run () { 5 | await client.indices.delete({ index: 'game-of-thrones' }); 6 | } 7 | 8 | run().catch(console.log) 9 | -------------------------------------------------------------------------------- /indexData.js: -------------------------------------------------------------------------------- 1 | const { Client } = require('@elastic/elasticsearch') 2 | const client = new Client({ node: 'http://localhost:9200' }) 3 | const faker = require('faker') 4 | 5 | async function run () { 6 | // Let's start by indexing some data 7 | for( let i=0; i<=100; i++ ) { 8 | await client.index({ 9 | index: 'game-of-thrones', 10 | document: { 11 | character: `${faker.name.firstName()} ${faker.name.lastName()}`, 12 | quote: faker.lorem.words() 13 | } 14 | }) 15 | } 16 | 17 | // here we are forcing an index refresh, otherwise we will not 18 | // get any result in the consequent search 19 | await client.indices.refresh({ index: 'game-of-thrones' }) 20 | } 21 | 22 | run().catch(console.log) 23 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "es-test", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "es-test", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "@elastic/elasticsearch": "^8.0.0", 13 | "faker": "^5.5.3" 14 | } 15 | }, 16 | "node_modules/@elastic/elasticsearch": { 17 | "version": "8.0.0", 18 | "resolved": "https://registry.npmjs.org/@elastic/elasticsearch/-/elasticsearch-8.0.0.tgz", 19 | "integrity": "sha512-V9H35N4rc9uxf+5WYSOwdwdJeP+oa5i933QuMKJNaLj9PbADVXj1sCkCt7qdAmRf/0qOuTuVK18fNrkHhWUDCA==", 20 | "dependencies": { 21 | "@elastic/transport": "^8.0.1", 22 | "tslib": "^2.3.0" 23 | }, 24 | "engines": { 25 | "node": ">=12" 26 | } 27 | }, 28 | "node_modules/@elastic/transport": { 29 | "version": "8.0.2", 30 | "resolved": "https://registry.npmjs.org/@elastic/transport/-/transport-8.0.2.tgz", 31 | "integrity": "sha512-OlDz3WO3pKE9vSxW4wV/mn7rYCtBmSsDwxr64h/S1Uc/zrIBXb0iUsRMSkiybXugXhjwyjqG2n1Wc7jjFxrskQ==", 32 | "dependencies": { 33 | "debug": "^4.3.2", 34 | "hpagent": "^0.1.2", 35 | "ms": "^2.1.3", 36 | "secure-json-parse": "^2.4.0", 37 | "tslib": "^2.3.0", 38 | "undici": "^4.14.1" 39 | }, 40 | "engines": { 41 | "node": ">=12" 42 | } 43 | }, 44 | "node_modules/debug": { 45 | "version": "4.3.3", 46 | "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.3.tgz", 47 | "integrity": "sha512-/zxw5+vh1Tfv+4Qn7a5nsbcJKPaSvCDhojn6FEl9vupwK2VCSDtEiEtqr8DFtzYFOdz63LBkxec7DYuc2jon6Q==", 48 | "dependencies": { 49 | "ms": "2.1.2" 50 | }, 51 | "engines": { 52 | "node": ">=6.0" 53 | }, 54 | "peerDependenciesMeta": { 55 | "supports-color": { 56 | "optional": true 57 | } 58 | } 59 | }, 60 | "node_modules/debug/node_modules/ms": { 61 | "version": "2.1.2", 62 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", 63 | "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" 64 | }, 65 | "node_modules/faker": { 66 | "version": "5.5.3", 67 | "resolved": "https://registry.npmjs.org/faker/-/faker-5.5.3.tgz", 68 | "integrity": "sha512-wLTv2a28wjUyWkbnX7u/ABZBkUkIF2fCd73V6P2oFqEGEktDfzWx4UxrSqtPRw0xPRAcjeAOIiJWqZm3pP4u3g==" 69 | }, 70 | "node_modules/hpagent": { 71 | "version": "0.1.2", 72 | "resolved": "https://registry.npmjs.org/hpagent/-/hpagent-0.1.2.tgz", 73 | "integrity": "sha512-ePqFXHtSQWAFXYmj+JtOTHr84iNrII4/QRlAAPPE+zqnKy4xJo7Ie1Y4kC7AdB+LxLxSTTzBMASsEcy0q8YyvQ==" 74 | }, 75 | "node_modules/ms": { 76 | "version": "2.1.3", 77 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", 78 | "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" 79 | }, 80 | "node_modules/secure-json-parse": { 81 | "version": "2.4.0", 82 | "resolved": "https://registry.npmjs.org/secure-json-parse/-/secure-json-parse-2.4.0.tgz", 83 | "integrity": "sha512-Q5Z/97nbON5t/L/sH6mY2EacfjVGwrCcSi5D3btRO2GZ8pf1K1UN7Z9H5J57hjVU2Qzxr1xO+FmBhOvEkzCMmg==" 84 | }, 85 | "node_modules/tslib": { 86 | "version": "2.3.1", 87 | "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz", 88 | "integrity": "sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==" 89 | }, 90 | "node_modules/undici": { 91 | "version": "4.15.0", 92 | "resolved": "https://registry.npmjs.org/undici/-/undici-4.15.0.tgz", 93 | "integrity": "sha512-kHppwh/y49FLEXl/zYCCbGB0D3nrcWNBczNYCsDdNYzWPs80aQgfKic1PVkJEIc2YlR7m0Lf5i559zbr0AA7FQ==", 94 | "engines": { 95 | "node": ">=12.18" 96 | } 97 | } 98 | }, 99 | "dependencies": { 100 | "@elastic/elasticsearch": { 101 | "version": "8.0.0", 102 | "resolved": "https://registry.npmjs.org/@elastic/elasticsearch/-/elasticsearch-8.0.0.tgz", 103 | "integrity": "sha512-V9H35N4rc9uxf+5WYSOwdwdJeP+oa5i933QuMKJNaLj9PbADVXj1sCkCt7qdAmRf/0qOuTuVK18fNrkHhWUDCA==", 104 | "requires": { 105 | "@elastic/transport": "^8.0.1", 106 | "tslib": "^2.3.0" 107 | } 108 | }, 109 | "@elastic/transport": { 110 | "version": "8.0.2", 111 | "resolved": "https://registry.npmjs.org/@elastic/transport/-/transport-8.0.2.tgz", 112 | "integrity": "sha512-OlDz3WO3pKE9vSxW4wV/mn7rYCtBmSsDwxr64h/S1Uc/zrIBXb0iUsRMSkiybXugXhjwyjqG2n1Wc7jjFxrskQ==", 113 | "requires": { 114 | "debug": "^4.3.2", 115 | "hpagent": "^0.1.2", 116 | "ms": "^2.1.3", 117 | "secure-json-parse": "^2.4.0", 118 | "tslib": "^2.3.0", 119 | "undici": "^4.14.1" 120 | } 121 | }, 122 | "debug": { 123 | "version": "4.3.3", 124 | "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.3.tgz", 125 | "integrity": "sha512-/zxw5+vh1Tfv+4Qn7a5nsbcJKPaSvCDhojn6FEl9vupwK2VCSDtEiEtqr8DFtzYFOdz63LBkxec7DYuc2jon6Q==", 126 | "requires": { 127 | "ms": "2.1.2" 128 | }, 129 | "dependencies": { 130 | "ms": { 131 | "version": "2.1.2", 132 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", 133 | "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" 134 | } 135 | } 136 | }, 137 | "faker": { 138 | "version": "5.5.3", 139 | "resolved": "https://registry.npmjs.org/faker/-/faker-5.5.3.tgz", 140 | "integrity": "sha512-wLTv2a28wjUyWkbnX7u/ABZBkUkIF2fCd73V6P2oFqEGEktDfzWx4UxrSqtPRw0xPRAcjeAOIiJWqZm3pP4u3g==" 141 | }, 142 | "hpagent": { 143 | "version": "0.1.2", 144 | "resolved": "https://registry.npmjs.org/hpagent/-/hpagent-0.1.2.tgz", 145 | "integrity": "sha512-ePqFXHtSQWAFXYmj+JtOTHr84iNrII4/QRlAAPPE+zqnKy4xJo7Ie1Y4kC7AdB+LxLxSTTzBMASsEcy0q8YyvQ==" 146 | }, 147 | "ms": { 148 | "version": "2.1.3", 149 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", 150 | "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" 151 | }, 152 | "secure-json-parse": { 153 | "version": "2.4.0", 154 | "resolved": "https://registry.npmjs.org/secure-json-parse/-/secure-json-parse-2.4.0.tgz", 155 | "integrity": "sha512-Q5Z/97nbON5t/L/sH6mY2EacfjVGwrCcSi5D3btRO2GZ8pf1K1UN7Z9H5J57hjVU2Qzxr1xO+FmBhOvEkzCMmg==" 156 | }, 157 | "tslib": { 158 | "version": "2.3.1", 159 | "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz", 160 | "integrity": "sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==" 161 | }, 162 | "undici": { 163 | "version": "4.15.0", 164 | "resolved": "https://registry.npmjs.org/undici/-/undici-4.15.0.tgz", 165 | "integrity": "sha512-kHppwh/y49FLEXl/zYCCbGB0D3nrcWNBczNYCsDdNYzWPs80aQgfKic1PVkJEIc2YlR7m0Lf5i559zbr0AA7FQ==" 166 | } 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "es-test", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "dev": "docker-compose up -d", 8 | "dev:down": "docker-compose down", 9 | "index:create": "node indexData.js", 10 | "index:search": "node searchIndex.js", 11 | "index:drop": "node dropIndex.js" 12 | }, 13 | "keywords": [], 14 | "author": "", 15 | "license": "ISC", 16 | "dependencies": { 17 | "@elastic/elasticsearch": "^8.0.0", 18 | "faker": "^5.5.3" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /searchIndex.js: -------------------------------------------------------------------------------- 1 | const { Client } = require('@elastic/elasticsearch') 2 | const client = new Client({ node: 'http://localhost:9200' }) 3 | 4 | async function run () { 5 | // here we are forcing an index refresh, otherwise we will not 6 | // get any result in the consequent search 7 | await client.indices.refresh({ index: 'game-of-thrones' }) 8 | 9 | // Let's search! 10 | const result= await client.search({ 11 | index: 'game-of-thrones', 12 | query: { 13 | multi_match: { 14 | query: 'dolor', 15 | fields: ['character', 'quote'] 16 | } 17 | } 18 | }) 19 | 20 | console.log(result.hits.hits) 21 | console.log(result.hits.hits.length) 22 | 23 | // await client.indices.delete({ index: 'game-of-thrones' }); 24 | } 25 | 26 | run().catch(console.log) 27 | --------------------------------------------------------------------------------