第八章 广告检索系统——广告数据索引的设计与实现(1)

本文详细介绍了广告检索系统中广告数据索引的设计与实现,包括正向索引和倒排索引的概念及应用,以及推广计划、推广单元、关键词、兴趣和地域索引对象的定义和服务实现。重点讨论了全量和增量索引维护策略,确保检索服务的高效性和数据完整性。

此博客用于个人学习,来源于网上,对知识点进行一个整理。

1. 广告数据索引:

设计索引的目的就是为了加快检索的速度,将原始数据抽象,规划出合理的字段,在内存中构建广告数据索引。记住,并不是所有的数据都需要放在索引里。

1.1 广告数据索引设计:

1)正向索引:

定义:通过唯一键/主键生成与对象的映射关系。核心思想是通过一个键找到一个对象,且这种关系是确定的,即唯一键对应到唯一的对象。主要应用在推广计划,推广单元和创意中。

例子:

在这里插入图片描述
2)倒排索引:

定义:也被称为反向索引,是一种索引方法,它的设计是为了存储在全文搜索下某个单词在一个文档或一组文档中存储位置的映射。是在文档检索系统中最常用的数据结构。核心思想是通过内容去确定包含关系的对象。

例子:

在这里插入图片描述
倒排索引在广告系统中的应用:核心用途是对各个维度限制的“整理”。

1.2 广告数据索引维护:

核心思想是保证检索服务中的索引是完整的,所以采用全量索引加增量索引的方式实现。

全量索引:检索系统在启动时一次性读取当前数据库中(注意,不能直接从数据库中直接读取)的所有数据,建立索引。

增量索引:系统运行过程中,监控数据库变化,即增量,实时加载更新,构建索引。

在这里插入图片描述

2. 推广计划索引对象定义与服务实现:

首先需要意识到的一点是,并不是所有的数据库都需要建立索引对象,同样的,也并不是所有的属性都得包含在索引对象里面。

2.1 索引接口方法:

包括了索引的增删改查方法,后面定义的索引对象都要实现这个接口,来实现对应的增删改查方法。

public interface IndexAware<K,V> {

    V get(K key);

    void add(K key,V value);

    void update(K key,V value);

    void delete(K key,V value);
}

2.2 建立索引对象:

方法有了,接下来要定义的就是包含着索引字段的对象类。

@Data
@NoArgsConstructor
@AllArgsConstructor
public class AdPlanObject {

    private Long planId;
    private Long userId;
    private Integer planStatus;
    private Date startDate;
    private Date endDate;

    /**
     * 由于更新数据的时候并不一定是更新所有字段,这里是为了确定更新的字段
     * @param newObject
     */
    public void update(AdPlanObject newObject){
        if (null != newObject.getPlanId()){
            this.planId = newObject.getPlanId();
        }
        if (null != newObject.getUserId()){
            this.userId = newObject.getUserId();
        }
        if (null != newObject.getPlanStatus()) {
            this.planStatus = newObject.getPlanStatus();
        }
        if (null != newObject.getStartDate()) {
            this.startDate = newObject.getStartDate();
        }
        if (null != newObject.getEndDate()) {
            this.endDate = newObject.getEndDate();
        }
    }
}

2.3 定义索引的实现类:

需要去实现之前接口定义的方法,首先要定义一个 map 的全局变量,因为 map 就是典型的 key-value 结构,符合我们的需求。由于我们的服务会实现索引的更新,而对于索引的更新,我们需要保证的就是线程安全的,于是我们需要先静态的构造一个安全的 map 全局变量。

@Slf4j
@Component
public class AdPlanIndex implements IndexAware<Long,AdPlanObject> {

    private static Map<Long,AdPlanObject> objectMap;

    static {
        objectMap = new ConcurrentHashMap<>();
    }

    @Override
    public AdPlanObject get(Long key) {
        return objectMap.get(key);
    }

    @Override
    public void add(Long key, AdPlanObject value) {
        log.info("before add: {}", objectMap);
        objectMap.put(key,value);
        log.info("after add: {}", objectMap);
    }

    @Override
    public void update(Long key, AdPlanObject value) {
        log.info("before update: {}",objectMap);
        AdPlanObject oldObject = objectMap.get(key);
        if (null == oldObject){
            objectMap.put(key, value);
        }else {
            oldObject.update(value);
        }
        log.info("after update: {}",objectMap);
    }

    @Override
    public void delete(Long key, AdPlanObject value) {
        log.info("before delete: {}", objectMap);
        objectMap.remove(key);
        log.info("after delete: {}", objectMap);
    }
}

3. 推广单元索引对象定义与服务实现:

3.1 建立索引对象:

需要注意的是,由于推广单元与推广计划是多对一的关系,于是推广单元的索引对象内包含推广计划的索引对象。

@Data
@NoArgsConstructor
@AllArgsConstructor
public class AdUnitObject {

    private Long unitId;
    private Integer unitStatus;
    private Integer positionType;
    private Long planId;

    private AdPlanObject adPlanObject;

    void update(AdUnitObject newObject){
        if (null != newObject.getUnitId()){
            this.unitId = newObject.getUnitId();
        }
        if (null != newObject.getUnitStatus()){
            this.unitStatus = newObject.getUnitStatus();
        }
        if (null != newObject.getPositionType()){
            this.positionType = newObject.getPositionType();
        }
        if (null != newObject.getPlanId()){
            this.planId = newObject.getPlanId();
        }
        if (null != newObject.getAdPlanObject()){
            this.adPlanObject = newObject.getAdPlanObject();
        }
    }
}

3.2 定义索引的实现类:

各个方法的逻辑与推广计划一致。

@Slf4j
@Component
public class AdUnitIndex implements IndexAware<Long,AdUnitObject> {

    private static Map<Long,AdUnitObject> objectMap;

    static {
        objectMap = new ConcurrentHashMap<>();
    }

    @Override
    public AdUnitObject get(Long key) {
        return objectMap.get(key);
    }

    @Override
    public void add(Long key, AdUnitObject value) {
        log.info("before add: {}", objectMap);
        objectMap.put(key,value);
        log.info("after add: {}", objectMap);
    }

    @Override
    public void update(Long key, AdUnitObject value) {
        log.info("before update: {}",objectMap);
        AdUnitObject oldObject = objectMap.get(key);
        if (null == oldObject){
            objectMap.put(key, value);
        }else {
            oldObject.update(value);
        }
        log.info("after update: {}",objectMap);
    }

    @Override
    public void delete(Long key, AdUnitObject value) {
        log.info("before delete: {}", objectMap);
        objectMap.remove(key);
        log.info("after delete: {}", objectMap);
    }
}

4. 关键词索引对象定义与服务实现:

与其他类型不同的是,推广单元包含三种限制:关键词限制,兴趣限制和地域限制。对于这三种,我们都需要建立对应的索引对象和方法。

4.1 建立索引对象:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UnitKeywordObject {

    private Long unitId;
    private String keyword;
}

4.2 定义工具类:

为了方便我们方法的实现,需要定义一个工具类,里面实现一个方法:传进来一个 map,如果这个 map 的 key 不存在,返回一个新的 value 对象。

public class CommonUtils {

    /**
     * 传进来一个 map,如果这个 map 的 key 不存在,返回一个新的 value 对象
     * @param key
     * @param map
     * @param factory
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K,V> V getOrCreate(K key, Map<K,V> map, Supplier<V> factory){
        return map.computeIfAbsent(key,k -> factory.get());
    }
}

4.3 定义索引的实现类:

由于一个关键词可以对应很多 id,于是采用 <String,Set<Long>> 进行存储,其中需要定义两个 map 对象,分别代表从 id 到关键词和关键词到 id 的映射,同样的,实现增删改查方法时也需要对这两个 map 进行维护。其中,由于更新成本比较高,不允许更新,更新操作可以通过删除加添加功能实现。另外,定义一个方法用于匹配推广单元是否包含这些关键词。

@Slf4j
@Component
public class UnitKeywordIndex implements IndexAware<String, Set<Long>> {

    //关键词到推广单元 id 的映射
    private static Map<String,Set<Long>> keywordUnitMap;
    //推广单元 id 到关键词的映射,一个推广单元也可以设置很多个限制
    private static Map<Long,Set<String>> unitKeywordMap;

    static {
        keywordUnitMap = new ConcurrentHashMap<>();
        unitKeywordMap = new ConcurrentHashMap<>();
    }

    @Override
    public Set<Long> get(String key) {
        if (StringUtils.isEmpty(key)){
            return Collections.emptySet();
        }
        Set<Long> result = keywordUnitMap.get(key);
        if (result == null){
            return Collections.emptySet();
        }
        return result;
    }

    @Override
    public void add(String key, Set<Long> value) {
        log.info("UnitKeywordIndex,before add: {}",unitKeywordMap);
        Set<Long> unitIdSet = CommonUtils.getOrCreate(
                key,keywordUnitMap, ConcurrentSkipListSet::new
        );
        unitIdSet.addAll(value);
        for (Long unitId:value){
            Set<String> keywordSet = CommonUtils.getOrCreate(
                    unitId,unitKeywordMap,ConcurrentSkipListSet::new
            );
            keywordSet.add(key);
        }
        log.info("UnitKeywordIndex,after add: {}",unitKeywordMap);
    }

    /**
     * 由于更新成本比较高,不允许更新,更新操作可以通过删除加添加功能实现
     * @param key
     * @param value
     */
    @Override
    public void update(String key, Set<Long> value) {
        log.info("keyword index can not support update");
    }

    @Override
    public void delete(String key, Set<Long> value) {
        log.info("UnitKeywordIndex,before delete: {}",unitKeywordMap);
        Set<Long> unitIds = CommonUtils.getOrCreate(
                key,keywordUnitMap,ConcurrentSkipListSet::new
        );
        unitIds.removeAll(value);
        for (Long unitId : value){
            Set<String> keywordSet = CommonUtils.getOrCreate(
                    unitId,unitKeywordMap,ConcurrentSkipListSet::new
            );
            keywordSet.remove(key);
        }
        log.info("UnitKeywordIndex,after delete: {}",unitKeywordMap);
    }

    /**
     * 匹配推广单元是否包含这些关键词
     * @param unitId
     * @param keywords
     * @return
     */
    public boolean match(Long unitId, List<String> keywords){
        if (unitKeywordMap.containsKey(unitId) && CollectionUtils.isNotEmpty(unitKeywordMap.get(unitId))){
            Set<String> unitKeywords = unitKeywordMap.get(unitId);
            return CollectionUtils.isSubCollection(keywords,unitKeywords);
        }
        return false;
    }
}

5. 兴趣索引对象定义与服务实现:

实现逻辑与前面类似,不再赘述。

5.1 建立索引对象:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UnitItObject {

    private Long unitId;
    private String itTag;
}

5.2 定义索引的实现类:

@Slf4j
@Component
public class UnitItIndex implements IndexAware<String, Set<Long>> {

    // <itTag, adUnitId set>
    private static Map<String, Set<Long>> itUnitMap;

    // <unitId, itTag set>
    private static Map<Long, Set<String>> unitItMap;

    static {
        itUnitMap = new ConcurrentHashMap<>();
        unitItMap = new ConcurrentHashMap<>();
    }

    @Override
    public Set<Long> get(String key) {
        return itUnitMap.get(key);
    }

    @Override
    public void add(String key, Set<Long> value) {
        log.info("UnitItIndex, before add: {}", unitItMap);
        Set<Long> unitIds = CommonUtils.getOrCreate(
                key, itUnitMap, ConcurrentSkipListSet::new
        );
        unitIds.addAll(value);
        for (Long unitId : value) {
            Set<String> its = CommonUtils.getOrCreate(
                    unitId, unitItMap, ConcurrentSkipListSet::new
            );
            its.add(key);
        }
        log.info("UnitItIndex, after add: {}", unitItMap);
    }

    @Override
    public void update(String key, Set<Long> value) {
        log.error("it index can not support update");
    }

    @Override
    public void delete(String key, Set<Long> value) {
        log.info("UnitItIndex, before delete: {}", unitItMap);
        Set<Long> unitIds = CommonUtils.getOrCreate(
                key, itUnitMap, ConcurrentSkipListSet::new
        );
        unitIds.removeAll(value);
        for (Long unitId : value) {
            Set<String> itTagSet = CommonUtils.getOrCreate(
                    unitId, unitItMap, ConcurrentSkipListSet::new
            );
            itTagSet.remove(key);
        }
        log.info("UnitItIndex, after delete: {}", unitItMap);
    }

    public boolean match(Long unitId, List<String> itTags) {
        if (unitItMap.containsKey(unitId) && CollectionUtils.isNotEmpty(unitItMap.get(unitId))) {
            Set<String> unitKeywords = unitItMap.get(unitId);
            return CollectionUtils.isSubCollection(itTags, unitKeywords);
        }
        return false;
    }
}

6. 地域索引对象定义与服务实现:

6.1 建立索引对象:

考虑到存在两个 String 结构的量(省份和城市),于是采用连接在一起的方式。

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UnitDistrictObject {

    private Long unitId;
    private String province;
    private String city;

    // <String, Set<Long>>
    // province-city
}

6.2 定义索引的实现类:

@Slf4j
@Component
public class UnitDistrictIndex implements IndexAware<String, Set<Long>> {

    private static Map<String, Set<Long>> districtUnitMap;
    private static Map<Long, Set<String>> unitDistrictMap;

    static {
        districtUnitMap = new ConcurrentHashMap<>();
        unitDistrictMap = new ConcurrentHashMap<>();
    }

    @Override
    public Set<Long> get(String key) {
        return districtUnitMap.get(key);
    }

    @Override
    public void add(String key, Set<Long> value) {
        log.info("UnitDistrictIndex, before add: {}", unitDistrictMap);
        Set<Long> unitIds = CommonUtils.getOrCreate(
                key, districtUnitMap, ConcurrentSkipListSet::new
        );
        unitIds.addAll(value);
        for (Long unitId : value) {
            Set<String> districts = CommonUtils.getOrCreate(
                    unitId, unitDistrictMap, ConcurrentSkipListSet::new
            );
            districts.add(key);
        }
        log.info("UnitDistrictIndex, after add: {}", unitDistrictMap);
    }

    @Override
    public void update(String key, Set<Long> value) {
        log.error("district index can not support update");
    }

    @Override
    public void delete(String key, Set<Long> value) {
        log.info("UnitDistrictIndex, before delete: {}", unitDistrictMap);
        Set<Long> unitIds = CommonUtils.getOrCreate(
                key, districtUnitMap,
                ConcurrentSkipListSet::new
        );
        unitIds.removeAll(value);
        for (Long unitId : value) {
            Set<String> districts = CommonUtils.getOrCreate(
                    unitId, unitDistrictMap, ConcurrentSkipListSet::new
            );
            districts.remove(key);
        }
        log.info("UnitDistrictIndex, after delete: {}", unitDistrictMap);
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值