|
1 | 1 | var client = require('./elasticsearch/client');
|
2 | 2 |
|
3 | 3 | module.exports = function(placement) {
|
4 |
| - //construct should array |
5 |
| - |
6 |
| - var interestsFiltersArray = []; |
7 |
| - var interests = placement.tags; |
8 |
| - interests.forEach(function(interest){ |
9 |
| - var shouldObject = { |
10 |
| - term: { tags: interest} |
11 |
| - } |
12 |
| - interestsFiltersArray.push(shouldObject); |
13 |
| - }); |
| 4 | + //construct should array |
14 | 5 | var placementCity = (placement.city || "").toLowerCase();
|
15 | 6 | var placementCountry = (placement.country || "").toLowerCase();
|
16 |
| - return client.search({ |
| 7 | + var tags = placement.tags.join(" "); |
| 8 | + return client.search({ |
17 | 9 | index: 'creatives',
|
18 | 10 | type: 'creative',
|
19 |
| - //we only need the highest 2 creatives to calculate the price for the winning creative. |
20 |
| - body: { |
21 |
| - query: { |
22 |
| - function_score: { |
23 |
| - filter: { |
24 |
| - bool: { |
25 |
| - // each document should match ANY of the location condtions AND the ANY of the tags associated with the placement |
26 |
| - should: [ |
27 |
| - {and:[ |
28 |
| - {or:[ |
29 |
| - { |
30 |
| - and: [ |
31 |
| - {term: {targetCountry: placementCountry} }, |
32 |
| - {script: {script: "doc['targetCity'].empty"}} |
33 |
| - ] |
34 |
| - }, |
35 |
| - { |
36 |
| - and: [ |
37 |
| - { term: {targetCountry: placementCountry} }, |
38 |
| - { term: { targetCity: placementCity} } |
| 11 | + body: { |
| 12 | + // we only need the highest 2 creatives. |
| 13 | + "size": 2, |
| 14 | + "query": { |
| 15 | + "function_score": { |
| 16 | + "field_value_factor": { |
| 17 | + "field": "cpm", |
| 18 | + "factor": 10 |
| 19 | + }, |
| 20 | + "query": { |
| 21 | + "bool": { |
| 22 | + "must": { |
| 23 | + "match": { |
| 24 | + // full-text search utilized to score relevance of tags. |
| 25 | + "tags": tags |
| 26 | + } |
| 27 | + }, |
| 28 | + // a creative should match atleast one of the conditions in the should array |
| 29 | + "should": [ |
| 30 | + { |
| 31 | + // match exact city and country in placement. |
| 32 | + "constant_score": { |
| 33 | + "filter": { |
| 34 | + "and": [ |
| 35 | + {"term": {"targetCountry": placementCountry} }, |
| 36 | + {"term": {"targetCity": placementCity} } |
| 37 | + ] |
| 38 | + }, |
| 39 | + "boost": 3 |
| 40 | + } |
| 41 | + }, |
| 42 | + { |
| 43 | + // match exact country and does not target a certain city. |
| 44 | + "constant_score": { |
| 45 | + "filter": { |
| 46 | + "and": [ |
| 47 | + {"term": {"targetCountry": placementCountry}}, |
| 48 | + {"missing":{"field": "targetCity" }} |
39 | 49 | ]
|
40 | 50 | },
|
41 |
| - { |
42 |
| - script: { |
43 |
| - script: "doc['targetCountry'].empty && doc['targetOrigin'].empty" |
44 |
| - } |
45 |
| - }, |
46 |
| - { |
47 |
| - and: [ |
48 |
| - { |
49 |
| - script: { |
50 |
| - params: { "pLat": placement.latitude || 0.0, "pLon": placement.longitude || 0.0 }, |
51 |
| - script: "!doc['targetOrigin'].empty && !doc['targetRadius'].empty && doc['targetOrigin'].distanceInKm(pLat, pLon) <= doc['targetRadius'].value" |
52 |
| - } |
| 51 | + "boost": 2 |
| 52 | + } |
| 53 | + }, |
| 54 | + { |
| 55 | + // match a certain area covered by a certain radius from a certian origin. |
| 56 | + "constant_score": { |
| 57 | + "filter": { |
| 58 | + "and": [ |
| 59 | + {"missing": {"field": "targetCountry" }}, |
| 60 | + {"missing": {"field": "targetCity" }}, |
| 61 | + { |
| 62 | + "script": { |
| 63 | + "params": { |
| 64 | + "lat": placement.latitude || 0.0, |
| 65 | + "lon": placement.longitude || 0.0 |
| 66 | + }, |
| 67 | + "script": "!doc['targetOrigin'].empty && doc['targetOrigin'].distanceInKm(lat,lon) <= doc['targetRadius'].value" |
53 | 68 | }
|
| 69 | + } |
54 | 70 | ]
|
55 |
| - } |
56 |
| - ] |
| 71 | + }, |
| 72 | + "boost": 4 |
| 73 | + } |
57 | 74 | },
|
58 | 75 | {
|
59 |
| - or: interestsFiltersArray |
| 76 | + // no geolocation targeting at all |
| 77 | + "constant_score": { |
| 78 | + "filter": { |
| 79 | + "and":[ |
| 80 | + {"missing" :{"field": "targetCountry"}}, |
| 81 | + {"missing" : {"field": "targetCity"}}, |
| 82 | + {"missing" : {"field": "targetOrigin"}} |
| 83 | + ] |
| 84 | + }, |
| 85 | + "boost": 1.2 |
| 86 | + } |
60 | 87 | }
|
61 |
| - ] |
62 |
| - } |
63 |
| - ], |
64 |
| - must: [ |
65 |
| - // each document mush match the exact width and height in the placement |
66 |
| - //TODO: created standard adTypes which will reduce this to one condition only. |
67 |
| - {term: {width: placement.width}}, |
68 |
| - {term: {height: placement.height}} |
69 |
| - ] |
70 |
| - } |
71 |
| - |
72 |
| - }, |
73 |
| - functions:[ |
74 |
| - { |
75 |
| - script_score: { |
76 |
| - // the score depends on how matching is this creative with the placement, and how much does this creative cost. |
77 |
| - script: "_score*doc[\"cpm\"].value" |
78 |
| - } |
| 88 | + ], "minimum_number_should_match": 1 |
79 | 89 | }
|
80 |
| - ] |
| 90 | + } |
81 | 91 | }
|
82 | 92 | }
|
83 | 93 | }
|
84 |
| - }) |
| 94 | + }); |
85 | 95 | }
|
0 commit comments