From 5e55109e7a73d97d1715391855a9be35d71c1912 Mon Sep 17 00:00:00 2001 From: James Hibbard Date: Fri, 23 Sep 2016 13:33:34 +0200 Subject: [PATCH 1/3] Formatting --- aggregations.js | 132 +++++++++++++++++++------------------- filter.js | 140 ++++++++++++++++++++--------------------- index.js | 102 +++++++++++++++--------------- indices.js | 42 ++++++------- search_all.js | 66 +++++++++---------- search_bool.js | 122 +++++++++++++++++------------------ search_match.js | 76 +++++++++++----------- search_match_phrase.js | 76 +++++++++++----------- search_multi_match.js | 76 +++++++++++----------- suggest_phrase.js | 72 ++++++++++----------- suggest_term.js | 72 ++++++++++----------- 11 files changed, 488 insertions(+), 488 deletions(-) diff --git a/aggregations.js b/aggregations.js index 6e98bd5..61b1291 100644 --- a/aggregations.js +++ b/aggregations.js @@ -1,77 +1,77 @@ (function () { - 'use strict'; + 'use strict'; - const elasticsearch = require('elasticsearch'); - const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' - }); + const elasticsearch = require('elasticsearch'); + const esClient = new elasticsearch.Client({ + host: '127.0.0.1:9200', + log: 'error' + }); - const search = function search(index, body) { - return esClient.search({index: index, body: body}); - }; + const search = function search(index, 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 - } - } - } - }; + // 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 + } + } + } + }; - 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(`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(`\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(`\nthe oldest article is published in ${results.aggregations.min_year.value}`); - 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(`\nthe newest article is published in ${results.aggregations.max_year.value}`); - 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(`\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})`)); - test(); + 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']}`); - module.exports = { - search - }; + 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 + }; } ()); diff --git a/filter.js b/filter.js index f06722d..5542ae0 100644 --- a/filter.js +++ b/filter.js @@ -1,78 +1,78 @@ (function () { - 'use strict'; + 'use strict'; - const elasticsearch = require('elasticsearch'); - const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' - }); + const elasticsearch = require('elasticsearch'); + const esClient = new elasticsearch.Client({ + host: '127.0.0.1:9200', + log: 'error' + }); - const search = function search(index, body) { - return esClient.search({index: index, body: body}); - }; + const search = function search(index, 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 - } - } - } - ] - } - } - }; + // 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 + } + } + } + ] + } + } + }; - 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); - }; + 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); + }; - test(); + test(); - module.exports = { - search - }; + module.exports = { + search + }; } ()); diff --git a/index.js b/index.js index aa9ba63..d7a6524 100644 --- a/index.js +++ b/index.js @@ -1,53 +1,53 @@ (function () { - 'use strict'; - - const fs = require('fs'); - const elasticsearch = require('elasticsearch'); - const esClient = new elasticsearch.Client({ - 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); - }); - - 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); - }; - - test(); - - module.exports = { - bulkIndex - }; + 'use strict'; + + const fs = require('fs'); + const elasticsearch = require('elasticsearch'); + const esClient = new elasticsearch.Client({ + 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); + }); + + 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); + }; + + test(); + + module.exports = { + bulkIndex + }; } ()); diff --git a/indices.js b/indices.js index 4572d93..10fd483 100644 --- a/indices.js +++ b/indices.js @@ -1,28 +1,28 @@ (function () { - 'use strict'; + 'use strict'; - const elasticsearch = require('elasticsearch'); - const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' - }); + const elasticsearch = require('elasticsearch'); + const esClient = new elasticsearch.Client({ + 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}`)); - }; + const indices = function indices() { + 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(); - }; + // only for testing purposes + // all calls should be initiated through the module + const test = function test() { + console.log(`elasticsearch indices information:`); + indices(); + }; - test(); + test(); - module.exports = { - indices - }; + module.exports = { + indices + }; } ()); diff --git a/search_all.js b/search_all.js index 9e24b12..87db731 100644 --- a/search_all.js +++ b/search_all.js @@ -1,40 +1,40 @@ (function () { - 'use strict'; + 'use strict'; - const elasticsearch = require('elasticsearch'); - const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' - }); + const elasticsearch = require('elasticsearch'); + const esClient = new elasticsearch.Client({ + host: '127.0.0.1:9200', + log: 'error' + }); - const search = function search(index, body) { - return esClient.search({index: index, body: body}); - }; + const search = function search(index, 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: {} - } - }; + // 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); - }; + 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(); + test(); - module.exports = { - search - }; -} ()); \ No newline at end of file + module.exports = { + search + }; +} ()); diff --git a/search_bool.js b/search_bool.js index 237e291..0f6cd88 100644 --- a/search_bool.js +++ b/search_bool.js @@ -1,68 +1,68 @@ (function () { - 'use strict'; + 'use strict'; - const elasticsearch = require('elasticsearch'); - const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' - }); + const elasticsearch = require('elasticsearch'); + const esClient = new elasticsearch.Client({ + host: '127.0.0.1:9200', + log: 'error' + }); - const search = function search(index, body) { - return esClient.search({index: index, body: body}); - }; + const search = function search(index, 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 - } - } - } - ] - } - } - }; + // 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 + } + } + } + ] + } + } + }; - 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); - }; + 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); + }; - test(); + test(); - module.exports = { - search - }; -} ()); \ No newline at end of file + module.exports = { + search + }; +} ()); diff --git a/search_match.js b/search_match.js index 48a7c47..dd849d0 100644 --- a/search_match.js +++ b/search_match.js @@ -1,46 +1,46 @@ (function () { - 'use strict'; + 'use strict'; - const elasticsearch = require('elasticsearch'); - const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' - }); + const elasticsearch = require('elasticsearch'); + const esClient = new elasticsearch.Client({ + host: '127.0.0.1:9200', + log: 'error' + }); - const search = function search(index, body) { - return esClient.search({index: index, body: body}); - }; + const search = function search(index, 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 - } - } - } - }; + // 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 + } + } + } + }; - 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} 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(); + test(); - module.exports = { - search - }; + module.exports = { + search + }; } ()); diff --git a/search_match_phrase.js b/search_match_phrase.js index cd68abc..c842b5b 100644 --- a/search_match_phrase.js +++ b/search_match_phrase.js @@ -1,45 +1,45 @@ (function () { - 'use strict'; + 'use strict'; - const elasticsearch = require('elasticsearch'); - const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' - }); + const elasticsearch = require('elasticsearch'); + const esClient = new elasticsearch.Client({ + host: '127.0.0.1:9200', + log: 'error' + }); - const search = function search(index, body) { - return esClient.search({index: index, body: body}); - }; + const search = function search(index, 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' - } - } - } - }; + // 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' + } + } + } + }; - 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.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); + }; - test(); + test(); - module.exports = { - search - }; -} ()); \ No newline at end of file + module.exports = { + search + }; +} ()); diff --git a/search_multi_match.js b/search_multi_match.js index 7e1d5af..f470e31 100644 --- a/search_multi_match.js +++ b/search_multi_match.js @@ -1,45 +1,45 @@ (function () { - 'use strict'; + 'use strict'; - const elasticsearch = require('elasticsearch'); - const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' - }); + const elasticsearch = require('elasticsearch'); + const esClient = new elasticsearch.Client({ + host: '127.0.0.1:9200', + log: 'error' + }); - const search = function search(index, body) { - return esClient.search({index: index, body: body}); - }; + const search = function search(index, 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 - } - } - }; + // 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); - }; + 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); + }; - test(); + test(); - module.exports = { - search - }; -} ()); \ No newline at end of file + module.exports = { + search + }; +} ()); diff --git a/suggest_phrase.js b/suggest_phrase.js index d66c3be..9615167 100644 --- a/suggest_phrase.js +++ b/suggest_phrase.js @@ -1,43 +1,43 @@ (function () { - 'use strict'; + 'use strict'; - const elasticsearch = require('elasticsearch'); - const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' - }); + const elasticsearch = require('elasticsearch'); + const esClient = new elasticsearch.Client({ + host: '127.0.0.1:9200', + log: 'error' + }); - const suggest = function search(index, body) { - return esClient.suggest({index: index, body: body}); - }; + const suggest = function search(index, body) { + return esClient.suggest({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' - } - } - }; + // 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' + } + } + }; - 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.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); + }; - test(); + test(); - module.exports = { - suggest - }; -} ()); \ No newline at end of file + module.exports = { + suggest + }; +} ()); diff --git a/suggest_term.js b/suggest_term.js index af5357e..a6cd90d 100644 --- a/suggest_term.js +++ b/suggest_term.js @@ -1,43 +1,43 @@ (function () { - 'use strict'; + 'use strict'; - const elasticsearch = require('elasticsearch'); - const esClient = new elasticsearch.Client({ - host: '127.0.0.1:9200', - log: 'error' - }); + const elasticsearch = require('elasticsearch'); + const esClient = new elasticsearch.Client({ + host: '127.0.0.1:9200', + log: 'error' + }); - const suggest = function search(index, body) { - return esClient.suggest({index: index, body: body}); - }; + const suggest = function search(index, body) { + return esClient.suggest({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' - } - } - }; + // 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' + } + } + }; - 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.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); + }; - test(); + test(); - module.exports = { - suggest - }; -} ()); \ No newline at end of file + module.exports = { + suggest + }; +} ()); From 3955b603d126df7673f4986632b3217190314ce8 Mon Sep 17 00:00:00 2001 From: James Hibbard Date: Fri, 23 Sep 2016 13:50:11 +0200 Subject: [PATCH 2/3] Updated README --- README.md | 55 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 7f402e6..e2277cf 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,46 @@ -## Elasticsearch in Node.js Examples -------------------------------- +# Build a Custom Search Engine with Node.js and Elasticsearch -This repo contains examples on how to use elasticsearch in Node.js to create search functionalities. +Behrooz Kamali takes an in-depth look at elasticsearch—a scalable, high-performance search engine—demonstrating how to integrate it into a Node project. -To clone this repo in your machine: +Article url: https://www.sitepoint.com/search-engine-node-elasticsearch -``` -git clone https://github.com/behroozk/node-elasticsearch-tutorial.git -``` +## Requirements -Then, to download all the requirements: +* [Node.js](http://nodejs.org/) (min version v0.11.0) +* [Java Runtime Environment](https://java.com/en/) (min version 1.8) +* [Elasticsearch](https://www.elastic.co/) -``` -npm install -``` +## Installation Steps (if applicable) -Here is a list of all the files in this repo: +1. Clone repo +2. Run `npm install` +3. Start Elasticsearch + +More precise instructions can be found in the [installation section of the article](https://www.sitepoint.com/search-engine-node-elasticsearch#installingelasticsearch) + +## List of files in this repo: 1. `data.json`: sample data file -1. `index.js`: script for indexing the data in elasticsearch -1. `search.js`: search functionality -1. `filter.js`: basic filter functionality -1. `aggregations.js`: demonstration of how aggregations work -1. `suggest.js`: script for generating suggestions for search terms +2. `index.js`: script for indexing the data in elasticsearch +3. `indices.js`: script to check indexing was successful +4. `search_all.js`: return all documents in one or more indices +5. `search_match.js`: match documents that contain specific values in a field +6. `search_multi_match.js`: search within multiple fields +7. `search_match_phrase.js`: match a complete phrase +8. `search_bool.js`: combining multiple queries +9. `filter.js`: basic filter functionality +10. `aggregations.js`: demonstration of how aggregations work +11. `suggest_term.js`: generate suggestions for search terms +12. `suggest_phrase.js`: generate suggestions for search phrases + +## License + +The MIT License (MIT) + +Copyright (c) 2016 SitePoint + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. From bba43ab372dd90d020a666d6aab41cb3db5aade6 Mon Sep 17 00:00:00 2001 From: James Hibbard Date: Fri, 23 Sep 2016 13:51:02 +0200 Subject: [PATCH 3/3] Typos --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e2277cf..7e6ee3b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Behrooz Kamali takes an in-depth look at elasticsearch—a scalable, high-performance search engine—demonstrating how to integrate it into a Node project. -Article url: https://www.sitepoint.com/search-engine-node-elasticsearch +**Article url**: https://www.sitepoint.com/search-engine-node-elasticsearch ## Requirements @@ -10,7 +10,7 @@ Article url: https://www.sitepoint.com/search-engine-node-elasticsearch * [Java Runtime Environment](https://java.com/en/) (min version 1.8) * [Elasticsearch](https://www.elastic.co/) -## Installation Steps (if applicable) +## Installation Steps 1. Clone repo 2. Run `npm install`