Solr(二)创建索引和查询索引的基本应用

基础内容可见solr wiki : http://wiki.apache.org/solr/Solrj

1.根据数据库对象属性创建schedule.xml中的属性字段


<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false"/>
   <field name="product_id" type="string" indexed="false" stored="true" required="true" multiValued="false"/>
   
   <field name="category_attribute" type="smartCnText" indexed="true" stored="true"/>
   <field name="product_design" type="smartCnText" indexed="true" stored="true"/>
   <field name="product_material" type="smartCnText" indexed="true" stored="true"/>
   <field name="product_name_cn" type="smartCnText" indexed="true" stored="true"/>
   <field name="product_sku" type="smartCnText" indexed="true" stored="true"/>
   <field name="product_title" type="smartCnText" indexed="true" stored="true"/>
   <field name="sale_count" type="string" indexed="true" stored="true"/>
   <field name="create_time" type="string" indexed="false" stored="true"/>
   <field name="current_price" type="string" indexed="false" stored="true"/>
   <field name="sale_price" type="string" indexed="true" stored="true"/>
   <field name="market_price" type="string" indexed="true" stored="true"/>
   <field name="bigcate_name" type="smartCnText" indexed="true" stored="true"/>
   <field name="brand_name" type="smartCnText" indexed="true" stored="true"/>
   <field name="brand_code" type="smartCnText" indexed="true" stored="true"/>
   <field name="product_color" type="smartCnText" indexed="true" stored="true"/>
   <field name="product_size" type="smartCnText" indexed="true" stored="true"/>
   <field name="picture_path" type="string" indexed="false" stored="true"/>
   <field name="page_path" type="string" indexed="false" stored="true"/>
   <field name="material_art" type="smartCnText" indexed="true" stored="true"/>
   <field name="material_out" type="smartCnText" indexed="true" stored="true"/>
   <field name="material_in" type="smartCnText" indexed="true" stored="true"/>
   <field name="material_filter" type="smartCnText" indexed="true" stored="true"/>
   <field name="filter_weight" type="smartCnText" indexed="true" stored="true"/>
   <field name="product_keyword" type="smartCnText" indexed="true" stored="true"/>
   
   <field name="text" type="smartCnText" indexed="true" stored="true" multiValued="true"/>
   
   <field name="_version_" type="long" indexed="true" stored="true"/>

<uniqueKey>id</uniqueKey>
 <defaultSearchField>text</defaultSearchField>
 
   <copyField source="category_attribute" dest="text"/>
   <copyField source="product_design" dest="text"/>
   <copyField source="product_material" dest="text"/>
   <copyField source="product_name_cn" dest="text"/>
   <copyField source="product_sku" dest="text"/>
   <copyField source="product_title" dest="text"/>
   <copyField source="bigcate_name" dest="text"/>
   <copyField source="brand_name" dest="text"/>
   <copyField source="product_color" dest="text"/>
   <copyField source="product_size" dest="text"/>
   <copyField source="material_art" dest="text"/>
   <copyField source="material_out" dest="text"/>
   <copyField source="material_in" dest="text"/>
   <copyField source="material_filter" dest="text"/>
   <copyField source="filter_weight" dest="text"/>
   <copyField source="product_keyword" dest="text"/>
   <copyField source="current_price" dest="text"/>

2.使用solrj创建索引



package com.vegaga.sou.index.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vegaga.common.StringUtils;
import com.vegaga.common.product.PriceUtils;
import com.vegaga.domain.product.VgProduct;
import com.vegaga.domain.product.VgProductDetail;
import com.vegaga.domain.product.VgProductSku;
import com.vegaga.service.VgProductSkuService;
import com.vegaga.sou.index.service.VgProductSkuIndexService;

@Service("vgProductSkuIndexService")
public class VgProductSkuIndexServiceImpl implements VgProductSkuIndexService {
	
	private static final String SEARCH_PRODUCT_ID = "product_id";
	/**末端分类属性名*/
	private static final String SEARCH_CATEGORY_ATTRIBUTE = "category_attribute";
	/**图案*/
	private static final String SEARCH_PRODUCT_DESIGN = "product_design";
	/**材质*/
	private static final String SEARCH_PRODUCT_MATERIAL = "product_material";
	/**商品名称*/
	private static final String SEARCH_PRODUCT_NAME_CN = "product_name_cn";
	/**商品SKU*/
	private static final String SEARCH_PRODUCT_SKU = "product_sku";
	/**商品标题*/
	private static final String SEARCH_PRODUCT_TITLE = "product_title";
	/**商品销量*/
	private static final String SEARCH_SALE_COUNT = "sale_count";
	/**创建时间*/
	private static final String SEARCH_CREATE_TIME = "create_time";
	/**当前价格*/
	private static final String SEARCH_CURRENT_PRICE = "current_price";
	/**卖价*/
	private static final String SEARCH_SALE_PRICE = "sale_price";
	/**市场价*/
	private static final String SEARCH_MARKET_PRICE = "market_price";
	/**商品大分类*/
	private static final String SEARCH_SKU_BIGCATE_NAME = "bigcate_name";
	/**品牌名称*/
	private static final String SEARCH_BRAND_NAME = "brand_name";
	/**品牌code*/
	private static final String SEARCH_BRAND_CODE = "brand_code";
	/**商品颜色*/
	private static final String SEARCH_PRODUCT_COLOR = "product_color";
	/**商品尺寸*/
	private static final String SEARCH_PRODUCT_SIZE = "product_size";
	/**商品图片路径*/
	private static final String SEARCH_SKU_PICTURE_PATH = "picture_path";
	/**商品页面路径*/
	private static final String SEARCH_SKU_PAGE_PATH = "page_path";
	private static final String SEARCH_PRODUCT_MATERIAL_ART = "material_art";
	private static final String SEARCH_PRODUCT_MATERIAL_OUT = "material_out";
	private static final String SEARCH_PRODUCT_MATERIAL_IN = "material_in";
	private static final String SEARCH_PRODUCT_MATERIAL_FILTER = "material_filter";
	private static final String SEARCH_FILTER_WEIGHT = "filter_weight";
	private static final String SEARCH_PRODUCT_KEYWORD = "product_keyword";
	
	private static final String SEARCH_PRODUCT_TEXT = "text";
	
	
	
	VgProductSkuService vgProductSkuService;
	
	@Autowired
	public void setVgProductSkuService(VgProductSkuService vgProductSkuService) {
		this.vgProductSkuService = vgProductSkuService;
	}
	
	@Override
	public List<SolrInputDocument> addVgProductSkuIndex() {
		List<SolrInputDocument> documents = new ArrayList<SolrInputDocument>();
		List<VgProductSku> vgProductSkus = vgProductSkuService.getAll();
		if(vgProductSkus != null){
			for(VgProductSku vgProductSku : vgProductSkus){
				System.out.println("load"+vgProductSku);
				VgProduct vgProduct = vgProductSku.getVgProduct();
				VgProductDetail vgProductDetail = vgProduct.getVgProductDetail();
				SolrInputDocument document = new SolrInputDocument();
				try{
					document.addField("id", vgProductSku.getId().toString());
					document.addField(SEARCH_PRODUCT_ID, vgProduct.getId().toString());//不做索引
					document.addField(SEARCH_CATEGORY_ATTRIBUTE, StringUtils.nullToEmpStr(vgProduct.getCategoryAttribute()));
					document.addField(SEARCH_PRODUCT_DESIGN, StringUtils.nullToEmpStr(vgProduct.getDesign()));
					document.addField(SEARCH_PRODUCT_MATERIAL, StringUtils.nullToEmpStr(vgProduct.getMaterial()));
					document.addField(SEARCH_PRODUCT_NAME_CN, StringUtils.nullToEmpStr(vgProduct.getProductNameCn()));
					
					document.addField(SEARCH_PRODUCT_SKU, StringUtils.nullToEmpStr(vgProductSku.getProductSku()));
					document.addField(SEARCH_PRODUCT_TITLE, StringUtils.nullToEmpStr(vgProductSku.getProductSkuTitle()));
					document.addField(SEARCH_SALE_COUNT, StringUtils.nullToEmpStr(vgProductSku.getSaleCount()));
					document.addField(SEARCH_CREATE_TIME, StringUtils.nullToEmpStr(vgProductSku.getCreateTime()));
					document.addField(SEARCH_CURRENT_PRICE, PriceUtils.getCurrentPrice(vgProductSku).toString());
					document.addField(SEARCH_SALE_PRICE, vgProductSku.getSalePrice().toString());
					document.addField(SEARCH_MARKET_PRICE, vgProductSku.getMarketPrice().toString());
					document.addField(SEARCH_SKU_BIGCATE_NAME, StringUtils.nullToEmpStr(vgProductSku.getSkuBigCateName()));
					document.addField(SEARCH_BRAND_NAME, StringUtils.nullToEmpStr(vgProductSku.getBrandName()));
					document.addField(SEARCH_BRAND_CODE, StringUtils.nullToEmpStr(vgProductSku.getBrandCode()));
					document.addField(SEARCH_PRODUCT_COLOR, StringUtils.nullToEmpStr(vgProductSku.getProductColor()));
					document.addField(SEARCH_PRODUCT_SIZE, StringUtils.nullToEmpStr(vgProductSku.getProductSize()));
					document.addField(SEARCH_SKU_PICTURE_PATH, StringUtils.nullToEmpStr(vgProductSku.getSkuPicturePath()));
					document.addField(SEARCH_SKU_PAGE_PATH, StringUtils.nullToEmpStr(vgProductSku.getSkuPagePath()));
					
					document.addField(SEARCH_PRODUCT_MATERIAL_ART, StringUtils.nullToEmpStr(vgProductDetail.getMaterialArt()));
					document.addField(SEARCH_PRODUCT_MATERIAL_OUT, StringUtils.nullToEmpStr(vgProductDetail.getMaterialOut()));
					document.addField(SEARCH_PRODUCT_MATERIAL_IN, StringUtils.nullToEmpStr(vgProductDetail.getMaterialIn()));
					document.addField(SEARCH_PRODUCT_MATERIAL_FILTER, StringUtils.nullToEmpStr(vgProductDetail.getMaterialFiller()));
					document.addField(SEARCH_FILTER_WEIGHT, StringUtils.nullToEmpStr(vgProductDetail.getFillerWeight()));
					document.addField(SEARCH_PRODUCT_KEYWORD, StringUtils.nullToEmpStr(vgProductDetail.getKeyWord()));
					
					document.addField(SEARCH_PRODUCT_TEXT, "");
					documents.add(document);
				}catch(Exception e){
					//e.printStackTrace();
				}
			}
		}
		return documents;
	}
	
}


package com.vegaga.sou.task;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.springframework.beans.factory.annotation.Autowired;

import com.vegaga.sou.index.service.VgProductSkuIndexService;

public class AddIndexToSolrTask {
	
	VgProductSkuIndexService vgProductSkuIndexService;
	
	@Autowired
	public void setVgProductSkuIndexService(VgProductSkuIndexService vgProductSkuIndexService) {
		this.vgProductSkuIndexService = vgProductSkuIndexService;
	}

	public void addIndexToSolr(){
		HttpSolrServer server = new HttpSolrServer("http://127.0.0.1:8081/solr");
		try {
			server.deleteByQuery("*.*");
			server.add(vgProductSkuIndexService.addVgProductSkuIndex());
			server.commit();
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
3.读取索引信息
package com.vegaga.sou.task;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;

public class AddIndexToSolrTaskSearchTest {
	
	public static void main(String[] args) {
		HttpSolrServer server = new HttpSolrServer("http://localhost:8081/solr");
		SolrQuery query = new SolrQuery();
		query.setQuery("106203");
		QueryResponse resp = null;
		try {
			resp = server.query(query);
			for(SolrDocument document : resp.getResults()){
				System.out.println(document.getFieldValue("id"));
				System.out.println(document.getFieldValue("product_id"));
				System.out.println(document.getFieldValue("material_art"));
			}
		} catch (SolrServerException e) {
			e.printStackTrace();
		}
	}

}

转载于:https://my.oschina.net/sourcecoding/blog/115763

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值