注意:连接solr服务器地址:ip:端口号/solr/core名称。。。一定要加core名称(没有的新建一个)
public class SolrJTest {
@Test
public void AddSolr() throws SolrServerException, IOException{
//创建连接
SolrServer server = new HttpSolrServer("http://192.168.145.130:8983/solr/new_core/");
//创建一个文档对象
SolrInputDocument document = new SolrInputDocument();
//创建域(id是必须的,其他的要在我们solr服务器中配置的有)
document.addField("id", "SolrTestId1");
document.addField("item_title", "测试商品");
document.addField("item_sell_point", "卖点");
添加到索引库
server.add(document);
//提交
server.commit();
}
@Test
public void getSolr() throws SolrServerException{
//创建连接
SolrServer server = new HttpSolrServer("http://192.168.145.130:8983/solr/new_core/");
//创建查询
SolrQuery query = new SolrQuery();
//创造查询条件
query.setQuery("*:*");
//执行查询
QueryResponse response = server.query(query);
//取查询结果
//取查询结果
SolrDocumentList solrDocumentList = response.getResults();
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
System.out.println(solrDocument.get("item_title"));
System.out.println(solrDocument.get("item_sell_point"));
}
}
}