<!--[if !supportLists]-->1.<!--[endif]-->在solr配置完毕后,进行solrj的增删改查操作。
D:\solr\home\collection1\conf\schema.xml(自己的目录)
注意:由于schema.xml中的uniqueKey为id,所以操作任何对象需要有id,否则报错
添加
<!--[if !supportLists]-->1.<!--[endif]-->添加单个字段:
private static final String SOLR_URL = "http://localhost:8080/solr/";
@Test
public void addIndex() {
SolrServer solrServer = new HttpSolrServer(SOLR_URL);
SolrInputDocument document = new SolrInputDocument();
/*
* 添加单个字段
* /
document.addField("id", "1234");
document.addField("title", "软件交流会");
try {
solrServer.add(document);
solrServer.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.getMessage();
}
<!--[if !supportLists]-->2.<!--[endif]-->添加javabean对象
private static final String SOLR_URL = "http://localhost:8080/solr/";
@Test
public void addIndex() {
Product product = new Product();
product.setId(1001);
product.setName("eclipse");
product.setPrice(1000);
product.setDesc("开发工具");
SolrServer solrServer = new HttpSolrServer(SOLR_URL);
SolrInputDocument document = new SolrInputDocument();
/**
* 添加javabean对象
*/
try {
solrServer.addBean(product);
solrServer.commit();
} catch (IOException e) {
e.getMessage();
} catch (SolrServerException e) {
e.getMessage();
}
}
将server提取
private static final String SOLR_URL = "http://localhost:8080/solr/";
private SolrServer solrServer ;
@Before
public void before() {
solrServer = new HttpSolrServer(SOLR_URL);
}
/**
* 提交与释放资源
*/
@After
public void after() {
try {
solrServer.commit();
solrServer = null;
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
删除
/**
* 删除,通过上面的id进行删除
*/
@Test
public void deleteIndex() {
//SolrServer server = new HttpSolrServer(SOLR_URL);
try {
solrServer.deleteById("1012");
//server.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
修改
/**
* 修改
*/
@Test
public void updateIndex() {
Product product = new Product();
product.setId(1013);
product.setPrice(20000);
product.setDesc("特价处理");
try {
solrServer.addBean(product);
} catch (IOException e) {
e.printStackTrace();
} catch (SolrServerException e) {
e.printStackTrace();
}
}
查询
/**
* 查询
* @throws Exception
*/
@Test
public void query() throws Exception {
SolrQuery query = new SolrQuery();
query.setQuery("name:springtoolsuite");//设置查询条件,参考页面中的q
QueryResponse response = solrServer.query(query);
SolrDocumentList results = response.getResults();//查询获取结果集
//遍历获取集合中的每个数据
for (SolrDocument solrDocument : results) {
System.out.println(solrDocument.getFieldValue("name"));
System.out.println(solrDocument.getFieldValue("price"));
}
}
高亮查询
/**
* 高亮查询
* @throws Exception
*/
@Test
public void highLightQuery() throws Exception {
SolrQuery solrQuery = new SolrQuery();
solrQuery.setHighlight(true);//开启高亮
solrQuery.setHighlightSimplePre("<font color='red'>");//设置高亮字段的前缀(标记)
solrQuery.setHighlightSimplePost("</font>");
solrQuery.setQuery("name:springtoolsuite");
QueryResponse queryResponse = solrServer.query(solrQuery);//查询响应
//SolrDocumentList list = queryResponse.getResults();
Map<String, Map<String, List<String>>> map = queryResponse.getHighlighting();
System.out.println(map.entrySet());
for (Map.Entry<String, Map<String, List<String>>> entry : map.entrySet()) {
for(Map.Entry<String, List<String>> value : entry.getValue().entrySet()) {
System.out.println(value.getKey());
for(String str : value.getValue()) {
System.out.println(str);
}
}
}
}