Spring Data SolrSolrSolrJAPI
<field name="item_goodsid" type="long" indexed="true" stored="true"/> <field name="item_title" type="text_ik" indexed="true" stored="true"/> <field name="item_price" type="double" indexed="true" stored="true"/> <field name="item_image" type="string" indexed="false" stored="true" /> <field name="item_category" type="string" indexed="true" stored="true" /> <field name="item_seller" type="text_ik" indexed="true" stored="true" /> <field name="item_brand" type="string" indexed="true" stored="true" /> <field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/> <!-- 复制域 --><copyField source="item_title" dest="item_keywords"/> <copyField source="item_category" dest="item_keywords"/> <copyField source="item_seller" dest="item_keywords"/> <copyField source="item_brand" dest="item_keywords"/> <!-- 动态域 --> <dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-solr</artifactId> <version>1.5.5.RELEASE</version> </dependency>
public class TbItem implements Serializable{ private static final long serialVersionUID = 1L; @Field private Long id; @Field("item_title") private String title; private String sellPoint; @Field("item_price") private BigDecimal price; private Integer stockCount; private Integer num; private String barcode; @Field("item_image") private String image; private Long categoryid; private String status; private Date createTime; @Field("item_updatetime") private Date updateTime; private String itemSn; private BigDecimal costPirce; private BigDecimal marketPrice; private String isDefault; @Field("item_goodsid") private Long goodsId; private String sellerId; private String cartThumbnail; @Field("item_category") private String category; @Field("item_brand") private String brand; private String spec; @Field("item_seller") private String seller; @Dynamic @Field("item_spec_*") private Map<String,String> specMap;
@Component public class SolrUtil { @Autowired private TbItemMapper itemMapper; @Autowired private SolrTemplate solrTemplate; public void importItemData(){ TbItemExample example = new TbItemExample(); Criteria criteria = example.createCriteria(); criteria.andStatusEqualTo("1"); List<TbItem> list = itemMapper.selectByExample(example); System.out.println("---商品列表---"); for (TbItem tbItem : list) { System.out.println(tbItem.getId()+" "+ tbItem.getTitle()+ " "+tbItem.getPrice()); // 动态域数据 String spec = tbItem.getSpec(); // FastJSON的泛型 Map<String, String> map = JSON.parseObject(spec, new TypeReference<Map<String, String>>(){}); tbItem.setSpecMap(map); } solrTemplate.saveBeans(list); solrTemplate.commit(); System.out.println("---结束---"); }}
(6) 高亮显示
1) 添加/修改
solrTemplate.saveBean(item);
2) 主键查询
Item item = solrTemplate.getById(1, Item.class);
3) 主键删除
solrTemplate.deleteById("1");
4) 分页
Query query=new SimpleQuery("*:*"); query.setOffset(20);//开始索引(默认0) query.setRows(20);//每页记录数(默认10) ScoredPage<Item> page = solrTemplate.queryForPage(query, Item.class); List<Item> list = page.getContent();
5) 条件查询
Query query=new SimpleQuery("*:*"); Criteria criteria=new Criteria("item_title").contains("2"); criteria=criteria.and("item_title").contains("5"); query.addCriteria(criteria); ScoredPage<Item> page = solrTemplate.queryForPage(query, Item.class); List<TbItem> list = page.getContent();
6) 删除所有
Query query=new SimpleQuery("*:*"); solrTemplate.delete(query);
7) 提交(cud)
solrTemplate.commit();
8) 关于动态域搜索
item_spec_网络;item_spec_机身内存spec = {'key1':'value1', 'key2':'value2'};spec = {'网络':'value1', '机身内存':'value2'} Map<String, String> spec = (Map<String, String>) searchMap.get("spec"); for (String key : spec.keySet()) { String value = spec.get(key); FilterQuery filterQuery = new SimpleFilterQuery(); Criteria filterCriteria = new Criteria("item_spec_" + key); filterCriteria.is(value); filterQuery.addCriteria(filterCriteria); query.addFilterQuery(filterQuery); }