Solrj 创建、修改、添加、删除、索引库

代码如下:

private static  HttpSolrServer server = null;
    private static  SolrSearchUtils instance = new SolrSearchUtils();
    /**
     * 单利模式
     */
    public static SolrSearchUtils getInstance(String sellerCode){    
    	initServiceParam(TopUp.upConfig(sellerCodeRels.get(sellerCode) == null ? "" : sellerCodeRels.get(sellerCode)));
    	return instance;     
    }
    
    /**
     * 初始化参数
     * @param url
     */
    private static void initServiceParam(String url){
		server = new HttpSolrServer(url);
                server.setAllowCompression(true);
		server.setSoTimeout(60000); // socket read timeout 
		server.setConnectionTimeout(100); 
		server.setDefaultMaxConnectionsPerHost(100); 
		server.setMaxTotalConnections(100); 
		server.setFollowRedirects(false); // defaults to false 
		server.setAllowCompression(true); 
		server.setMaxRetries(1); // defaults to 0.  > 1 not recommended. 
    }
    
    
    /**
     * 定义添加那些字段
     * @param pro 为一个实体对象
     * @return
     */
    public static SolrInputDocument parese(Product pro) {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("productCode", pro.getProductCode());
        doc.addField("productName", pro.getProductName());
        doc.addField("remarkName", pro.getRemarkName());
        doc.addField("productDetails", pro.getProductDetails());
        doc.addField("mainpicUrl", pro.getMainpicUrl());
        doc.addField("originalPrice", pro.getOriginalPrice());
        doc.addField("currentPrice", pro.getCurrentPrice());
        doc.addField("updateTime", pro.getUpdateTime());
        doc.addField("oneId", pro.getOneId());
        doc.addField("oneName", pro.getOneName());
        doc.addField("twoId", pro.getTwoId());
        doc.addField("twoName", pro.getTwoName());
        doc.addField("brandCode", pro.getBrandCode());
        doc.addField("brandCodeName", pro.getBrandCodeName());
        doc.addField("productNumber", pro.getProductNumber());
        doc.addField("sellerCode", pro.getSellerCode());
        doc.addField("tagList", pro.getTagList());
        doc.addField("propertyValue", pro.getPropertyValue());
        doc.addField("stockNum",pro.getStockNum());
        return doc;
    }
    
    /**
     * 添加单个商品到索引库
     * @param Product
     */
    public  void addOne(Product pro){
        SolrInputDocument doc = parese(pro);
			try {
				server.add(doc);
				server.commit();
			} catch (SolrServerException e){
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
    }

    /**
     * 添加多个商品到索引库
     * @param Product
     */
    public  void addList(List<Product> pro){
        List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
        for (Product pinfo : pro) {
            SolrInputDocument doc = parese(pinfo);
            docs.add(doc);
        }
        try {
			server.add(docs);
			server.optimize();
			server.commit();
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

    
    /**
     * 修改单个商品索引
     * @param Product
     */
    public void updateOne(Product pro){
        addOne(pro);
    }

    /**
     * 删除全部商品索引,尽量不要使用
     */
    public void delete(){
        try {
			server.deleteByQuery("*:*");
			server.commit();
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
    /**
     * 根据商品的zid删除单个商品索引
     */
    public void deleteById(String productCode){
        try {
			server.deleteById(productCode);
			server.commit();
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

 Product 实体详解:

并通过solrj的注解Field来实现实体的自动注入

**
 * 商品缓存的实体类
 * @author zhouguohui 
 * @version 1.0
 */
public class Product {
	/**
	 * 商品ID
	 */
	@Field
	private String productCode ;
	/**
	 * 商品名称
	 */
	@Field
	private String productName;
	/**
	 * 商品副标题名称
	 */
	@Field
	private String remarkName;
	/**
	 * 商品详情信息
	 */
	@Field
	private String productDetails;
	/**
	 * 图片url地址
	 */
	@Field
	private String mainpicUrl;
	/**
	 * 商品原价
	 */
	@Field
	private Double originalPrice;
	/**
	 * 当前价格
	 */
	@Field
	private Double currentPrice;
	/**
	 * 更新时间
	 */
	@Field
	private Date updateTime;
	/**
	 * 一级ID
	 */
	@Field
	private List<String> oneId;
	/**
	 * 一级name
	 */
	@Field
	private List<String> oneName;
	/**
	 * 二级ID
	 */
	@Field
	private List<String> twoId;
	/**
	 * 二级name
	 */
	@Field
	private List<String> twoName;
	/**
	 * 商品规格
	 */
	@Field
	private List<String> propertyValue ;
	/**
	 * 品牌ID
	 */
	@Field
	private String brandCode;
	/**
	 * 品牌名称
	 */
	@Field
	private String brandCodeName;
	/**
	 * 商品销量
	 */
	@Field
	private int productNumber;
	/**
	 * 商品卖家
	 */
	@Field
	private String sellerCode;
	/**
	 * 标签
	 */
	@Field
	private String tagList;
	/**
	 * 商品库存  0代表没有库存  1代表有库存
	 */
	@Field
	private int stockNum;

 

你可能感兴趣的:(Solr)