Solr學習筆記(三)-----SpringDataSolr操作
阿新 • • 發佈:2018-11-09
對solr伺服器進行訪問:本質上就是使用了http請求和響應,當我們訪問solr進行搜尋時,實質上就是傳送了一個http請求,如http://localhost:9080/solr/collection1/select?q=%3A&wt=json&indent=true
solr接收到這個請求後,給我們返回了響應。
對Solr伺服器進行訪問的方式,一共有三種:
1、使用solr官方類庫--solrJ,實現原理就是使用http請求和響應,只是進行了一層封裝,不需要寫請求的url等引數 2、使用SpringDataSolr,實現原理是,對solr官方類庫--solrJ進行了封裝 3、使用httpClient,手動請求solr,手動處理響應
SpringDataSolr的使用:
1、引入相關jar包
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>1.5.5.RELEASE</version>
</dependency>
2、在src/main/resources下建立 applicationContext-solr.xml
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:solr="http://www.springframework.org/schema/data/solr" xsi:schemaLocation="http://www.springframework.org/schema/data/solr http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- solr伺服器地址 --> <solr:solr-serverid="solrServer"url="http://127.0.0.1:8080/solr"/> <!-- solr模板,使用solr模板可對索引庫進行CRUD的操作 --> <beanid="solrTemplate"class="org.springframework.data.solr.core.SolrTemplate"> <constructor-argref="solrServer"/> </bean> </beans>
3、@Field註解:當一個實體類的屬性使用@Field註解標識時,就表示這個屬性和solr裡面的一個域的name相匹配,如:
public class TbItem implements Serializable{ @Field private Long id; //就表示這個屬性對應solr中域名為id的域 @Field("item_title") private String title; //就表示這個屬性對應solr中域名為item_title的域 @Field("item_goodsid") private Long goodsId; @Field("item_category") private String category; @Field("item_brand") private String brand; @Field("item_seller") private String seller; @Field("item_price") private BigDecimal price; @Field("item_image") private String image; }
4、向solr索引庫中增加&修改資料:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-solr.xml")
public class TestTemplate {
@Autowired
private SolrTemplate solrTemplate;
@Test
public void testAdd(){
TbItem item = new TbItem();
item.setId(1L); //id域是必須要新增的(PS:當id相同時,再次插入時,就變成了修改操作)
item.setTitle("魅族Pro7");
item.setCategory("手機");
item.setBrand("魅族");
item.setSeller("魅族旗艦店");
item.setGoodsId(10L);
item.setPrice(new BigDecimal(1500));
//將資料新增到solr中
solrTemplate.saveBean(item);
//提交
solrTemplate.commit();
}
}
5、對solr進行按id域查詢和刪除:
/**
* 根據solr索引庫的id查詢資料
*/
@Test
public void findById(){
TbItem item = solrTemplate.getById(1L, TbItem.class);
System.out.println(item.getTitle());
}
/**
* 根據solr索引庫的id刪除資料
*/
@Test
public void testDelete(){
solrTemplate.deleteById("1"); //因為插入solr中的資料,id就變成string,這裡也可以傳入一個list,批量刪除
solrTemplate.commit(); //必須要提交,否則不起作用
}
6、批量插入和分頁查詢
/**
* 批量插入
*/
@Test
public void testAddList(){
List<TbItem>list=new ArrayList();
for(int i=0; i<100; i++){
TbItem item=new TbItem();
item.setId(i+1L);
item.setBrand("魅族");
item.setCategory("手機");
item.setGoodsId(1L);
item.setSeller("魅族旗艦店");
item.setTitle("魅族Pro"+i);
item.setPrice(new BigDecimal(2000+i));
list.add(item);
}
solrTemplate.saveBeans(list); //saveBeans這個方法,這樣就可以實現批量插入
solrTemplate.commit();
}
/**
* 分頁查詢
*/
@Test
public void testQueryPage(){
//1.構造查詢物件,Query是一個介面,new一個它的實現類SimpleQuery
Query query = new SimpleQuery("*:*"); //表示查詢全部資料,如果要根據域查詢,就在這裡傳入域名
query.setOffset(20); //從第幾條資料開始查詢,預設是0
query.setRows(20); //查詢多少條資料,預設是10
//2.查詢資料,這裡得到的是一頁的資料
ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
for (TbItem item : page) {
System.out.println(item.getTitle());
System.out.println(item.getSeller());
System.out.println(item.getBrand());
}
System.out.println("總記錄數:" + page.getTotalElements());
System.out.println("總頁數: " + page.getTotalPages()); //如果在上面不設定Offset和Rows的話,就會採用預設值,name總頁數就會是1
}
7、條件查詢和刪除全部記錄
/**
* 條件查詢
*/
@Test
public void testPageQueryMutil(){
//1.構造查詢物件,Query是一個介面,new一個它的實現類SimpleQuery
Query query = new SimpleQuery("*:*"); //表示查詢全部資料,如果要根據域查詢,就在這裡傳入域名
//新增查詢條件
Criteria criteria = new Criteria("item_title").contains("5"); //查詢item_title域中包含 5 這個詞的資料
criteria = criteria.and("item_brand").contains("2"); //新增一個查詢條件,查詢item_brand 域中包含 2 這個詞的資料
query.addCriteria(criteria);
//query.setOffset(20); //從第幾條資料開始查詢,預設是0
//query.setRows(20); //查詢多少條資料,預設是10
//2.查詢資料,這裡得到的是一頁的資料
ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
for (TbItem item : page) {
System.out.println(item.getTitle());
System.out.println(item.getSeller());
System.out.println(item.getBrand());
}
System.out.println("總記錄數:" + page.getTotalElements());
System.out.println("總頁數: " + page.getTotalPages()); //如果在上面不設定Offset和Rows的話,就會採用預設值,name總頁數就會是1
}
/**
* 刪除全部記錄
*/
@Test
public void testDeleteAll(){
Query query=new SimpleQuery("*:*");
solrTemplate.delete(query);
solrTemplate.commit();
}