Springcloud查詢返回廣告創(chuàng)意實例代碼

根據(jù)三個維度繼續(xù)過濾

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:國際域名空間、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、豐寧網(wǎng)站維護(hù)、網(wǎng)站推廣。

在上一節(jié)中我們實現(xiàn)了根據(jù)流量信息過濾的代碼,但是我們的條件有可能是多條件一起傳給我們的檢索服務(wù)的,本節(jié)我們繼續(xù)實現(xiàn)根據(jù)推廣單元的三個維度條件的過濾。

在SearchImpl類中添加過濾方法

public class SearchImpl implements ISearch {
  @Override
  public SearchResponse fetchAds(SearchRequest request) {
    ...
      // 根據(jù)三個維度過濾
      if (featureRelation == FeatureRelation.AND) {
        filterKeywordFeature(adUnitIdSet, keywordFeature);
        filterHobbyFeature(adUnitIdSet, hobbyFeatrue);
        filterDistrictFeature(adUnitIdSet, districtFeature);

        targetUnitIdSet = adUnitIdSet;
      } else {
        getOrRelationUnitIds(adUnitIdSet, keywordFeature, hobbyFeatrue, districtFeature);
      }
    }
    return null;
  }

定義三個方法實現(xiàn)過濾

/**
   * 獲取三個維度各自滿足時的廣告id
   */
  private Set<Long> getOrRelationUnitIds(Set<Long> adUnitIdsSet,
                      KeywordFeature keywordFeature,
                      HobbyFeatrue hobbyFeatrue,
                      DistrictFeature districtFeature) {
    if (CollectionUtils.isEmpty(adUnitIdsSet)) return Collections.EMPTY_SET;

    // 我們在處理的時候,需要對副本進(jìn)行處理,大家可以考慮一下為什么需要這么做?
    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
        )
    );
  }

  /**
   * 根據(jù)傳遞的關(guān)鍵詞過濾
   */
  private void filterKeywordFeature(Collection<Long> adUnitIds, KeywordFeature keywordFeature) {
    if (CollectionUtils.isEmpty(adUnitIds)) return;
    if (CollectionUtils.isNotEmpty(keywordFeature.getKeywords())) {
      // 如果存在需要過濾的關(guān)鍵詞,查找索引實例對象進(jìn)行過濾處理
      CollectionUtils.filter(
          adUnitIds,
          adUnitId -> IndexDataTableUtils.of(UnitKeywordIndexAwareImpl.class)
                          .match(adUnitId, keywordFeature.getKeywords())
      );
    }
  }

  /**
   * 根據(jù)傳遞的興趣信息過濾
   */
  private void filterHobbyFeature(Collection<Long> adUnitIds, HobbyFeatrue hobbyFeatrue) {
    if (CollectionUtils.isEmpty(adUnitIds)) return;
    // 如果存在需要過濾的興趣,查找索引實例對象進(jìn)行過濾處理
    if (CollectionUtils.isNotEmpty(hobbyFeatrue.getHobbys())) {
      CollectionUtils.filter(
          adUnitIds,
          adUnitId -> IndexDataTableUtils.of(UnitHobbyIndexAwareImpl.class)
                          .match(adUnitId, hobbyFeatrue.getHobbys())
      );
    }
  }

  /**
   * 根據(jù)傳遞的地域信息過濾
   */
  private void filterDistrictFeature(Collection<Long> adUnitIds, DistrictFeature districtFeature) {
    if (CollectionUtils.isEmpty(adUnitIds)) return;
    // 如果存在需要過濾的地域信息,查找索引實例對象進(jìn)行過濾處理
    if (CollectionUtils.isNotEmpty(districtFeature.getProvinceAndCities())) {
      CollectionUtils.filter(
          adUnitIds,
          adUnitId -> {
            return IndexDataTableUtils.of(UnitDistrictIndexAwareImpl.class)
                         .match(adUnitId, districtFeature.getProvinceAndCities());
          }
      );
    }
  }

根據(jù)推廣單元id獲取推廣創(chuàng)意

我們知道,推廣單元和推廣創(chuàng)意的關(guān)系是多對多,從上文我們查詢到了推廣單元ids,接下來我們實現(xiàn)根據(jù)推廣單元id獲取推廣創(chuàng)意的代碼,let's code.

首先,我們需要在com.sxzhongf.ad.index.creative_relation_unit.CreativeRelationUnitIndexAwareImpl 關(guān)聯(lián)索引中查到推廣創(chuàng)意的ids

