此博客用于个人学习,来源于网上,对知识点进行一个整理。
1. 根据匹配信息实现对推广单元的再筛选:
1.1 完善 UnitDistrictIndex:
之前定义的 UnitDistrictIndex 类中并没有 match 的实现方法,需要我们去添加。
public boolean match(Long adUnitId, List<DistrictFeature.ProvinceAndCity> districts){
if (unitDistrictMap.containsKey(adUnitId) && CollectionUtils.isNotEmpty(unitDistrictMap.get(adUnitId))){
Set<String> unitDistricts = unitDistrictMap.get(adUnitId);
List<String> targetDistricts = districts.stream().map(
d -> CommonUtils.stringConcat(d.getProvince(),d.getCity())
).collect(Collectors.toList());
return CollectionUtils.isSubCollection(targetDistricts,unitDistricts);
}
return false;
}
1.2 在 SearchImpl 中实现三重过滤:
由于限制间存在 AND 或者 OR 的关系,所以需要分情况进行过滤。
@Slf4j
@Service
public class SearchImpl implements ISearch {
@Override
public SearchResponse fetchAds(SearchRequest request) {
// 请求的广告位信息
List<AdSlot> adSlots = request.getRequestInfo().getAdSlots();
// 三个 Feature
KeywordFeature keywordFeature = request.getFeatureInfo().getKeywordFeature();
DistrictFeature districtFeature = request.getFeatureInfo().getDistrictFeature();
ItFeature itFeature = request.getFeatureInfo().getItFeature();
FeatureRelation relation = request.getFeatureInfo().getRelation();
// 构造响应对象
SearchResponse response = new SearchResponse();
Map<String, List<SearchResponse.Creative>> adSlot2Ads = response.getAdSlot2Ads();
for (AdSlot adSlot : adSlots) {
Set<Long> targetUnitIdSet;
// 根据流量类型获取初始 AdUnit
Set<Long> adUnitIdSet = DataTable.of(
AdUnitIndex.class
).match(adSlot.getPositionType());
if (relation == FeatureRelation.AND){
//进行三重过滤
filterKeywordFeature(adUnitIdSet, keywordFeature);
filterDistrictFeature(adUnitIdSet, districtFeature);
filterItTagFeature(adUnitIdSet, itFeature);
targetUnitIdSet = adUnitIdSet;
}else {
targetUnitIdSet = getORRelationUnitIds(adUnitIdSet,keywordFeature,districtFeature,itFeature);
}
}
return null;
}
private Set<Long> getORRelationUnitIds(Set<Long> adUnitIdSet, KeywordFeature keywordFeature, DistrictFeature districtFeature, ItFeature itFeature) {
if (CollectionUtils.isEmpty(adUnitIdSet)) {
return Collections.emptySet();
}
Set<Long> keywordUnitIdSet = new HashSet<>(adUnitIdSet);
Set<Long> districtUnitIdSet = new HashSet<>(adUnitIdSet);
Set<Long> itUnitIdSet