Skip to content

Commit

Permalink
creatives scores are boosted on matching more tags and on matching ge…
Browse files Browse the repository at this point in the history
…olocation of placement. #67 #68 #70
  • Loading branch information
ysholqamy committed May 1, 2015
1 parent 8bfaf82 commit 6ce44a9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 74 deletions.
142 changes: 76 additions & 66 deletions lib/placement_matcher.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,95 @@
var client = require('./elasticsearch/client');

module.exports = function(placement) {
//construct should array

var interestsFiltersArray = [];
var interests = placement.tags;
interests.forEach(function(interest){
var shouldObject = {
term: { tags: interest}
}
interestsFiltersArray.push(shouldObject);
});
//construct should array
var placementCity = (placement.city || "").toLowerCase();
var placementCountry = (placement.country || "").toLowerCase();
return client.search({
var tags = placement.tags.join(" ");
return client.search({
index: 'creatives',
type: 'creative',
//we only need the highest 2 creatives to calculate the price for the winning creative.
body: {
query: {
function_score: {
filter: {
bool: {
// each document should match ANY of the location condtions AND the ANY of the tags associated with the placement
should: [
{and:[
{or:[
{
and: [
{term: {targetCountry: placementCountry} },
{script: {script: "doc['targetCity'].empty"}}
]
},
{
and: [
{ term: {targetCountry: placementCountry} },
{ term: { targetCity: placementCity} }
body: {
// we only need the highest 2 creatives.
"size": 2,
"query": {
"function_score": {
"field_value_factor": {
"field": "cpm",
"factor": 10
},
"query": {
"bool": {
"must": {
"match": {
// full-text search utilized to score relevance of tags.
"tags": tags
}
},
// a creative should match atleast one of the conditions in the should array
"should": [
{
// match exact city and country in placement.
"constant_score": {
"filter": {
"and": [
{"term": {"targetCountry": placementCountry} },
{"term": {"targetCity": placementCity} }
]
},
"boost": 3
}
},
{
// match exact country and does not target a certain city.
"constant_score": {
"filter": {
"and": [
{"term": {"targetCountry": placementCountry}},
{"missing":{"field": "targetCity" }}
]
},
{
script: {
script: "doc['targetCountry'].empty && doc['targetOrigin'].empty"
}
},
{
and: [
{
script: {
params: { "pLat": placement.latitude || 0.0, "pLon": placement.longitude || 0.0 },
script: "!doc['targetOrigin'].empty && !doc['targetRadius'].empty && doc['targetOrigin'].distanceInKm(pLat, pLon) <= doc['targetRadius'].value"
}
"boost": 2
}
},
{
// match a certain area covered by a certain radius from a certian origin.
"constant_score": {
"filter": {
"and": [
{"missing": {"field": "targetCountry" }},
{"missing": {"field": "targetCity" }},
{
"script": {
"params": {
"lat": placement.latitude || 0.0,
"lon": placement.longitude || 0.0
},
"script": "!doc['targetOrigin'].empty && doc['targetOrigin'].distanceInKm(lat,lon) <= doc['targetRadius'].value"
}
}
]
}
]
},
"boost": 4
}
},
{
or: interestsFiltersArray
// no geolocation targeting at all
"constant_score": {
"filter": {
"and":[
{"missing" :{"field": "targetCountry"}},
{"missing" : {"field": "targetCity"}},
{"missing" : {"field": "targetOrigin"}}
]
},
"boost": 1.2
}
}
]
}
],
must: [
// each document mush match the exact width and height in the placement
//TODO: created standard adTypes which will reduce this to one condition only.
{term: {width: placement.width}},
{term: {height: placement.height}}
]
}

},
functions:[
{
script_score: {
// the score depends on how matching is this creative with the placement, and how much does this creative cost.
script: "_score*doc[\"cpm\"].value"
}
], "minimum_number_should_match": 1
}
]
}
}
}
}
})
});
}
16 changes: 8 additions & 8 deletions models/creative.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var asIndexed = function() {
.then(function(advertiser){
var indexedHash = {
id: _this.dataValues.id,
tags: campaign.dataValues.tags.split(','),
tags: campaign.dataValues.tags,
advertiserId: advertiser.dataValues.id,
campaignId: campaign.dataValues.id,
remainingBudget: campaign.dataValues.budget,
Expand Down Expand Up @@ -73,14 +73,14 @@ module.exports = function(sequelize, DataTypes) {
}, function(err) {
callback(err);
});
},
afterDestroy: function(record, options, callback) {
esDocumentManager.deleteDocument(record.id, {index:'creatives', type:'creative'}).then(function(response) {
callback(null, record);
}, function(err){
callback(err);
});
}
//afterDestroy: function(record, options, callback) {
//esDocumentManager.deleteDocument(record.id, {index:'creatives', type:'creative'}).then(function(response) {
// callback(null, record);
// }, function(err){
// callback(err);
// });
// }
}
});
return Creative;
Expand Down

0 comments on commit 6ce44a9

Please sign in to comment.