From 84cdfef99124acf089c9e3995c8404cac93f16be Mon Sep 17 00:00:00 2001 From: KrishnaChittodia21 Date: Fri, 16 Oct 2020 02:56:51 +0530 Subject: [PATCH] fixed failing cases --- aggregations.js | 125 ++++++++++++++++++++-------------------- filter.js | 126 ++++++++++++++++++++--------------------- index.js | 75 ++++++++++++------------ indices.js | 29 +++++----- package-lock.json | 92 ++++++++++++++++++++++++++++++ package.json | 2 +- search_all.js | 53 ++++++++--------- search_bool.js | 106 +++++++++++++++++----------------- search_match.js | 63 +++++++++++---------- search_match_phrase.js | 59 ++++++++++--------- search_multi_match.js | 63 +++++++++++---------- suggest_phrase.js | 59 ++++++++++--------- suggest_term.js | 68 +++++++++++++--------- 13 files changed, 516 insertions(+), 404 deletions(-) create mode 100644 package-lock.json diff --git a/aggregations.js b/aggregations.js index 6e98bd5..2d94feb 100644 --- a/aggregations.js +++ b/aggregations.js @@ -1,77 +1,78 @@ (function () { 'use strict'; - + const elasticsearch = require('elasticsearch'); const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' + host: '127.0.0.1:9200', + log: 'error' }); - + const search = function search(index, body) { - return esClient.search({index: index, body: body}); + return esClient.search({index: index, body: body}); }; - + // only for testing purposes // all calls should be initiated through the module const test = function test() { - let body = { - size: 0, // size is set to 0 here to focus on the aggregations and not the articles - from: 0, - query: { - match_all: {} - }, - aggregations: { - min_year: { - min: {field: 'year'} - }, - max_year: { - max: {field: 'year'} - }, - year_percentile: { - percentiles: {field: 'year'} - }, - year_histogram: { - histogram: {field: 'year', interval: 5} - }, - keywords: { - terms: { - field: 'keywords', - size: 20 - } - } + let body = { + size: 0, // size is set to 0 here to focus on the aggregations and not the articles + from: 0, + query: { + match_all: {} + }, + aggregations: { + min_year: { + min: {field: 'year'} + }, + max_year: { + max: {field: 'year'} + }, + year_percentile: { + percentiles: {field: 'year'} + }, + year_histogram: { + histogram: {field: 'year', interval: 5} + }, + keywords: { + terms: { + field: 'year', + size: 20 } - }; - - console.log(`retrieving documents with a combined bool query and filter (displaying ${body.size} items at a time)`); - search('library', body) - .then(results => { - console.log(`calculated aggregations in ${results.took}ms`); - - console.log(`\nthe oldest article is published in ${results.aggregations.min_year.value}`); - - console.log(`\nthe newest article is published in ${results.aggregations.max_year.value}`); - - console.log(`\nhistogram of number of articles published in 5-year intervals`); - results.aggregations.year_histogram.buckets.forEach(bucket => console.log(`\tnumber of articles published in ${bucket.key}-${bucket.key + 4}: ${'#'.repeat(bucket.doc_count/5)} (${bucket.doc_count})`)); - - console.log(`\npercentile of articles published in different years:`); - console.log(`\t1% of articles are published on or before ${results.aggregations.year_percentile.values['1.0']}`); - console.log(`\t5% of articles are published on or before ${results.aggregations.year_percentile.values['5.0']}`); - console.log(`\t25% of articles are published on or before ${results.aggregations.year_percentile.values['25.0']}`); - console.log(`\t50% of articles are published on or before ${results.aggregations.year_percentile.values['50.0']}`); - console.log(`\t75% of articles are published on or before ${results.aggregations.year_percentile.values['75.0']}`); - console.log(`\t95% of articles are published on or before ${results.aggregations.year_percentile.values['95.0']}`); - console.log(`\t99% of articles are published on or before ${results.aggregations.year_percentile.values['99.0']}`); - - console.log(`\ntop ${results.aggregations.keywords.buckets.length} article tags:`); - results.aggregations.keywords.buckets.forEach((bucket, index) => console.log(`\t${++index} ${bucket.key}: ${bucket.doc_count}`)); - }) - .catch(console.error); + } + } + }; + + console.log(`retrieving documents with a combined bool query and filter (displaying ${body.size} items at a time)`); + search('library', body) + .then(results => { + console.log(`calculated aggregations in ${results.took}ms`); + + console.log(`\nthe oldest article is published in ${results.aggregations.min_year.value}`); + + console.log(`\nthe newest article is published in ${results.aggregations.max_year.value}`); + + console.log(`\nhistogram of number of articles published in 5-year intervals`); + results.aggregations.year_histogram.buckets.forEach(bucket => console.log(`\tnumber of articles published in ${bucket.key}-${bucket.key + 4}: ${'#'.repeat(bucket.doc_count/5)} (${bucket.doc_count})`)); + + console.log(`\npercentile of articles published in different years:`); + console.log(`\t1% of articles are published on or before ${results.aggregations.year_percentile.values['1.0']}`); + console.log(`\t5% of articles are published on or before ${results.aggregations.year_percentile.values['5.0']}`); + console.log(`\t25% of articles are published on or before ${results.aggregations.year_percentile.values['25.0']}`); + console.log(`\t50% of articles are published on or before ${results.aggregations.year_percentile.values['50.0']}`); + console.log(`\t75% of articles are published on or before ${results.aggregations.year_percentile.values['75.0']}`); + console.log(`\t95% of articles are published on or before ${results.aggregations.year_percentile.values['95.0']}`); + console.log(`\t99% of articles are published on or before ${results.aggregations.year_percentile.values['99.0']}`); + + console.log(`\ntop ${results.aggregations.keywords.buckets.length} article tags:`); + results.aggregations.keywords.buckets.forEach((bucket, index) => console.log(`\t${++index} ${bucket.key}: ${bucket.doc_count}`)); + }) + .catch(console.error); }; - + test(); - + module.exports = { - search + search }; -} ()); + } ()); + \ No newline at end of file diff --git a/filter.js b/filter.js index f06722d..a8a50bd 100644 --- a/filter.js +++ b/filter.js @@ -1,78 +1,78 @@ (function () { 'use strict'; - + const elasticsearch = require('elasticsearch'); const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' + host: '127.0.0.1:9200', + log: 'error' }); - + const search = function search(index, body) { - return esClient.search({index: index, body: body}); + return esClient.search({index: index, body: body}); }; - + // only for testing purposes // all calls should be initiated through the module const test = function test() { - let body = { - size: 20, - from: 0, - query: { - bool: { - must: [ - { - query_string: { - query: '(authors.firstname:D* OR authors.lastname:H*) AND (title:excepteur)' - } - } - ], - should: [ - { - match: { - body: { - query: 'Elit nisi fugiat dolore amet', - type: 'phrase' - } - } - } - ], - must_not: [ - { - range: { - year: { - lte: 2000, - gte: 1990 - } - } - } - ], - filter: [ - { - range: { - year: { - gte: 2011, - lte: 2015 - } - } - } - ] + let body = { + size: 20, + from: 0, + query: { + bool: { + must: [ + { + query_string: { + query: '(authors.firstname:D* OR authors.lastname:H*) AND (title:excepteur)' } - } - }; - - console.log(`retrieving documents with a combined bool query and filter (displaying ${body.size} items at a time)...`); - search('library', body) - .then(results => { - console.log(`found ${results.hits.total} items in ${results.took}ms`); - if (results.hits.total > 0) console.log(`returned article titles:`); - results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title} (score: ${hit._score})`)); - }) - .catch(console.error); + } + ], + should: [ + { + match_phrase: { + body: { + query: 'Elit nisi fugiat dolore amet', + } + } + } + ], + must_not: [ + { + range: { + year: { + lte: 2000, + gte: 1990 + } + } + } + ], + filter: [ + { + range: { + year: { + gte: 2011, + lte: 2015 + } + } + } + ] + } + } + }; + + console.log(`retrieving documents with a combined bool query and filter (displaying ${body.size} items at a time)...`); + search('library', body) + .then(results => { + console.log(`found ${results.hits.total.value} items in ${results.took}ms`); + if (results.hits.total.value > 0) console.log(`returned article titles:`); + results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title} (score: ${hit._score})`)); + }) + .catch(console.error); }; - + test(); - + module.exports = { - search + search }; -} ()); + } ()); + \ No newline at end of file diff --git a/index.js b/index.js index aa9ba63..89dc25b 100644 --- a/index.js +++ b/index.js @@ -1,53 +1,54 @@ (function () { 'use strict'; - + const fs = require('fs'); const elasticsearch = require('elasticsearch'); const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' + host: '127.0.0.1:9200', + log: 'error' }); - + const bulkIndex = function bulkIndex(index, type, data) { - let bulkBody = []; - - data.forEach(item => { - bulkBody.push({ - index: { - _index: index, - _type: type, - _id: item.id - } - }); - - bulkBody.push(item); + let bulkBody = []; + + data.forEach(item => { + bulkBody.push({ + index: { + _index: index, + _type: type, + _id: item.id + } }); - - esClient.bulk({body: bulkBody}) - .then(response => { - let errorCount = 0; - response.items.forEach(item => { - if (item.index && item.index.error) { - console.log(++errorCount, item.index.error); - } - }); - console.log(`Successfully indexed ${data.length - errorCount} out of ${data.length} items`); - }) - .catch(console.err); + + bulkBody.push(item); + }); + + esClient.bulk({body: bulkBody}) + .then(response => { + let errorCount = 0; + response.items.forEach(item => { + if (item.index && item.index.error) { + console.log(++errorCount, item.index.error); + } + }); + console.log(`Successfully indexed ${data.length - errorCount} out of ${data.length} items`); + }) + .catch(console.err); }; - + // only for testing purposes // all calls should be initiated through the module const test = function test() { - const articlesRaw = fs.readFileSync('data.json'); - const articles = JSON.parse(articlesRaw); - console.log(`${articles.length} items parsed from data file`); - bulkIndex('library', 'article', articles); + const articlesRaw = fs.readFileSync('data.json'); + const articles = JSON.parse(articlesRaw); + console.log(`${articles.length} items parsed from data file`); + bulkIndex('library', 'article', articles); }; - + test(); - + module.exports = { - bulkIndex + bulkIndex }; -} ()); + } ()); + \ No newline at end of file diff --git a/indices.js b/indices.js index 4572d93..6256f86 100644 --- a/indices.js +++ b/indices.js @@ -1,28 +1,29 @@ (function () { 'use strict'; - + const elasticsearch = require('elasticsearch'); const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' + host: '127.0.0.1:9200', + log: 'error' }); - + const indices = function indices() { - return esClient.cat.indices({v: true}) - .then(console.log) - .catch(err => console.error(`Error connecting to the es client: ${err}`)); + return esClient.cat.indices({v: true}) + .then(console.log) + .catch(err => console.error(`Error connecting to the es client: ${err}`)); }; - + // only for testing purposes // all calls should be initiated through the module const test = function test() { - console.log(`elasticsearch indices information:`); - indices(); + console.log(`elasticsearch indices information:`); + indices(); }; - + test(); - + module.exports = { - indices + indices }; -} ()); + } ()); + \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..b0c3df7 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,92 @@ +{ + "name": "node-elasticsearch-tutorial", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "agentkeepalive": { + "version": "3.5.2", + "resolved": "https://registry.npmjs.org/agentkeepalive/-/agentkeepalive-3.5.2.tgz", + "integrity": "sha512-e0L/HNe6qkQ7H19kTlRRqUibEAwDK5AFk6y3PtMsuut2VAH6+Q4xZml1tNDJD7kSAyqmbG/K08K5WEJYtUrSlQ==", + "requires": { + "humanize-ms": "^1.2.1" + } + }, + "ansi-regex": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" + }, + "ansi-styles": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", + "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=" + }, + "chalk": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", + "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", + "requires": { + "ansi-styles": "^2.2.1", + "escape-string-regexp": "^1.0.2", + "has-ansi": "^2.0.0", + "strip-ansi": "^3.0.0", + "supports-color": "^2.0.0" + } + }, + "elasticsearch": { + "version": "16.7.1", + "resolved": "https://registry.npmjs.org/elasticsearch/-/elasticsearch-16.7.1.tgz", + "integrity": "sha512-PL/BxB03VGbbghJwISYvVcrR9KbSSkuQ7OM//jHJg/End/uC2fvXg4QI7RXLvCGbhBuNQ8dPue7DOOPra73PCw==", + "requires": { + "agentkeepalive": "^3.4.1", + "chalk": "^1.0.0", + "lodash": "^4.17.10" + } + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" + }, + "has-ansi": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", + "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=", + "requires": { + "ansi-regex": "^2.0.0" + } + }, + "humanize-ms": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/humanize-ms/-/humanize-ms-1.2.1.tgz", + "integrity": "sha1-xG4xWaKT9riW2ikxbYtv6Lt5u+0=", + "requires": { + "ms": "^2.0.0" + } + }, + "lodash": { + "version": "4.17.20", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", + "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==" + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + }, + "strip-ansi": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", + "requires": { + "ansi-regex": "^2.0.0" + } + }, + "supports-color": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", + "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=" + } + } +} diff --git a/package.json b/package.json index db5b225..ab3974c 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,6 @@ }, "homepage": "https://github.com/behroozk/node-elasticsearch-tutorial#readme", "dependencies": { - "elasticsearch": "^11.0.1" + "elasticsearch": "^16.7.1" } } diff --git a/search_all.js b/search_all.js index 9e24b12..9cd518d 100644 --- a/search_all.js +++ b/search_all.js @@ -1,40 +1,41 @@ (function () { 'use strict'; - + const elasticsearch = require('elasticsearch'); const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' + host: '127.0.0.1:9200', + log: 'error' }); - + const search = function search(index, body) { - return esClient.search({index: index, body: body}); + return esClient.search({index: index, body: body}); }; - + // only for testing purposes // all calls should be initiated through the module const test = function test() { - let body = { - size: 20, - from: 0, - query: { - match_all: {} - } - }; - - console.log(`retrieving all documents (displaying ${body.size} at a time)...`); - search('library', body) - .then(results => { - console.log(`found ${results.hits.total} items in ${results.took}ms`); - console.log(`returned article titles:`); - results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title}`)); - }) - .catch(console.error); + let body = { + size: 20, + from: 0, + query: { + match_all: {} + } + }; + + console.log(`retrieving all documents (displaying ${body.size} at a time)...`); + search('library', body) + .then(results => { + console.log(`found ${results.hits.total} items in ${results.took}ms`); + console.log(`returned article titles:`); + results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title}`)); + }) + .catch(console.error); }; - + test(); - + module.exports = { - search + search }; -} ()); \ No newline at end of file + } ()); + \ No newline at end of file diff --git a/search_bool.js b/search_bool.js index 237e291..87fdb3c 100644 --- a/search_bool.js +++ b/search_bool.js @@ -1,68 +1,68 @@ (function () { 'use strict'; - + const elasticsearch = require('elasticsearch'); const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' + host: '127.0.0.1:9200', + log: 'error' }); - + const search = function search(index, body) { - return esClient.search({index: index, body: body}); + return esClient.search({index: index, body: body}); }; - + // only for testing purposes // all calls should be initiated through the module const test = function test() { - let body = { - size: 20, - from: 0, - query: { - bool: { - must: [ - { - query_string: { - query: '(authors.firstname:D* OR authors.lastname:H*) AND (title:excepteur)' - } - } - ], - should: [ - { - match: { - body: { - query: 'Elit nisi fugiat dolore amet', - type: 'phrase' - } - } - } - ], - must_not: [ - { - range: { - year: { - lte: 2000, - gte: 1990 - } - } - } - ] + let body = { + size: 20, + from: 0, + query: { + bool: { + must: [ + { + query_string: { + query: '(authors.firstname:D* OR authors.lastname:H*) AND (title:excepteur)' } - } - }; - - console.log(`retrieving documents with a combined bool query (displaying ${body.size} items at a time)...`); - search('library', body) - .then(results => { - console.log(`found ${results.hits.total} items in ${results.took}ms`); - if (results.hits.total > 0) console.log(`returned article titles:`); - results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title} (score: ${hit._score})`)); - }) - .catch(console.error); + } + ], + should: [ + { + match_phrase: { + body: { + query: 'Elit nisi fugiat dolore amet', + } + } + } + ], + must_not: [ + { + range: { + year: { + lte: 2000, + gte: 1990 + } + } + } + ] + } + } + }; + + console.log(`retrieving documents with a combined bool query (displaying ${body.size} items at a time)...`); + search('library', body) + .then(results => { + console.log(`found ${results.hits.total.value} items in ${results.took}ms`); + if (results.hits.total.value > 0) console.log(`returned article titles:`); + results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title} (score: ${hit._score})`)); + }) + .catch(console.error); }; - + test(); - + module.exports = { - search + search }; -} ()); \ No newline at end of file + } ()); + \ No newline at end of file diff --git a/search_match.js b/search_match.js index 48a7c47..d0df432 100644 --- a/search_match.js +++ b/search_match.js @@ -1,46 +1,47 @@ (function () { 'use strict'; - + const elasticsearch = require('elasticsearch'); const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' + host: '127.0.0.1:9200', + log: 'error' }); - + const search = function search(index, body) { - return esClient.search({index: index, body: body}); + return esClient.search({index: index, body: body}); }; - + // only for testing purposes // all calls should be initiated through the module const test = function test() { - let body = { - size: 20, - from: 0, - query: { - match: { - title: { - query: 'Quist partiatur', - minimum_should_match: 3, - fuzziness: 2 - } - } + let body = { + size: 20, + from: 0, + query: { + match: { + title: { + query: 'Quist partiatur', + minimum_should_match: 2, + fuzziness: 2 } - }; - - console.log(`retrieving documents whose title matches '${body.query.match.title.query}' (displaying ${body.size} items at a time)...`); - search('library', body) - .then(results => { - console.log(`found ${results.hits.total} items in ${results.took}ms`); - if (results.hits.total > 0) console.log(`returned article titles:`); - results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title} (score: ${hit._score})`)); - }) - .catch(console.error); + } + } + }; + + console.log(`retrieving documents whose title matches '${body.query.match.title.query}' (displaying ${body.size} items at a time)...`); + search('library', body) + .then(results => { + console.log(`found ${results.hits.total.value} items in ${results.took}ms`); + if (results.hits.total > 0) console.log(`returned article titles:`); + results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title} (score: ${hit._score})`)); + }) + .catch(console.error); }; - + test(); - + module.exports = { - search + search }; -} ()); + } ()); + \ No newline at end of file diff --git a/search_match_phrase.js b/search_match_phrase.js index cd68abc..bf25b03 100644 --- a/search_match_phrase.js +++ b/search_match_phrase.js @@ -1,45 +1,44 @@ (function () { 'use strict'; - + const elasticsearch = require('elasticsearch'); const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' + host: '127.0.0.1:9200', + log: 'error' }); - + const search = function search(index, body) { - return esClient.search({index: index, body: body}); + return esClient.search({index: index, body: body}); }; - + // only for testing purposes // all calls should be initiated through the module const test = function test() { - let body = { - size: 20, - from: 0, - query: { - match: { - title: { - query: 'voluptate anim', - type: 'phrase' - } - } + let body = { + size: 20, + from: 0, + query: { + match_phrase: { + title: { + query: 'voluptate anim', } - }; - - console.log(`retrieving documents whose title matches phrase '${body.query.match.title.query}' (displaying ${body.size} items at a time)...`); - search('library', body) - .then(results => { - console.log(`found ${results.hits.total} items in ${results.took}ms`); - if (results.hits.total > 0) console.log(`returned article titles:`); - results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title} (score: ${hit._score})`)); - }) - .catch(console.error); + } + } + }; + console.log(`retrieving documents whose title matches phrase '${body.query.match_phrase.title.query}' (displaying ${body.size} items at a time)...`); + search('library', body) + .then(results => { + console.log(`found ${results.hits.total.value} items in ${results.took}ms`); + if (results.hits.total.value > 0) console.log(`returned article titles:`); + results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title} (score: ${hit._score})`)); + }) + .catch(console.error); }; - + test(); - + module.exports = { - search + search }; -} ()); \ No newline at end of file + } ()); + \ No newline at end of file diff --git a/search_multi_match.js b/search_multi_match.js index 7e1d5af..5c242b3 100644 --- a/search_multi_match.js +++ b/search_multi_match.js @@ -1,45 +1,46 @@ (function () { 'use strict'; - + const elasticsearch = require('elasticsearch'); const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' + host: '127.0.0.1:9200', + log: 'error' }); - + const search = function search(index, body) { - return esClient.search({index: index, body: body}); + return esClient.search({index: index, body: body}); }; - + // only for testing purposes // all calls should be initiated through the module const test = function test() { - let body = { - size: 20, - from: 0, - query: { - multi_match: { - query: 'Bradford', - fields: ['title', 'authors.*name'], - minimum_should_match: 3, - fuzziness: 2 - } - } - }; - - console.log(`retrieving documents whose title or authors match '${body.query.multi_match.query}' (displaying ${body.size} items at a time)...`); - search('library', body) - .then(results => { - console.log(`found ${results.hits.total} items in ${results.took}ms`); - if (results.hits.total > 0) console.log(`returned article titles:`); - results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title} (score: ${hit._score})`)); - }) - .catch(console.error); + let body = { + size: 20, + from: 0, + query: { + multi_match: { + query: 'Bradford', + fields: ['title', 'authors.*name'], + minimum_should_match: 3, + fuzziness: 2 + } + } + }; + + console.log(`retrieving documents whose title or authors match '${body.query.multi_match.query}' (displaying ${body.size} items at a time)...`); + search('library', body) + .then(results => { + console.log(`found ${results.hits.total.value} items in ${results.took}ms`); + if (results.hits.total > 0) console.log(`returned article titles:`); + results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title} (score: ${hit._score})`)); + }) + .catch(console.error); }; - + test(); - + module.exports = { - search + search }; -} ()); \ No newline at end of file + } ()); + \ No newline at end of file diff --git a/suggest_phrase.js b/suggest_phrase.js index d66c3be..52b1e29 100644 --- a/suggest_phrase.js +++ b/suggest_phrase.js @@ -1,43 +1,46 @@ (function () { 'use strict'; - + const elasticsearch = require('elasticsearch'); const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' + host: '127.0.0.1:9200', + log: 'error' }); - + const suggest = function search(index, body) { - return esClient.suggest({index: index, body: body}); + return esClient.search({index: index, body: body}); }; - + // only for testing purposes // all calls should be initiated through the module const test = function test() { - let body = { - text: 'dolo lore fugi', - bodySuggester: { - phrase: { - field: 'body' - } + let body = { + suggest : { + text: 'Et duis', + simple_phrase: { + phrase: { + field: "title", } - }; - - console.log(`retrieving phrase suggestions for "${body.text}"...`); - suggest('library', body) - .then(results => { - console.log(`suggestions for the phrase are:`); - results.bodySuggester.forEach(phrase => { - console.log(`phrase: ${phrase.text}`); - phrase.options.forEach((option, index) => console.log(`\t suggestion ${++index}: ${option.text}`)); - }); - }) - .catch(console.error); + } + } + }; + + console.log(`retrieving phrase suggestions for "${body.suggest.text}"...`); + suggest('library', body) + .then(results => { + console.log(`suggestions for the phrase are:`); + results.suggest.simple_phrase.forEach(phrase => { + console.log(`phrase: ${phrase.text}`); + phrase.options.forEach((option, index) => console.log(`\t suggestion ${++index}: ${option.text}`)); + }); + }) + .catch(console.error); }; - + test(); - + module.exports = { - suggest + suggest }; -} ()); \ No newline at end of file + } ()); + \ No newline at end of file diff --git a/suggest_term.js b/suggest_term.js index af5357e..73d4e68 100644 --- a/suggest_term.js +++ b/suggest_term.js @@ -1,43 +1,55 @@ (function () { 'use strict'; - + const elasticsearch = require('elasticsearch'); const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' + host: '127.0.0.1:9200', + log: 'error' }); - + const suggest = function search(index, body) { - return esClient.suggest({index: index, body: body}); + return esClient.search({index: index, body: body}); }; - + // only for testing purposes // all calls should be initiated through the module const test = function test() { - let body = { - text: 'dolo lore fugi', - titleSuggester: { - term: { - field: 'title' - } + let body = { + // suggest : { + // suggestion : { + // text : "voluptate anim", + // term : { + // field : "title" + // } + // } + // } + suggest : { + text: 'voluptate anim', + titleSuggester: { + term: { + field: 'title' } - }; - - console.log(`retrieving term suggestions for "${body.text}"...`); - suggest('library', body) - .then(results => { - console.log(`suggestions for each term are:`); - results.titleSuggester.forEach((term, index) => { - console.log(`term ${++index}: ${term.text}`); - term.options.forEach((option, index) => console.log(`\t suggestion ${++index}: ${option.text}`)); - }); - }) - .catch(console.error); + } + } + + }; + + console.log(`retrieving term suggestions for "${body.suggest.text}"...`); + suggest('library', body) + .then(results => { + console.log(`suggestions for each term are: `); + results.suggest.titleSuggester.forEach((term, index) => { + console.log(`term ${++index}: ${term.text}`); + term.options.forEach((option, index) => console.log(`\t suggestion ${++index}: ${option.text}`)); + }); + }) + .catch(console.error); }; - + test(); - + module.exports = { - suggest + suggest }; -} ()); \ No newline at end of file + } ()); + \ No newline at end of file