1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
public class SolrTest {
public final String baseURL = "http://192.168.22.128:8081/solr";
public HttpSolrServer server=null;
@Before public void init() throws Exception{ server=new HttpSolrServer(baseURL); }
@Test public void testAdd() throws Exception{ Car car1 = new Car("Audi000A4001","奥迪A4L",295000,"/images/001.jpg"); SolrInputDocument doc1 = new SolrInputDocument(); doc1.addField("id",car1.getId()); doc1.addField("name",car1.getName()); doc1.addField("price",car1.getPrice()); doc1.addField("url",car1.getUrl()); server.add(doc1); UpdateResponse rspcommit = server.commit(); rspcommit.getStatus(); System.out.println("操作完成!!!"); }
@Test public void testAddBean() throws Exception{ Car car1 = new Car("BMW0000B5001","宝马5系",436800,"/images/002.jpg"); server.addBean(car1); server.commit(); System.out.println("操作完成!!!"); }
@Test public void change() throws Exception { SolrDocument doc1 = new SolrDocument(); doc1.addField("id","Siju0032SJ0C8001"); doc1.addField("name","世爵C8"); doc1.addField("price",1370000); doc1.addField("url","/images/005.jps");
Car car = server.getBinder().getBean(Car.class,doc1); System.out.println(car); System.out.println("操作完成!!!"); }
@Test public void testDel() throws Exception{ server.deleteByQuery("*:*"); server.commit(); System.out.println("操作完成!!!"); }
@Test public void testUpdate() throws Exception { SolrInputDocument doc1 = new SolrInputDocument(); doc1.addField("id","1"); doc1.addField("title","lisi"); server.add(doc1);
server.commit(); System.out.println("操作完成!!!"); }
@Test public void testQuery() throws Exception { SolrQuery query = new SolrQuery("name:*"); query.setStart(0); query.setRows(3);
QueryResponse rsp = server.query( query ); SolrDocumentList results = rsp.getResults(); System.out.println("总记录数为:" + results.getNumFound());
for(SolrDocument document : results){ System.out.println(document.get("id") + " " + document.get("name") + " " + document.get("price")); } }
@Test public void testQueryMulti() throws Exception { ModifiableSolrParams params = new ModifiableSolrParams();
params.set("q","*:*"); params.set("start",0); params.set("rows",10); params.set("sort","price desc");
QueryResponse response = server.query(params); SolrDocumentList list = response.getResults();
for(SolrDocument doc : list){ System.out.println(server.getBinder().getBean(Car.class,doc)); } }
@Test public void testQueryCase() throws Exception{ SolrQuery params = new SolrQuery();
params.set("sort","price desc"); QueryResponse response = server.query(params); SolrDocumentList list = response.getResults();
for(SolrDocument doc : list){ System.out.println(server.getBinder().getBean(Car.class,doc)); } }
@After public void destroy(){ server.shutdown(); server = null; } }
|