6.Solr4.10.3API使用(CURD)
阿新 • • 發佈:2017-05-20
更新 document ati urn multi out lis sys ued
轉載請出自出處:http://www.cnblogs.com/hd3013779515/
1.在工程中引入solr-solrj-4.10.3.jar
<dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>4.10.3</version> </dependency>
2.Solr的增刪改查
(1) schema.xml配置修改
<field name="stu_name"type="text_ik" indexed="true" stored="true" multiValued="false" /> <field name="stu_sex" type="int" indexed="true" stored="true" multiValued="false" /> <field name="stu_address" type="text_ik" indexed="true" stored="true" multiValued="false" />
(2)Student.java
package cn.ljh.ssm.test;import org.apache.solr.client.solrj.beans.Field; public class Student{ @Field("id") private String id; @Field("stu_name") private String name; @Field("stu_sex") private int sex; @Field("stu_address") private String address; public String getId() { returnid; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", address=" + address + "]"; } }
(3) HttpSolrServerSingleton.java
package cn.ljh.ssm.test; import org.apache.solr.client.solrj.impl.HttpSolrServer; public class HttpSolrServerSingleton { //solr server URL指的時solr發布到web工程後的訪問路徑 private final static String SolrURL = "http://192.168.137.168:8080/solr"; //使用懶漢式單例中的靜態內部類方式 private static class HttpSolrServerSingletonContainer{ private static HttpSolrServer instance = new HttpSolrServer( HttpSolrServerSingleton.SolrURL); } //solrServer是線程安全的,所以在使用時需要使用單例的模式,減少資源的消耗 public static HttpSolrServer getInstance(){ return HttpSolrServerSingletonContainer.instance; } }
(4) SolrHelloWorldTest.java
package cn.ljh.ssm.test; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Random; 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; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import org.junit.Test; public class SolrHelloWorldTest { @Test public void testAdd(){ try { HttpSolrServer server = HttpSolrServerSingleton.getInstance(); //先刪除所有數據 server.deleteByQuery("*:*"); SolrInputDocument doc = new SolrInputDocument(); doc.addField("id",String.valueOf(1)); doc.addField("name","apple phone"); doc.addField("price","6000"); server.add(doc); SolrInputDocument doc2 = new SolrInputDocument(); doc2.addField("id",String.valueOf(2)); doc2.addField("name","huawei phone"); doc2.addField("price","1000"); server.add(doc2); SolrInputDocument doc3 = new SolrInputDocument(); doc3.addField("id",String.valueOf(3)); doc3.addField("name","mi phone"); doc3.addField("price","2000"); server.add(doc3); SolrInputDocument doc5 = new SolrInputDocument(); doc5.addField("id",String.valueOf(15)); doc5.addField("name","mi phone02"); doc5.addField("price","2000"); server.add(doc5); Random random = new Random(); for (int i = 0; i < 10; i++) { SolrInputDocument doc4 = new SolrInputDocument(); doc4.addField("id",i+4); doc4.addField("name","phone"+i); doc4.addField("price",random.nextInt(2000)); server.add(doc4); } server.commit();//提交,將所有更新提交到索引中 } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 使用POJO添加document */ @Test public void testAddStudent(){ try { HttpSolrServer server = HttpSolrServerSingleton.getInstance(); List<Student> studentList = new ArrayList<Student>(); Student stu1 = new Student(); stu1.setId("103"); stu1.setName("張小強"); stu1.setSex(1); stu1.setAddress("北京市海澱區知春路"); studentList.add(stu1); stu1 = new Student(); stu1.setId("104"); stu1.setName("劉小米"); stu1.setSex(0); stu1.setAddress("北京市海澱區北苑路"); studentList.add(stu1); server.addBeans(studentList); server.commit(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Test public void testQueryStudent(){ try { HttpSolrServer server = HttpSolrServerSingleton.getInstance(); String strQuery = "stu_name:小米";//q表示查詢的內容 SolrQuery query = new SolrQuery(strQuery); QueryResponse resp = server.query(query); SolrDocumentList sdList = resp.getResults(); long totalResults = sdList.getNumFound();//命中的總記錄數 System.out.println("totalResults-->"+totalResults); for(SolrDocument sd:sdList){ Student student = server.getBinder().getBean(Student.class, sd); System.out.println(student); } } catch (SolrServerException e) { e.printStackTrace(); } } @Test public void testQuery(){ try { HttpSolrServer server = HttpSolrServerSingleton.getInstance(); String strQuery = "name:apple";//q表示查詢的內容 SolrQuery query = new SolrQuery(); query.set("q",strQuery); QueryResponse resp = server.query(query); SolrDocumentList sdList = resp.getResults(); long totalResults = sdList.getNumFound();//命中的總記錄數 System.out.println("totalResults-->"+totalResults); for(SolrDocument sd:sdList){ System.out.print("id:" + sd.getFieldValue("id") + " " + "name:" + sd.getFieldValue("name") + " " + "price:" + sd.getFieldValue("price")); } } catch (SolrServerException e) { e.printStackTrace(); } } @Test public void testDelete(){ try { HttpSolrServer server = HttpSolrServerSingleton.getInstance(); server.deleteById("1"); server.deleteByQuery("id:2 id:3"); server.commit(); } catch (Exception e) { e.printStackTrace(); } } }
6.Solr4.10.3API使用(CURD)