-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
creatives scores are boosted on matching more tags and on matching ge…
- Loading branch information
Youssef Hossam
committed
May 1, 2015
1 parent
8bfaf82
commit 8458c70
Showing
2 changed files
with
84 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters