|  | @@ -1,137 +0,0 @@
 | 
											
												
													
														|  | -package com.tzld.piaoquan.recommend.server.framework.candidiate;
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -import com.google.common.collect.Lists;
 |  | 
 | 
											
												
													
														|  | -import com.typesafe.config.Config;
 |  | 
 | 
											
												
													
														|  | -import org.apache.commons.lang3.StringUtils;
 |  | 
 | 
											
												
													
														|  | -import org.apache.commons.lang3.tuple.Pair;
 |  | 
 | 
											
												
													
														|  | -import org.slf4j.Logger;
 |  | 
 | 
											
												
													
														|  | -import org.slf4j.LoggerFactory;
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -import java.util.List;
 |  | 
 | 
											
												
													
														|  | -import java.util.Map;
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -/**
 |  | 
 | 
											
												
													
														|  | - * 描述检索的key
 |  | 
 | 
											
												
													
														|  | - */
 |  | 
 | 
											
												
													
														|  | -public class IndexDescription {
 |  | 
 | 
											
												
													
														|  | -    public static Logger LOGGER = LoggerFactory.getLogger(IndexDescription.class);
 |  | 
 | 
											
												
													
														|  | -    public String name;
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -    public int recallWeight;
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -    /**
 |  | 
 | 
											
												
													
														|  | -     * redis,knn
 |  | 
 | 
											
												
													
														|  | -     */
 |  | 
 | 
											
												
													
														|  | -    public String db;
 |  | 
 | 
											
												
													
														|  | -    public String itemType;
 |  | 
 | 
											
												
													
														|  | -    public String indexName;
 |  | 
 | 
											
												
													
														|  | -    public String keyType;
 |  | 
 | 
											
												
													
														|  | -    public String key;
 |  | 
 | 
											
												
													
														|  | -    public List<Pair<String, String>> matches;
 |  | 
 | 
											
												
													
														|  | -    public String ordering;
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -    /**
 |  | 
 | 
											
												
													
														|  | -     * 处理类似item_cf:when=day:score=click 的情况,提取出
 |  | 
 | 
											
												
													
														|  | -     * item_cf, when->day
 |  | 
 | 
											
												
													
														|  | -     */
 |  | 
 | 
											
												
													
														|  | -    public void processIndexName() {
 |  | 
 | 
											
												
													
														|  | -        if (indexName.contains(":")) {
 |  | 
 | 
											
												
													
														|  | -            String[] indics = indexName.split(":");
 |  | 
 | 
											
												
													
														|  | -            indexName = indics[0];
 |  | 
 | 
											
												
													
														|  | -            if (indics.length > 1) {
 |  | 
 | 
											
												
													
														|  | -                matches = Lists.newArrayList();
 |  | 
 | 
											
												
													
														|  | -                for (int i = 1; i < indics.length; ++i) {
 |  | 
 | 
											
												
													
														|  | -                    String[] pair = indics[i].split("=");
 |  | 
 | 
											
												
													
														|  | -                    if (pair.length == 2) {
 |  | 
 | 
											
												
													
														|  | -                        matches.add(Pair.of(pair[0], pair[1]));
 |  | 
 | 
											
												
													
														|  | -                    } else {
 |  | 
 | 
											
												
													
														|  | -                        LOGGER.error("index name [{}] match failed", indics[i]);
 |  | 
 | 
											
												
													
														|  | -                    }
 |  | 
 | 
											
												
													
														|  | -                }
 |  | 
 | 
											
												
													
														|  | -            }
 |  | 
 | 
											
												
													
														|  | -        }
 |  | 
 | 
											
												
													
														|  | -    }
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -    /**
 |  | 
 | 
											
												
													
														|  | -     * 根据当前indexdesc的字段信息,生成candidate,用于后续召回
 |  | 
 | 
											
												
													
														|  | -     * @param queue
 |  | 
 | 
											
												
													
														|  | -     * @param indexDescription
 |  | 
 | 
											
												
													
														|  | -     * @return
 |  | 
 | 
											
												
													
														|  | -     */
 |  | 
 | 
											
												
													
														|  | -    public static Candidate convertToCandidate(String queue, IndexDescription indexDescription) {
 |  | 
 | 
											
												
													
														|  | -        Candidate candidate = new Candidate();
 |  | 
 | 
											
												
													
														|  | -        indexDescription.processIndexName();
 |  | 
 | 
											
												
													
														|  | -        QueueName queueName;
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -        //knn类召回,不需要type
 |  | 
 | 
											
												
													
														|  | -        if ("knn".equalsIgnoreCase(indexDescription.keyType)) {
 |  | 
 | 
											
												
													
														|  | -            queueName = new QueueName(indexDescription.itemType, indexDescription.ordering);
 |  | 
 | 
											
												
													
														|  | -        } else {
 |  | 
 | 
											
												
													
														|  | -            queueName = new QueueName(indexDescription.itemType, indexDescription.ordering)
 |  | 
 | 
											
												
													
														|  | -                    .addMatch("type", indexDescription.indexName);
 |  | 
 | 
											
												
													
														|  | -        }
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -        if (indexDescription.matches != null && !indexDescription.matches.isEmpty()) {
 |  | 
 | 
											
												
													
														|  | -            for (Map.Entry<String, String> entry : indexDescription.matches) {
 |  | 
 | 
											
												
													
														|  | -                queueName.addMatch(entry.getKey(), entry.getValue());
 |  | 
 | 
											
												
													
														|  | -            }
 |  | 
 | 
											
												
													
														|  | -        }
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -        //添加类似tag->范冰冰 这类索引
 |  | 
 | 
											
												
													
														|  | -        if (StringUtils.isNotBlank(indexDescription.keyType)) {
 |  | 
 | 
											
												
													
														|  | -            queueName.addMatch(indexDescription.keyType, indexDescription.key);
 |  | 
 | 
											
												
													
														|  | -        }
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -        candidate.setCandidateKey(queueName.toString());
 |  | 
 | 
											
												
													
														|  | -        candidate.setCandidateNum(indexDescription.recallWeight);
 |  | 
 | 
											
												
													
														|  | -        candidate.setMergeQueueNum(indexDescription.recallWeight);
 |  | 
 | 
											
												
													
														|  | -        candidate.setCandidateQueueName(queueName);
 |  | 
 | 
											
												
													
														|  | -        candidate.setMergeQueueName(queue);
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -        return candidate;
 |  | 
 | 
											
												
													
														|  | -    }
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -    public static final String RECALL_WEIGHT = "recall-weight";
 |  | 
 | 
											
												
													
														|  | -    public static final String INDEX_DESC = "index-desc";
 |  | 
 | 
											
												
													
														|  | -    public static final String DB = "db"; // source
 |  | 
 | 
											
												
													
														|  | -    public static final String ITEM_TYPE = "item-type";
 |  | 
 | 
											
												
													
														|  | -    public static final String NAME = "name";
 |  | 
 | 
											
												
													
														|  | -    public static final String KEY_TYPE = "key-type";
 |  | 
 | 
											
												
													
														|  | -    public static final String KEY = "key";
 |  | 
 | 
											
												
													
														|  | -    public static final String ORDERING = "ordering";
 |  | 
 | 
											
												
													
														|  | -    public static final String MATCHES = "matches";
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -    public static IndexDescription parseIndexDescription(String name, Config conf) {
 |  | 
 | 
											
												
													
														|  | -        if (!conf.hasPath(RECALL_WEIGHT) || !conf.hasPath(INDEX_DESC)) {
 |  | 
 | 
											
												
													
														|  | -            LOGGER.error("name=" + name + ",必须输入recall-weight和indexdesc");
 |  | 
 | 
											
												
													
														|  | -            return null;
 |  | 
 | 
											
												
													
														|  | -        }
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -        Config desc = conf.getConfig(INDEX_DESC);
 |  | 
 | 
											
												
													
														|  | -        if (!desc.hasPath(DB) || !desc.hasPath(ITEM_TYPE) || !desc.hasPath(NAME) || !desc.hasPath(ORDERING)) {
 |  | 
 | 
											
												
													
														|  | -            LOGGER.error("name=" + name + ",必须输入recall-weight和indexdesc");
 |  | 
 | 
											
												
													
														|  | -            return null;
 |  | 
 | 
											
												
													
														|  | -        }
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -        IndexDescription indexDescription = new IndexDescription();
 |  | 
 | 
											
												
													
														|  | -        indexDescription.name = name;
 |  | 
 | 
											
												
													
														|  | -        indexDescription.recallWeight = conf.getInt(RECALL_WEIGHT);
 |  | 
 | 
											
												
													
														|  | -        indexDescription.db = desc.getString(DB);
 |  | 
 | 
											
												
													
														|  | -        indexDescription.itemType = desc.getString(ITEM_TYPE);
 |  | 
 | 
											
												
													
														|  | -        indexDescription.indexName = desc.getString(NAME);
 |  | 
 | 
											
												
													
														|  | -        indexDescription.ordering = desc.getString(ORDERING);
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -        if (desc.hasPath(KEY_TYPE)) {
 |  | 
 | 
											
												
													
														|  | -            indexDescription.keyType = desc.getString(KEY_TYPE);
 |  | 
 | 
											
												
													
														|  | -        }
 |  | 
 | 
											
												
													
														|  | -        if (desc.hasPath(KEY)) {
 |  | 
 | 
											
												
													
														|  | -            indexDescription.key = desc.getString(KEY);
 |  | 
 | 
											
												
													
														|  | -        }
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -        return indexDescription;
 |  | 
 | 
											
												
													
														|  | -    }
 |  | 
 | 
											
												
													
														|  | -}
 |  | 
 |