From 8d3d53c26192f2bd31043e631c24b8ee83eef917 Mon Sep 17 00:00:00 2001 From: goferito Date: Fri, 6 Nov 2015 20:41:19 +0100 Subject: [PATCH] Support for related search and spelling suggestions --- README.md | 14 ++++++++++++++ lib/bing.js | 37 +++++++++++++++++++++++++++++++++++++ test/integration.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) diff --git a/README.md b/README.md index 7a1a33f..b949d7f 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,20 @@ Accepted filter values: * Style:\ * Face:\ +#### Related Search: +```js +Bing.relatedSearch('berlin', {market: 'en-US'}, function (err, res, body) { + var suggestions = body.d.results.map(function(r){ return r.Title}); + console.log(suggestions.join('\n')); +}); +``` + +#### Spelling Suggestions: +```js +Bing.spellingSuggestion('awsome spell', function (err, res, body) { + console.log(body.d.results[0]); //awesome spell +}); +``` #### Specify Market Getting spanish results: diff --git a/lib/bing.js b/lib/bing.js index a28f72f..3e77b50 100644 --- a/lib/bing.js +++ b/lib/bing.js @@ -258,6 +258,43 @@ Bing.prototype.images = function (query, options, callback) { }; +/** + * Performs a Bing search in the Related Search vertical. + * + * @param {String} query Query term to search for. + * + * @param {Object} options Options to command, allows overriding + * of rootUri, accKey (Bing API key), + * userAgent, reqTimeout, top, skip, + * + * @param {requestCallback} callback Callback called with (potentially + * json-parsed) response. + * @function + */ +Bing.prototype.relatedSearch = function (query, options, callback) { + this.searchVertical(query, "RelatedSearch", options, callback); +}; + + +/** + * Performs a Bing search in the Spelling Suggestions vertical. + * + * @param {String} query Query term to search for. + * + * @param {Object} options Options to command, allows overriding + * of rootUri, accKey (Bing API key), + * userAgent, reqTimeout, top, skip, + * + * @param {requestCallback} callback Callback called with (potentially + * json-parsed) response. + * @function + */ +Bing.prototype.spelling = function (query, options, callback) { + this.searchVertical(query, "SpellingSuggestions", options, callback); +}; + + + /** * Capitalises the first word of the passed string * diff --git a/test/integration.js b/test/integration.js index d176e46..65e4377 100644 --- a/test/integration.js +++ b/test/integration.js @@ -165,3 +165,48 @@ describe("Bing Video", function () { }); }); + +describe("Bing Related Search", function () { + + this.timeout(1000 * 10); + + it('finds related search suggestions', function (done) { + + Bing.relatedSearch('berlin', + { top: 5, market: 'en-US' }, + function (err, res, body) { + + should.not.exist(err); + should.exist(res); + should.exist(body); + + body.d.results.should.have.length(5); + + done(); + }); + }); +}); + + +describe("Bing Spelling Suggestion", function () { + + this.timeout(1000 * 10); + + it('finds proper spelling', function (done) { + + Bing.spelling('awsome spell', + function (err, res, body) { + + should.not.exist(err); + should.exist(res); + should.exist(body); + + // Find at leas one suggestion + body.d.results.length.should.be.aboveOrEqual(1); + body.d.results[0].Value.should.equal("awesome spell"); + + done(); + }); + }); +}); +