java 测试 solr更新和查询
2016-02-20 16:29
369 查看
@Test public void addDocument() throws Exception{ //1.创建链接 SolrServer solr = new HttpSolrServer("http://localhost:8080/solr"); //2.创建一文档对象 SolrInputDocument document = new SolrInputDocument(); //3.向文档对象中添加域 (先定义后使用) document.addField("id", "001"); document.addField("title", "这是新的域"); //4.提交文档到索引库 solr.add(document); //5.提交 solr.commit(); } @Test public void deleteDocument() throws Exception{ //1.创建链接 SolrServer solr = new HttpSolrServer("http://localhost:8080/solr"); //2.根据id删除 solr.deleteById("001"); //根据查询删除 //solr.deleteByQuery("*:*"); //.提交 solr.commit(); }
/** * 使用@Field注解的属性要和Solr配置的Field对应。 用于更新、插入 到索引库中的bean类 * @author w7 * */ public class Books { @Field private String id; @Field private String bname; @Field private String bauthor; @Field private String bprice; @Field private String bcurrprice; @Field private String bdiscount; @Field private String bpress; @Field private String bpublishtime; @Field private String bedition; @Field private String bpagenum; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getBname() { return bname; } public void setBname(String bname) { this.bname = bname; } public String getBauthor() { return bauthor; } public void setBauthor(String bauthor) { this.bauthor = bauthor; } public String getBprice() { return bprice; } public void setBprice(String bprice) { this.bprice = bprice; } public String getBcurrprice() { return bcurrprice; } public void setBcurrprice(String bcurrprice) { this.bcurrprice = bcurrprice; } public String getBdiscount() { return bdiscount; } public void setBdiscount(String bdiscount) { this.bdiscount = bdiscount; } public String getBpress() { return bpress; } public void setBpress(String bpress) { this.bpress = bpress; } public String getBpublishtime() { return bpublishtime; } public void setBpublishtime(String bpublishtime) { this.bpublishtime = bpublishtime; } public String getBedition() { return bedition; } public void setBedition(String bedition) { this.bedition = bedition; } public String getBpagenum() { return bpagenum; } public void setBpagenum(String bpagenum) { this.bpagenum = bpagenum; } @Override public String toString() { return "Books [id=" + id + ", bname=" + bname + ", bauthor=" + bauthor + ", bprice=" + bprice + ", bcurrprice=" + bcurrprice + ", bdiscount=" + bdiscount + ", bpress=" + bpress + ", bpublishtime=" + bpublishtime + ", bedition=" + bedition + ", bpagenum=" + bpagenum + "]"; } }
/** * solr测试 * * @author w7 * */ public class SolrTest { private static SolrServer server; private static final String DEFAULT_URL = "http://localhost:8080/solr"; public static void init() { server = new HttpSolrServer(DEFAULT_URL); } // 添加 book 到索引库 public static void indexBooks(Books book) { try { UpdateResponse response = server.addBean(book); server.commit(); System.err.println(response.getStatus());// 响应状态 } catch (IOException | SolrServerException e) { e.printStackTrace(); } } // 查询 public static void testQueryAll() { SolrQuery params = new SolrQuery(); // 查询关键词,*:*代表所有属性、所有值,即所有index params.set("q", "*:*"); // 分页,start=0就是从0开始,rows=5当前返回5条记录,第二页就是变化start这个值为5就可以了。 params.set("start", 0); params.set("rows", "5"); // 排序,如果按照id排序,那么 写为: id desc(or asc) params.set("sort", "id asc"); QueryResponse response = null; try { response = server.query(params); } catch (SolrServerException e) { e.printStackTrace(); } if (response != null) { System.out.println("Search Results: "); SolrDocumentList list = response.getResults(); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).get("bpress"));//根据key取值 } } } //测试: public static void main(String[] args) { init(); SolrTest.testQueryAll(); } }
相关文章推荐
- 初识 ScheduledThreadPoolExecutor
- Spring MVC 学习笔记 Viewresol和View
- 搭建持续集成单元测试平台(Jenkins+Ant+Java+Junit+SVN)
- 蓝桥杯 历届试题 打印十字图 java详解
- Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
- eclipse常用快捷键
- Spark - ERROR Executor: Exception in tjava.lang.OutOfMemoryError: unable to create new native thread
- Mac上如何安装gradle以及eclipse上如何搭建gradle
- Java发送get及post请求工具方法
- Java认证考试实例疑难辨析(11)
- Spring 资源访问剖析和策略模式应用
- 夯实JAVA基本之一——泛型详解(2):高级进阶
- Servlet JNICALL Java_calcServlet_calc
- 夯实JAVA基本之一 —— 泛型详解(1):基本使用
- Spring DI
- 数组中出现次数超过一半的数字 -java
- JAVA多线程-线程间通信(二)-生产者/消费者模式实现
- java中截取字符串技巧
- Java 可变参数列表
- JAVA的Date类与Calendar类【转】