RecallRequest.java 3.6 KB
package com.yoho.search.models.recall;

import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.MD5Util;
import com.yoho.search.cache.model.ICacheRequest;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.service.recall.strategy.IStrategy;
import com.yoho.search.service.recall.strategy.StrategyEnum;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.util.Arrays;


/**
 * 召回请求
 */
public class RecallRequest implements ICacheRequest, IRecallRequest {

    private ParamQueryFilter paramQueryFilter;
    private IStrategy strategy;
    public RecallRequest(ParamQueryFilter paramQueryFilter, IStrategy strategy) {
        this.paramQueryFilter = paramQueryFilter;
        this.strategy = strategy;
        this.redisKeyBuilder = genRedisKeyBuilder();
    }

    private RedisKeyBuilder redisKeyBuilder;

    public RecallRequest(ParamQueryFilter paramQueryFilter, IStrategy strategy,String extendId) {
        this.paramQueryFilter = paramQueryFilter;
        this.strategy = strategy;
        this.redisKeyBuilder = genRedisKeyBuilder();
    }

    private RedisKeyBuilder genRedisKeyBuilder() {
        StringBuilder sb = new StringBuilder();
        sb.append("paramMd5Key:").append(paramQueryFilter == null ? "" : paramQueryFilter.getParamMd5Key());
        sb.append("strategyCacheKey:").append(strategy == null ? "" : strategy.strategyCacheKey());
        String cacheKey = MD5Util.string2MD5(sb.toString());
        RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance();
        redisKeyBuilder.appendFixed("YOHOSEARCH:").appendFixed("RECALL:");
        redisKeyBuilder.appendFixed(strategy.strategtEnum().name()).appendFixed(":");
        redisKeyBuilder.appendVar(cacheTimeInMinute()).appendFixed(":");
        redisKeyBuilder.appendVar(cacheKey);
        return redisKeyBuilder;
    }

    @Override
    public RedisKeyBuilder redisKeyBuilder() {
        return this.redisKeyBuilder;
    }

    @Override
    public int cacheTimeInMinute() {
        return this.strategy.cacheTimeInMinute();
    }

    @Override
    public IStrategy strategy() {
        return strategy;
    }

    public StrategyEnum requestStrategy() {
        return this.strategy.strategtEnum();
    }

    public String intervalKey(){
        return this.strategy.intervalKey();
    }

    @Override
    public SearchParam searchParam() {
        SearchParam searchParam = new SearchParam();
        if (paramQueryFilter != null && paramQueryFilter.getParamQuery() != null) {
            searchParam.setQuery(this.paramQueryFilter.getParamQuery());
        }
        searchParam.setFiter(this.getRealFilter());
        searchParam.setIncludeFields(this.includeFields());
        searchParam.setSortBuilders(Arrays.asList(this.strategy.sortBuilder()));
        searchParam.setOffset(0);
        searchParam.setSize(this.strategy.size());
        return searchParam;
    }

    private QueryBuilder getRealFilter() {
        BoolQueryBuilder realFilter = QueryBuilders.boolQuery();
        if (strategy != null && strategy.extendFilter() != null) {
            realFilter.must(this.strategy.extendFilter());
        }
        if (paramQueryFilter != null && paramQueryFilter.getParamFilter() != null) {
            realFilter.must(this.paramQueryFilter.getParamFilter());
        }
        if (realFilter.hasClauses()) {
            return realFilter;
        }
        return null;
    }

}