Skip to content
This repository was archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
Fix/indexandupdate (#58)
Browse files Browse the repository at this point in the history
* General refactor

* Prep to move search behind a v1 endpoint

* Comment out news router until News Rollout

* Comment out news data source until the News Rollout

* Fix indexing _id and increment views
  • Loading branch information
Bouncey authored Dec 5, 2017
1 parent 86250e3 commit 1705347
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
8 changes: 5 additions & 3 deletions elastic/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const events = require('events');
const elasticsearch = require('elasticsearch');
const uuidv4 = require('uuid/v4');

const eventEmitter = new events.EventEmitter();
const promisify = require('promisify-event');
Expand Down Expand Up @@ -47,7 +46,7 @@ function bulkInsert({ index, type, documents }) {
const request = documents.reduce((acc, current) => {
return [
...acc,
Object.assign({}, insert, { _id: current.id }),
{...insert, index: { ...insert.index, _id: current.id } },
current
];
}, []);
Expand Down Expand Up @@ -144,7 +143,10 @@ function incrementViewCount(id) {
type: 'story',
id,
body: {
script: `ctx._source.views += 1; ctx._source.newsViews += '${Date.now()}';`
script: {
lang: 'painless',
inline: `ctx._source.views += 1; ctx._source.newsViews.add(${Date.now()}L);`
}
},
retry_on_conflict: 3
}, function (err) {
Expand Down
34 changes: 33 additions & 1 deletion init/news/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
const { exec } = require('child_process');
const path = require('path');
const svn = require('node-svn-ultimate');
const fse = require('fs-extra');
const matter = require( 'gray-matter');
const { Observable } = require( 'rx');
const { bulkInsert } = require( '../../elastic');
const { log, readDir } = require( '../../utils');

const cURL = `curl -XPUT "http://localhost:9200/news" -H 'Content-Type: application/json' -d'
{
"mappings": {
"story": {
"properties": {
"newsViews": {
"type": "date"
}
}
}
}
}'`;

const logger = log('news');
const viewMap = fse.readFileSync(path.resolve(__dirname, './views.txt'), 'utf8')
.split('\n')
Expand All @@ -31,7 +46,7 @@ async function buildAndInsert(file) {
content: fileData.content,
data: { ...fileData.data },
views: id in viewMap ? viewMap[id] : 1,
newsViews: [],
newsViews: [Date.now()],
url
};
stories = [ ...stories, story ];
Expand All @@ -42,7 +57,24 @@ async function buildAndInsert(file) {
return;
}

function mapNewsViews() {
return new Promise((resolve, reject) => {
exec(cURL, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
reject();
return;
}
if (JSON.parse(stdout).acknowledged) {
resolve();
}
stderr && console.log(`${stderr}`);
});
});
}

exports.getStoryData = async () => {
await mapNewsViews();
fse.remove(storiesDir, (err) => {
if (err) {
logger(err.message, 'yellow');
Expand Down

0 comments on commit 1705347

Please sign in to comment.