solr(四、单机与集群的curd操作)
pom.xml
使用springboot的maven仓库
<!-- solr --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency>
单机模式
application.yml
spring:
data:
solr:
host: http://10.32.16.179:8983/solr/myCore
ps:只使用单个内核的话,在这里配置即可,注意前面一定要加http://
Book.java
package com.wk.test.solrTest; import org.apache.solr.client.solrj.beans.Field; import org.springframework.data.annotation.Id; import java.io.Serializable; public class Book implements Serializable { @Id @Field private String id; @Field private String bookName; @Field private double bookPrice; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public double getBookPrice() { return bookPrice; } public void setBookPrice(double bookPrice) { this.bookPrice = bookPrice; } }
Field注解代表managed-schema文件中对应的field,在使用solr前,需要配置好schema。
我在单机环境下测试,没有配置schema的话,只要字段上定义了@Field的注解,会自动配置schema。没有注解则会报错。但在集群环境下,不先配置schema的话,则一定会报错,@Field注解并不会自动配置schema。在生产环境中,强烈建议先配置好schema文件再使用。
managed-schema
<field name="bookName" type="string" uninvertible="false" indexed="true" stored="true"/> <field name="bookPrice" type="pdoubles" uninvertible="false" indexed="true" stored="true"/>
addBook/updateBook
更新或者插入,如果id存在则更新,不存在则插入
@Autowired private SolrClient solrClient; @Test public void addBook(){ //此次默认使用的一个地址的集合,若有多个,则在代码中 //HttpSolrClient solrClient = new HttpSolrClient.Builder("http://127.0.0.1:8983/solr/myCore/").build(); Book book = new Book(); book.setId("2"); book.setBookName("solr从入门到放弃"); book.setBookPrice(14.00); try { solrClient.addBean(book,10000); solrClient.commit(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
deleteBook
可根据条件或者ID来删除
@Test public void deteleBook(){ //此次默认使用的一个地址的集合,若有多个,则在代码中 //HttpSolrClient solrClient = new HttpSolrClient.Builder("http://127.0.0.1:8983/solr/myCore/").build(); try { //solrClient.deleteByQuery("bookName:solr从入门到放弃"); solrClient.deleteById("2"); solrClient.commit(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
queryBook
- q:查询字符串。查询所有*:* 。查询条件:bookName:solr其实还挺好学 AND bookPrice:13
- fq:过滤查询。createDate:[2020-03-16T23:59:59.999Z TO 2020-03-17T23:59:59.999Z]
- sort:排序。bookName asc,bookPrice desc
- start,rows:从0开始第几条数据。返回多少条记录。用作分页。
- fl:指定返回哪些字段,用逗号或者空格分隔。
- df:默认的查询字段
- raw query parameters:条件查询
- wt:返回格式。
@Test public void queryBook(){ //此次默认使用的一个地址的集合,若有多个,则在代码中 //HttpSolrClient solrClient = new HttpSolrClient.Builder("http://127.0.0.1:8983/solr/myCore/").build(); try { SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery("*:*"); solrQuery.setFilterQueries("bookPrice:[10 TO 15]"); solrQuery.setStart(0); solrQuery.setRows(20); solrQuery.addSort("id", SolrQuery.ORDER.asc); QueryResponse queryResponse = solrClient.query(solrQuery); if(queryResponse!=null){ List<Book> books = queryResponse.getBeans(Book.class); System.out.println(books); } } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
集群模式
application.yml
spring:
zk-host: 10.32.16.179:9983
repositories:
enabled: true
addBook/updateBook
@Autowired private CloudSolrClient cloudSolrClient; @Test public void addBook(){ Book book = new Book(); book.setId("2"); book.setBookName("solr从入门到放弃"); book.setBookPrice(14.00); try { cloudSolrClient.setDefaultCollection("c2"); cloudSolrClient.addBean(book,10000); cloudSolrClient.commit(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
deleteBook
@Test public void deteleBook(){ //此次默认使用的一个地址的集合,若有多个,则在代码中 //HttpSolrClient solrClient = new HttpSolrClient.Builder("http://127.0.0.1:8983/solr/myCore/").build(); try { //solrClient.deleteByQuery("bookName:solr从入门到放弃"); cloudSolrClient.deleteById("2"); cloudSolrClient.commit(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
queryBook
@Test public void queryBook(){ try { SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery("*:*"); solrQuery.setFilterQueries("bookPrice:[10 TO 15]"); solrQuery.setStart(0); solrQuery.setRows(20); solrQuery.addSort("id", SolrQuery.ORDER.asc); cloudSolrClient.setDefaultCollection("c2"); QueryResponse queryResponse = cloudSolrClient.query(solrQuery); if(queryResponse!=null){ List<Book> books = queryResponse.getBeans(Book.class); System.out.println(books); } } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }