org.apache.solr
solr-solrj
8.2.0
2、基本代码
package com.hyc.solr;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.Solrdocument;
import org.apache.solr.common.SolrdocumentList;
import org.apache.solr.common.SolrInputdocument;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class FirstAccess {
public static void main(String[] args) {
// save(); // 调用保存
// delete(); // 调用删除方法
search();
}
public static void search() {
HttpSolrClient client = null;
try {
// 创建客户端,给定url
String url = "http://192.168.2.130:8983/solr/bjsxt";
client = new HttpSolrClient.Builder(url).build();
// 查询参数
SolrQuery params = new SolrQuery();
// 提供搜素关键字
params.setQuery("title_zh_cn:solrJ保存的数据");
//排序规则 根据id升序排列 , asc :升序
params.setSort("id", SolrQuery.ORDER.asc);
//分页
params.setStart(0);
params.setRows(2);
//高亮处理
params.setHighlight(true); // 开启高亮处理
params.addHighlightField("title_zh_cn"); // 设置高亮字段
params.setHighlightSimplePre("");// 设置高亮前缀
params.setHighlightSimplePost("");// 设置后缀
// 搜索数据
QueryResponse response = client.query(params);
// 从响应的数据中获取高亮响应数据
Map>> highlighting = response.getHighlighting();
// 获得返回的结果集 是solrdocumentList是list的接口实现 。固定范型 Solrdocument
SolrdocumentList doList = response.getResults();
System.out.println("本次查询返回数据的行数:" + doList.size());
System.out.println("本次查询返回数据的总行数:" + doList.getNumFound());
for (Solrdocument doc : doList) {
System.out.println("id:" + doc.getFieldValue("id") + ",title_zh_cn" + doc.getFieldValue("title_zh_cn"));
Map> map = highlighting.get(doc.getFieldValue("id"));
//判断是否存在高亮数据
if (map != null && map.size() > 0) {
// 有高亮数据
List hlStrList = map.get("title_zh_cn");
System.out.println("高亮的数据是:" + hlStrList);
} else {
doc.get("title_zh_cn");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭连接资源
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void delete() {
HttpSolrClient client = null;
try {
// 创建客户端,给定url
String url = "http://192.168.2.130:8983/solr/bjsxt";
client = new HttpSolrClient.Builder(url).build();
client.deleteById("2000"); // 根据id删除solr数据
client.deleteById(Arrays.asList("1", "2")); // 批量删除
client.deleteByQuery("title_zh_cn"); // 条件删除
// 提交数据
client.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 保存数据到solr,如果主键的字段id值唯一就是新增,如果不是就是更改(覆盖)
public static void save() {
HttpSolrClient client = null;
try {
// 创建客户端,给定url
String url = "http://192.168.2.130:8983/solr/bjsxt";
client = new HttpSolrClient.Builder(url).build();
//创建保存数据的对象
SolrInputdocument doc = new SolrInputdocument();
// id 为字符串类型
doc.addField("id", "2002"); // 指定id
// 指定保存数据,数据覆盖
doc.addField("title_zh_cn", "solrJ保存的数据2");
// 添加到solr
client.add(doc);
client.commit(); // 提交当前url当前指向的core collection
//client.commit("bjsxt"); //指定 名称的提交事务
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
详情:笔记