/**
   * 通過推廣單元id獲取推廣創(chuàng)意id
   */
  public List<Long> selectAdCreativeIds(List<AdUnitIndexObject> unitIndexObjects) {
    if (CollectionUtils.isEmpty(unitIndexObjects)) return Collections.emptyList();

    //獲取要返回的廣告創(chuàng)意ids
    List<Long> result = new ArrayList<>();
    for (AdUnitIndexObject unitIndexObject : unitIndexObjects) {
      //根據(jù)推廣單元id獲取推廣創(chuàng)意
      Set<Long> adCreativeIds = unitRelationCreativeMap.get(unitIndexObject.getUnitId());
      if (CollectionUtils.isNotEmpty(adCreativeIds)) result.addAll(adCreativeIds);
    }

    return result;
  }

然后得到了推廣創(chuàng)意的id list后,我們在創(chuàng)意索引實現(xiàn)類com.sxzhongf.ad.index.creative.CreativeIndexAwareImpl中定義根據(jù)ids查詢創(chuàng)意的方法。

/**
 * 根據(jù)ids獲取創(chuàng)意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;
}

自此,我們已經(jīng)得到了想要的推廣單元和推廣創(chuàng)意,因為推廣單元包含了推廣計劃,所以我們想要的數(shù)據(jù)已經(jīng)全部可以獲取到了,接下來,我們還得過濾一次當(dāng)前我們查詢到的數(shù)據(jù)的狀態(tài),因為有的數(shù)據(jù),我們可能已經(jīng)進(jìn)行過邏輯刪除了,因此還需要判斷獲取的數(shù)據(jù)是否有效。在SearchImpl類中實現(xiàn)。

 /**
  * 根據(jù)狀態(tài)信息過濾數(shù)據(jù)
  */
 private void filterAdUnitAndPlanStatus(List<AdUnitIndexObject> unitIndexObjects, CommonStatus status) {
   if (CollectionUtils.isEmpty(unitIndexObjects)) return;

   //同時判斷推廣單元和推廣計劃的狀態(tài)
   CollectionUtils.filter(
       unitIndexObjects,
       unitIndexObject -> unitIndexObject.getUnitStatus().equals(status.getStatus()) &&
           unitIndexObject.getAdPlanIndexObject().getPlanStatus().equals(status.getStatus())
   );
 }

在SearchImpl中我們實現(xiàn)廣告創(chuàng)意的查詢.

...

//獲取 推廣計劃 對象list
List<AdUnitIndexObject> unitIndexObjects = IndexDataTableUtils.of(AdUnitIndexAwareImpl.class).fetch(adUnitIdSet);
//根據(jù)狀態(tài)過濾數(shù)據(jù)
filterAdUnitAndPlanStatus(unitIndexObjects, CommonStatus.VALID);
//獲取 推廣創(chuàng)意 id list
List<Long> creativeIds = IndexDataTableUtils.of(CreativeRelationUnitIndexAwareImpl.class)
                      .selectAdCreativeIds(unitIndexObjects);
//根據(jù) 推廣創(chuàng)意ids獲取推廣創(chuàng)意
List<CreativeIndexObject> creativeIndexObjects = IndexDataTableUtils.of(CreativeIndexAwareImpl.class)
...

根據(jù)廣告位adslot 實現(xiàn)對創(chuàng)意數(shù)據(jù)的過濾

因為我們的廣告位是有不同的大小,不同的類型,因此,我們在獲取到所有符合我們查詢維度以及流量類型的條件后,還需要針對不同的廣告位來展示不同的廣告創(chuàng)意信息。

/**
* 根據(jù)廣告位類型以及參數(shù)獲取展示的合適廣告信息
*
* @param creativeIndexObjects 所有廣告創(chuàng)意
* @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 -> {
       //審核狀態(tài)必須是通過
       return creative.getAuditStatus().equals(CommonStatus.VALID.getStatus())
           && creative.getWidth().equals(width)
           && creative.getHeight().equals(height)
           && type.contains(creative.getType());
     }
 );
}

組建搜索返回對象

正常業(yè)務(wù)場景中,同一個廣告位可以展示多個廣告信息,也可以只展示一個廣告信息,這個需要根據(jù)具體的業(yè)務(wù)場景來做不同的處理,本次為了演示方便,會從返回的創(chuàng)意列表中隨機選擇一個創(chuàng)意廣告信息進(jìn)行展示,當(dāng)然大家也可以根據(jù)業(yè)務(wù)類型,設(shè)置不同的優(yōu)先級或者權(quán)重值來進(jìn)行廣告選擇。

/**
 * 從創(chuàng)意列表中隨機獲取一條創(chuàng)意廣告返回出去
 *
 * @param creativeIndexObjects 創(chuàng)意廣告list
 */
private List<SearchResponse.Creative> buildCreativeResponse(List<CreativeIndexObject> creativeIndexObjects) {
  if (CollectionUtils.isEmpty(creativeIndexObjects)) return Collections.EMPTY_LIST;

  //隨機獲取一個廣告創(chuàng)意,也可以實現(xiàn)優(yōu)先級排序,也可以根據(jù)權(quán)重值等等,具體根據(jù)業(yè)務(wù)
  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)
  );
}

完整的請求過濾實現(xiàn)方法:

@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關(guān)系
    FeatureRelation featureRelation = request.getFeatureInfo().getRelation();

    //構(gòu)造響應(yīng)對象
    SearchResponse response = new SearchResponse();
    Map<String, List<SearchResponse.Creative>> adSlotRelationAds = response.getAdSlotRelationAds();

    for (AdSlot adSlot : adSlotList) {
      Set<Long> targetUnitIdSet;
      //根據(jù)流量類型從緩存中獲取 初始 廣告信息
      Set<Long> adUnitIdSet = IndexDataTableUtils.of(
          AdUnitIndexAwareImpl.class
      ).match(adSlot.getPositionType());

      // 根據(jù)三個維度過濾
      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);
      //根據(jù)狀態(tài)過濾數(shù)據(jù)
      filterAdUnitAndPlanStatus(unitIndexObjects, CommonStatus.VALID);

      //獲取 推廣創(chuàng)意 id list
      List<Long> creativeIds = IndexDataTableUtils.of(CreativeRelationUnitIndexAwareImpl.class)
                            .selectAdCreativeIds(unitIndexObjects);
      //根據(jù) 推廣創(chuàng)意ids獲取推廣創(chuàng)意
      List<CreativeIndexObject> creativeIndexObjects = IndexDataTableUtils.of(CreativeIndexAwareImpl.class)
                                        .fetch(creativeIds);

      //根據(jù) 廣告位adslot 實現(xiàn)對創(chuàng)意數(shù)據(jù)的過濾
      filterCreativeByAdSlot(creativeIndexObjects, adSlot.getWidth(), adSlot.getHeight(), adSlot.getType());

      //一個廣告位可以展示多個廣告,也可以僅展示一個廣告,具體根據(jù)業(yè)務(wù)來定
      adSlotRelationAds.put(
          adSlot.getAdSlotCode(),
          buildCreativeResponse(creativeIndexObjects)
      );
    }

    return response;
  }
  ...

檢索服務(wù)對外提供

暴露API接口
上文中,我們實現(xiàn)了檢索服務(wù)的核心邏輯,接下來,我們需要對外暴露我們的廣告檢索服務(wù)接口,在SearchController中提供:

@PostMapping("/fetchAd")
  public SearchResponse fetchAdCreative(@RequestBody SearchRequest request) {
    log.info("ad-serach: fetchAd ->{}", JSON.toJSONString(request));
    return search.fetchAds(request);
  }

實現(xiàn)API網(wǎng)關(guān)配置

zuul:
routes:
  sponsor: #在路由中自定義服務(wù)路由名稱
  path: /ad-sponsor/**
  serviceId: mscx-ad-sponsor #微服務(wù)name
  strip-prefix: false
  search: #在路由中自定義服務(wù)路由名稱
  path: /ad-search/**
  serviceId: mscx-ad-search #微服務(wù)name
  strip-prefix: false
prefix: /gateway/api
strip-prefix: true #不對 prefix: /gateway/api 設(shè)置的路徑進(jìn)行截取,默認(rèn)轉(zhuǎn)發(fā)會截取掉配置的前綴

以上就是本次分享的全部知識點內(nèi)容,感謝大家對創(chuàng)新互聯(lián)的支持

網(wǎng)頁標(biāo)題:Springcloud查詢返回廣告創(chuàng)意實例代碼
轉(zhuǎn)載注明:http://muchs.cn/article42/gedeec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、定制開發(fā)、微信公眾號、自適應(yīng)網(wǎng)站網(wǎng)站建設(shè)、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計