大发uu快3_uu快3彩票邀请码_大发uu快3彩票邀请码

[Spring cloud 一步步实现广告系统] 18. 查询返回广告创意

时间:2019-09-11 10:41:45 出处:大发uu快3_uu快3彩票邀请码_大发uu快3彩票邀请码
根据另一三个小 维度继续过滤

在上一节中其他同学 实现了根据流量信息过滤的代码,你要其他同学 的条件有机会是多条件一块儿传给其他同学 的检索服务的,本节其他同学 继续实现根据推广单元的另一三个小 维度条件的过滤。

  • SearchImpl类中加在过滤妙招
public class SearchImpl implements ISearch {
    @Override
    public SearchResponse fetchAds(SearchRequest request) {
        ...
            // 根据另一三个小


维度过滤
            if (featureRelation == FeatureRelation.AND) {
                filterKeywordFeature(adUnitIdSet, keywordFeature);
                filterHobbyFeature(adUnitIdSet, hobbyFeatrue);
                filterDistrictFeature(adUnitIdSet, districtFeature);

                targetUnitIdSet = adUnitIdSet;
            } else {
                getOrRelationUnitIds(adUnitIdSet, keywordFeature, hobbyFeatrue, districtFeature);
            }
        }
        return null;
    }
  • 定义另一三个小 妙招实现过滤
/**
     * 获取另一三个小


维度各人
满足时的广告id
     */
    private Set<Long> getOrRelationUnitIds(Set<Long> adUnitIdsSet,
                                           KeywordFeature keywordFeature,
                                           HobbyFeatrue hobbyFeatrue,
                                           DistrictFeature districtFeature) {
        if (CollectionUtils.isEmpty(adUnitIdsSet)) return Collections.EMPTY_SET;

        // 其他同学

在解决的以前,前要对副本进行解决,其他同学

上能



考虑一下为哪此前要越来越做?
        Set<Long> keywordUnitIdSet = new HashSet<>(adUnitIdsSet);
        Set<Long> hobbyUnitIdSet = new HashSet<>(adUnitIdsSet);
        Set<Long> districtUnitIdSet = new HashSet<>(adUnitIdsSet);

        filterKeywordFeature(keywordUnitIdSet, keywordFeature);
        filterHobbyFeature(hobbyUnitIdSet, hobbyFeatrue);
        filterDistrictFeature(districtUnitIdSet, districtFeature);

        // 返回它们的并集
        return new HashSet<>(
                CollectionUtils.union(
                        CollectionUtils.union(keywordUnitIdSet, hobbyUnitIdSet),
                        districtUnitIdSet
                )
        );
    }

    /**
     * 根据传递的关键词过滤
     */
    private void filterKeywordFeature(Collection<Long> adUnitIds, KeywordFeature keywordFeature) {
        if (CollectionUtils.isEmpty(adUnitIds)) return;
        if (CollectionUtils.isNotEmpty(keywordFeature.getKeywords())) {
            // 机会指在前要过滤的关键词,查找索引实例对象进行过滤解决
            CollectionUtils.filter(
                    adUnitIds,
                    adUnitId -> IndexDataTableUtils.of(UnitKeywordIndexAwareImpl.class)
                                                   .match(adUnitId, keywordFeature.getKeywords())
            );
        }
    }

    /**
     * 根据传递的兴趣信息过滤
     */
    private void filterHobbyFeature(Collection<Long> adUnitIds, HobbyFeatrue hobbyFeatrue) {
        if (CollectionUtils.isEmpty(adUnitIds)) return;
        // 机会指在前要过滤的兴趣,查找索引实例对象进行过滤解决
        if (CollectionUtils.isNotEmpty(hobbyFeatrue.getHobbys())) {
            CollectionUtils.filter(
                    adUnitIds,
                    adUnitId -> IndexDataTableUtils.of(UnitHobbyIndexAwareImpl.class)
                                                   .match(adUnitId, hobbyFeatrue.getHobbys())
            );
        }
    }

    /**
     * 根据传递的地域信息过滤
     */
    private void filterDistrictFeature(Collection<Long> adUnitIds, DistrictFeature districtFeature) {
        if (CollectionUtils.isEmpty(adUnitIds)) return;
        // 机会指在前要过滤的地域信息,查找索引实例对象进行过滤解决
        if (CollectionUtils.isNotEmpty(districtFeature.getProvinceAndCities())) {
            CollectionUtils.filter(
                    adUnitIds,
                    adUnitId -> {
                        return IndexDataTableUtils.of(UnitDistrictIndexAwareImpl.class)
                                                  .match(adUnitId, districtFeature.getProvinceAndCities());
                    }
            );
        }
    }
根据推广单元id获取推广创意

其他同学 知道,推广单元和推广创意的关系是多对多,从上文其他同学 查询到了推广单元ids,接下来其他同学 实现根据推广单元id获取推广创意的代码,let's code.

首先,其他同学 前要在com.sxzhongf.ad.index.creative_relation_unit.CreativeRelationUnitIndexAwareImpl 关联索引中查到推广创意的ids

 /**
     * 通过推广单元id获取推广创意id
     */
    public List<Long> selectAdCreativeIds(List<AdUnitIndexObject> unitIndexObjects) {
        if (CollectionUtils.isEmpty(unitIndexObjects)) return Collections.emptyList();

        //获取要返回的广告创意ids
        List<Long> result = new ArrayList<>();
        for (AdUnitIndexObject unitIndexObject : unitIndexObjects) {
            //根据推广单元id获取推广创意
            Set<Long> adCreativeIds = unitRelationCreativeMap.get(unitIndexObject.getUnitId());
            if (CollectionUtils.isNotEmpty(adCreativeIds)) result.addAll(adCreativeIds);
        }

        return result;
    }

你要得到了推广创意的id list后,其他同学 在创意索引实现类com.sxzhongf.ad.index.creative.CreativeIndexAwareImpl中定义根据ids查询创意的妙招。

/**
 * 根据ids获取创意list
 */
public List<CreativeIndexObject> findAllByIds(Collection<Long> ids) {
    if (CollectionUtils.isEmpty(ids)) return Collections.emptyList();
    List<CreativeIndexObject> result = new ArrayList<>();

    for (Long id : ids) {
        CreativeIndexObject object = get(id);
        if (null != object)
            result.add(object);
    }

    return result;
}

自此,其他同学 机会得到了你要的推广单元和推广创意,机会推广单元包含了推广计划,并且有其他同学 你要的数据机会完整版上能 获取到了,接下来,其他同学 还得过滤一次当前其他同学 查询到的数据的情形,机会有的数据,其他同学 机会机会进行过逻辑删除了,你要还前要判断获取的数据与否有效。在SearchImpl类中实现。

  /**
   * 根据情形信息过滤数据
   */
  private void filterAdUnitAndPlanStatus(List<AdUnitIndexObject> unitIndexObjects, CommonStatus status) {
      if (CollectionUtils.isEmpty(unitIndexObjects)) return;

      //一块儿判断推广单元和推广计划的情形
      CollectionUtils.filter(
              unitIndexObjects,
              unitIndexObject -> unitIndexObject.getUnitStatus().equals(status.getStatus()) &&
                      unitIndexObject.getAdPlanIndexObject().getPlanStatus().equals(status.getStatus())
      );
  }

SearchImpl中其他同学 实现广告创意的查询.

...

//获取 推广计划 对象list
List<AdUnitIndexObject> unitIndexObjects = IndexDataTableUtils.of(AdUnitIndexAwareImpl.class).fetch(adUnitIdSet);
//根据情形过滤数据
filterAdUnitAndPlanStatus(unitIndexObjects, CommonStatus.VALID);
//获取 推广创意 id list
List<Long> creativeIds = IndexDataTableUtils.of(CreativeRelationUnitIndexAwareImpl.class)
                                            .selectAdCreativeIds(unitIndexObjects);
//根据 推广创意ids获取推广创意
List<CreativeIndexObject> creativeIndexObjects = IndexDataTableUtils.of(CreativeIndexAwareImpl.class)
...
根据广告位adslot 实现对创意数据的过滤

机会其他同学 的广告位是有不同的大小,不同的类型,你要,其他同学 在获取到所有符合其他同学 查询维度以及流量类型的条件后,还前要针对不同的广告位来展示不同的广告创意信息。

/**
* 根据广告位类型以及参数获取展示的大概广告信息
*
* @param creativeIndexObjects 所有广告创意
* @param width                广告位width
* @param height               广告位height
*/
private void filterCreativeByAdSlot(List<CreativeIndexObject> creativeIndexObjects,
                                  Integer width,
                                  Integer height,
                                  List<Integer> type) {
  if (CollectionUtils.isEmpty(creativeIndexObjects)) return;

  CollectionUtils.filter(
          creativeIndexObjects,
          creative -> {
              //审核情形前并且通过
              return creative.getAuditStatus().equals(CommonStatus.VALID.getStatus())
                      && creative.getWidth().equals(width)
                      && creative.getHeight().equals(height)
                      && type.contains(creative.getType());
          }
  );
}
  • 组建搜索返回对象

    正常业务场景中,同另一三个小 广告位上能 展示多个广告信息,也上能 只展示另一三个小 广告信息,这种前要根据具体的业务场景来做不同的解决,本次为了演示方便,会从返回的创意列表中随机选取另一三个小 创意广告信息进行展示,当然其他同学 也上能 根据业务类型,设置不同的优先级机会权重值来进行广告选取。
/**
 * 从创意列表中随机获取第两根创意广告返回出去
 *
 * @param creativeIndexObjects 创意广告list
 */
private List<SearchResponse.Creative> buildCreativeResponse(List<CreativeIndexObject> creativeIndexObjects) {
    if (CollectionUtils.isEmpty(creativeIndexObjects)) return Collections.EMPTY_LIST;

    //随机获取另一三个小



广告创意,也上能



实现优先级排序,也上能



根据权重值等等,具体根据业务
    CreativeIndexObject randomObject = creativeIndexObjects.get(
            Math.abs(new Random().nextInt()) % creativeIndexObjects.size()
    );
    //List<SearchResponse.Creative> result = new ArrayList<>();
    //result.add(SearchResponse.convert(randomObject));

    return Collections.singletonList(
            SearchResponse.convert(randomObject)
    );
}

完整版的请求过滤实现妙招:

@Service
@Slf4j
public class SearchImpl implements ISearch {
    @Override
    public SearchResponse fetchAds(SearchRequest request) {

        //获取请求广告位信息
        List<AdSlot> adSlotList = request.getRequestInfo().getAdSlots();

        //获取另一三个小


Feature信息
        KeywordFeature keywordFeature = request.getFeatureInfo().getKeywordFeature();
        HobbyFeatrue hobbyFeatrue = request.getFeatureInfo().getHobbyFeatrue();
        DistrictFeature districtFeature = request.getFeatureInfo().getDistrictFeature();
        //Feature关系
        FeatureRelation featureRelation = request.getFeatureInfo().getRelation();


        //构造响应对象
        SearchResponse response = new SearchResponse();
        Map<String, List<SearchResponse.Creative>> adSlotRelationAds = response.getAdSlotRelationAds();

        for (AdSlot adSlot : adSlotList) {
            Set<Long> targetUnitIdSet;
            //根据流量类型从缓存中获取 初始 广告信息
            Set<Long> adUnitIdSet = IndexDataTableUtils.of(
                    AdUnitIndexAwareImpl.class
            ).match(adSlot.getPositionType());

            // 根据另一三个小


维度过滤
            if (featureRelation == FeatureRelation.AND) {
                filterKeywordFeature(adUnitIdSet, keywordFeature);
                filterHobbyFeature(adUnitIdSet, hobbyFeatrue);
                filterDistrictFeature(adUnitIdSet, districtFeature);

                targetUnitIdSet = adUnitIdSet;
            } else {
                targetUnitIdSet = getOrRelationUnitIds(adUnitIdSet, keywordFeature, hobbyFeatrue, districtFeature);
            }
            //获取 推广计划 对象list
            List<AdUnitIndexObject> unitIndexObjects = IndexDataTableUtils.of(AdUnitIndexAwareImpl.class)
                                                                          .fetch(targetUnitIdSet);
            //根据情形过滤数据
            filterAdUnitAndPlanStatus(unitIndexObjects, CommonStatus.VALID);

            //获取 推广创意 id list
            List<Long> creativeIds = IndexDataTableUtils.of(CreativeRelationUnitIndexAwareImpl.class)
                                                        .selectAdCreativeIds(unitIndexObjects);
            //根据 推广创意ids获取推广创意
            List<CreativeIndexObject> creativeIndexObjects = IndexDataTableUtils.of(CreativeIndexAwareImpl.class)
                                                                                .fetch(creativeIds);

            //根据 广告位adslot 实现对创意数据的过滤
            filterCreativeByAdSlot(creativeIndexObjects, adSlot.getWidth(), adSlot.getHeight(), adSlot.getType());

            //另一三个小



广告位上能



展示多个广告,也上能



仅展示另一三个小



广告,具体根据业务来定
            adSlotRelationAds.put(
                    adSlot.getAdSlotCode(),
                    buildCreativeResponse(creativeIndexObjects)
            );
        }

        return response;
    }
    ...
检索服务对外提供
  • 暴露API接口

    上文中,其他同学 实现了检索服务的核心逻辑,接下来,其他同学 前要对外暴露其他同学 的广告检索服务接口,在SearchController中提供:

        @PostMapping("/fetchAd")
        public SearchResponse fetchAdCreative(@RequestBody SearchRequest request) {
            log.info("ad-serach: fetchAd ->{}", JSON.toJSONString(request));
            return search.fetchAds(request);
        }
  • 实现API网关配置

    zuul:
    routes:
        sponsor: #在路由中自定义服务路由名称
        path: /ad-sponsor/**
        serviceId: mscx-ad-sponsor #微服务name
        strip-prefix: false
        search: #在路由中自定义服务路由名称
        path: /ad-search/**
        serviceId: mscx-ad-search #微服务name
        strip-prefix: false
    prefix: /gateway/api
    strip-prefix: true #不对 prefix: /gateway/api 设置的路径进行截取,默认转发会截取掉配置的前缀

热门

热门标签