Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 63 additions & 62 deletions aggregations.js
Original file line number Diff line number Diff line change
@@ -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
};
} ());
} ());

126 changes: 63 additions & 63 deletions filter.js
Original file line number Diff line number Diff line change
@@ -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
};
} ());
} ());

75 changes: 38 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
};
} ());
} ());

Loading