Skip to content

Commit

Permalink
corrected error in discovery code chapter 8
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Dill committed Nov 3, 2017
1 parent 1d764e5 commit ad1bd5c
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ exports.getNews = function(req, res, next)
// the Discovery API wants to receive a single JSON object as the passed in parameter.
// That JSON object has 7 elements in it, each of which is populated below.
var _query = {};
var qry;
var qry = '';
// Discovery allows you to query on multiple terms, just as did Alchemy. However with
// Discovery, the terms are separated by a 'pipe' character: | to mean 'or'. The following
// for/each loop creates the query object.
for (each in req.body.query){(function(_idx, _arr){qry = ((_idx == 0) ? _arr[_idx] : '|'+_arr[_idx]);})(each, req.body.query);}
_query.query = '"'+qry+'"';
__qry__ = req.body.query.split(',');
console.log(__qry__);
for (each in __qry__){(function(_idx, _arr){qry += ((_idx == 0) ? ' "'+_arr[_idx]+'" ' : '|'+' "'+_arr[_idx]+'" ');})(each, __qry__);}
_query.query = qry;
_query.filter = 'language:(english|en),crawl_date>'+start_date+',crawl_date<'+end_date;
_query.aggregation = '[nested(enriched_title.entities).filter(enriched_title.entities.type:Company).term(enriched_title.entities.text)]';
_query.count = parseInt(qry_count);
Expand All @@ -110,7 +112,6 @@ exports.getNews = function(req, res, next)
}
*/
// log the results on the console and send them back to the browser.
console.log(method+" success! "+JSON.stringify(data, null, 2));
res.send({"results":"success", "data": data});
}
});
Expand Down
6 changes: 0 additions & 6 deletions Chapter08/Documentation/answers/controller/router_complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var express = require('express');
var router = express.Router();
var speech_to_text = require('./features/speech_to_text');
var auth = require('./features/authenticate');
var multi_lingual = require('./features/multi_lingual');
var discovery = require('./features/discovery');
var format = require('date-format');

Expand All @@ -37,10 +36,5 @@ router.post('/auth/authenticate*', auth.authenticate);
router.post('/auth/register*', auth.register);
router.post('/auth/logout*', auth.logout);


router.get('/api/getSupportedLanguages*',multi_lingual.languages);
router.get('/api/getTextLocations*',multi_lingual.locations);
router.post('/api/selectedPrompts*',multi_lingual.prompts);

router.get('/discovery/getID*',discovery.getID);
router.post('/discovery/getNews*',discovery.getNews);
105 changes: 105 additions & 0 deletions Chapter08/controller/restapi/features/discovery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var extend = require('extend');
var watson = require('watson-developer-cloud');
var vcapServices = require('vcap_services');
var config = require('../../env.json');

var DiscoveryV1 = require('watson-developer-cloud/discovery/v1');
console.log(config.watson_discovery);
var discovery = new DiscoveryV1({
username: config.watson_discovery.credentials.username,
password: config.watson_discovery.credentials.password,
version: config.watson_discovery.credentials.version,
version_date: config.watson_discovery.credentials.version_date
});

/**
* getID returns a json object with all environments, configurations and collections
* in the Discovery News version, since we are not creating any of the above, there
* will be one environment, one configuration and one collection returned.
* @param {object} req - nodejs object with the request information
* req.body holds post parameters
* @param {object} res - nodejs response object
* @param {object} next - nodejs next object - used if this routine does not provide a response
*/
exports.getID = function(req, res, next)
{
var method = "getID";
discovery.getEnvironments({}, function(error, data) {
if (error)
{

}
else
{

}
});
}

/**
* getNews returns a json object with all environments, configurations and collections
* in the Discovery News version, since we are not creating any of the above, there
* will be one environment, one configuration and one collection returned.get the value of a specific cookie
* @param {object} req - nodejs object with the request information
* req.body holds post parameters
* req.body.startDate - when to start looking for documents
* req.body.endDate - when to stop looking
* req.body.count - number of documents to return
* req.body.query - comma delimited string of query words and phrases
* @param {object} res - nodejs response object
* @param {object} next - nodejs next object - used if this routine does not provide a response
*/
exports.getNews = function(req, res, next)
{
var method = "getNews";
// Discovery wants dates in something very close to ISO format, just without the trailing Z and with a timezone offset
// so _time forces the time of day to be 8AM
var _time = 'T12:00:00-0400';
// The browser sends in 4 values as part of the post command, each of which is delivered to nodesjs as
// part of req.body.

// the Discovery API wants to receive a single JSON object as the passed in parameter.
// That JSON object has 7 elements in it, each of which is populated below.
var _query = {};
var qry;
// Discovery allows you to query on multiple terms, just as did Alchemy. However with
// Discovery, the terms are separated by a 'pipe' character: | to mean 'or'. The following
// for/each loop creates the query object.

_query.query = '"'+qry+'"';

_query.aggregation = '[nested(enriched_title.entities).filter(enriched_title.entities.type:Company).term(enriched_title.entities.text)]';

_query.return = 'title,url,host,crawl_date, text, enriched_text.sentiment.document, author';
_query.environment_id = 'system';
_query.collection_id = 'news';
console.log(_query);
discovery.query(_query, function(error, data) {
var method = "query";
if (error)
{ // log the error and send an error message back to the browser

}
else
{ // the following for each loop is purely for diagnostic purposes and is commented
// out. If you uncomment this section, you will see data sent to your console.

}
});

}

0 comments on commit ad1bd5c

Please sign in to comment.