1、创建core
solr的core类似于mysql的表,如下创建名为testcore的core
1
| $ bin/solr create -c testcore
|
2、创建solr连接客户端
1 2
| private static final String urlString = "http://192.168.100.27:8983/solr/testcore"; private static SolrClient solr = new HttpSolrClient.Builder(urlString).build();
|
3、添加/更新索引
1 2 3 4 5 6 7 8
| SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "3"); doc.addField("name", "hello world test"); doc.addField("age", "1"); doc.addField("addr", "1111"); solr.add(doc); solr.commit();
|
4、删除索引
1 2 3 4 5
| solr.deleteById("3");
solr.commit();
|
5、查询
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
| SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.setStart(0); query.setRows(10); QueryResponse queryResponse = solr.query(query); SolrDocumentList docs = queryResponse.getResults(); Iterator<SolrDocument> iter = docs.iterator(); while(iter.hasNext()){ SolrDocument doc = iter.next(); System.out.println(doc.toString()); } solr.commit();
|