Skip to content
This repository has been archived by the owner on Apr 1, 2019. It is now read-only.

Commit

Permalink
Merge pull request #203 from nhsuk/feature/add-result-numbers-to-meta…
Browse files Browse the repository at this point in the history
…data

More visibility/flexibility of the number of results returned from search
  • Loading branch information
neilbmclaughlin authored Aug 7, 2017
2 parents 405c2fc + fb6a182 commit c4c390c
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 88 deletions.
4 changes: 2 additions & 2 deletions app/lib/esGeoQueryBuilder.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const getShouldClause = require('./utils/getTextSearchShouldClause');
const getBaseQuery = require('./utils/getBaseQuery');

function build(location, searchTerm) {
const query = getBaseQuery();
function build(location, searchTerm, size) {
const query = getBaseQuery(size);

query.body.query.bool.filter = {
geo_distance: {
Expand Down
4 changes: 2 additions & 2 deletions app/lib/esQueryBuilder.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const getShouldClause = require('./utils/getTextSearchShouldClause');
const getBaseQuery = require('./utils/getBaseQuery');

function build(searchTerm) {
const query = getBaseQuery();
function build(searchTerm, size) {
const query = getBaseQuery(size);
query.body.query.bool.should = getShouldClause(searchTerm);
return query;
}
Expand Down
4 changes: 2 additions & 2 deletions app/lib/utils/getBaseQuery.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function getBaseQuery() {
function getBaseQuery(size) {
return {
index: 'profiles',
type: 'gps',
body: {
size: 30,
size,
query: {
bool: {}
}
Expand Down
11 changes: 7 additions & 4 deletions app/middleware/getGps.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@ function mapResults(results, res, searchTerm) {
res.locals.resultsSubHeader = resultsFormat.pluraliseSurgery(res.locals.gps.length, searchTerm);
}

function getEsQuery(postcodeLocationDetails, searchTerm) {
function getEsQuery(postcodeLocationDetails, searchTerm, size) {
return (postcodeLocationDetails) ?
esGeoQueryBuilder.build(postcodeLocationDetails.location, searchTerm) :
esQueryBuilder.build(searchTerm);
esGeoQueryBuilder.build(postcodeLocationDetails.location, searchTerm, size) :
esQueryBuilder.build(searchTerm, size);
}

function getGps(req, res, next) {
const searchTerm = res.locals.search;
const postcode = res.locals.postcodeSearch;
const resultsLimit = res.locals.RESULTS_LIMIT;
const postcodeLocationDetails = res.locals.postcodeLocationDetails;
const esQuery = getEsQuery(postcodeLocationDetails, searchTerm);
const esQuery = getEsQuery(postcodeLocationDetails, searchTerm, resultsLimit);

elasticsearchClient
.search(esQuery)
Expand All @@ -56,6 +57,8 @@ function getGps(req, res, next) {
esQuery,
resultCount: results.hits.total
}, 'getGps');
// eslint-disable-next-line no-param-reassign
res.locals.resultsCount = results.hits.total;
mapResults(results, res, searchTerm);
})
.then(next)
Expand Down
1 change: 1 addition & 0 deletions app/middleware/locals.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = config =>
res.locals.WEBTRENDS_ANALYTICS_TRACKING_ID = config.webtrendsId;
res.locals.HOTJAR_ANALYTICS_TRACKING_ID = config.hotjarId;
res.locals.SITE_ROOT = req.app.locals.SITE_ROOT;
res.locals.RESULTS_LIMIT = config.resultsLimit;
/* eslint-enable no-param-reassign */

next();
Expand Down
1 change: 1 addition & 0 deletions app/middleware/setLocals.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const backLinkUtils = require('../lib/utils/backLink');
function fromRequest(req, res, next) {
res.locals.search = req.query.search;
res.locals.postcodeSearch = req.query.postcode;
res.locals.resultsCount = 0;
res.locals.isOutcode = false;
res.locals.searchLabel = 'Surgery or GP name';
res.locals.searchButton = 'Search';
Expand Down
2 changes: 2 additions & 0 deletions app/views/results.nunjucks
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{% block meta %}
<meta name="WT.gposs" content="{{ search }}"></meta>
<meta name="WT.gposs_r" content="1"></meta>
<meta name="DCSext.GPResultsLimit" content="{{ RESULTS_LIMIT }}"></meta>
<meta name="DCSext.GPTotalResults" content="{{ resultsCount }}"></meta>
{% endblock %}

{% block content %}
Expand Down
1 change: 1 addition & 0 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
host: process.env.ES_HOST || 'es',
port: process.env.ES_PORT || '9200',
},
resultsLimit: process.env.RESULTS_LIMIT || 30,
googleAnalyticsId: process.env.GOOGLE_ANALYTICS_TRACKING_ID,
webtrendsId: process.env.WEBTRENDS_ANALYTICS_TRACKING_ID,
hotjarId: process.env.HOTJAR_ANALYTICS_TRACKING_ID,
Expand Down
16 changes: 15 additions & 1 deletion test/integration/resultsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function assertSearchResponse(search, postcode, done, assertions) {
describe('Results page', () => {
const noOnlineBookingLinkMessage = 'This surgery doesn&apos;t have an online booking system.';

describe('page layout', () => {
describe('layout', () => {
it('should contain HTML', (done) => {
const search = 'Surgery';
const postcode = '';
Expand All @@ -47,6 +47,20 @@ describe('Results page', () => {
});
});

it('should contain meta data with results information', (done) => {
const search = 'Surgery';
const postcode = '';

assertSearchResponse(search, postcode, done, (err, res) => {
const $ = cheerio.load(res.text);
const resultsLimit = $('meta[name="DCSext.GPResultsLimit"]').attr('content');
const totalResults = $('meta[name="DCSext.GPTotalResults"]').attr('content');

expect(resultsLimit).to.eq('30');
expect(totalResults).to.eq('3687');
});
});

describe('matching surgeries found', () => {
describe('multiple matches', () => {
it('should have more than one result', (done) => {
Expand Down
5 changes: 3 additions & 2 deletions test/unit/lib/esGeoQueryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ describe('esGeoQueryBuilder', () => {
);
});

it('should return the size as 30', () => {
const query = esGeoQueryBuilder.build(location, searchTerm);
it('should return the size as per the setting in the locale', () => {
const res = { locals: { resultsReturnedCount: 30 } };
const query = esGeoQueryBuilder.build(location, searchTerm, res.locals.resultsReturnedCount);
expect(query.body.size).to.be.equal(30);
});
});
6 changes: 4 additions & 2 deletions test/unit/lib/esQueryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ describe('esQueryBuilder', () => {
);
});

it('should return the size as 30', () => {
const query = esQueryBuilder.build();
it('should return the size as set in the locale', () => {
const searchTerm = 'search for this';
const res = { locals: { resultsReturnedCount: 30 } };
const query = esQueryBuilder.build(searchTerm, res.locals.resultsReturnedCount);

expect(query.body.size).to.be.equal(30);
});
Expand Down
Loading

0 comments on commit c4c390c

Please sign in to comment.