01.java的springboot项目中去用solr框架,导入依赖
<dependencies>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>8.2.0</version>
</dependency>
</dependencies>
1新增/修改实现
String url = "http://192.168.32.133:8983/solr/testcore";//访问的url ip+端口号+/solr/库名
HttpSolrClient solrClient = new HttpSolrClient.Builder(url).build();//建造模式
SolrInputDocument inputDocument = new SolrInputDocument();
inputDocument.addField("id","3");//主键
inputDocument.addField("myfield","myfield3");
solrClient.add(inputDocument);//其实还需要写事务的回写操作rollback
solrClient.commit();//提交
2删除实现文章来源:https://www.toymoban.com/news/detail-519709.html
String url = "http://192.168.32.133:8983/solr/testcore";
HttpSolrClient solrClient = new HttpSolrClient.Builder(url).build();
solrClient.deleteById("3");
solrClient.commit();
3查询实现文章来源地址https://www.toymoban.com/news/detail-519709.html
public void testQuery(){
try {
String url = "http://192.168.52.130:8983/solr/testcore";
HttpSolrClient solrClient = new HttpSolrClient.Builder(url).build();
//封装了所有查询条件
SolrQuery params = new SolrQuery();
params.setQuery("name:丰富的");
//排序
params.setSort("price", SolrQuery.ORDER.desc);
//分页
params.setStart(0);
params.setRows(1);
//高亮
params.setHighlight(true);
params.addHighlightField("name");
params.setHighlightSimplePre("<span>");
params.setHighlightSimplePost("</span>");
QueryResponse response = solrClient.query(params);
SolrDocumentList list = response.getResults();
System.out.println("总条数:"+list.getNumFound());
//高亮数据
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
for(SolrDocument doc :list){
System.out.println(doc.get("id"));
Map<String, List<String>> map = highlighting.get(doc.get("id"));
List<String> HLList = map.get("name");
if(HLList!=null&&HLList.size()>0){//显示高亮数据
System.out.println(HLList.get(0));
}else{
System.out.println(doc.get("name"));
}
System.out.println(doc.get("price"));
System.out.println("===================");
}
solrClient.close();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
到了这里,关于Solr框架 03 使用SolrJ在项目中运用solr,结果和在web界面操作document一样的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!