1、solrJ客户端实例创建并设置连接超时时间:
final String solrUrl = "http://127.0.0.1:8080/solr";
//创建solrClient同时指定超时时间,不指定走默认配置
HttpSolrClient build = new HttpSolrClient.Builder(solrUrl)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
不同solr版本solrj 的创建方式有所不同
//solr4创建方式
//SolrServer solrServer = new HttpSolrServer("http://127.0.0.1:8080/solr");
//solr5创建方式,在url中指定core名称:core1
//HttpSolrClient solrServer=new HttpSolrClient("http://127.0.0.1:8080/solr/core1");
//solr7创建方式,在url中指定core名称:core1
HttpSolrClient solrServer= new HttpSolrClient.Builder("http://127.0.0.1:8080/solr/core1").build();
注意:solr5以后URL指向自定义核心的名称,如实例名称是core1,那么URL为http://127.0.0.1:8080/solr/core1
solrJ之查询
/**
* @Description:查询
* @throws SolrServerException
* @throws IOException
*/
public void querySolr() throws SolrServerException, IOException{
//获取连接
HttpSolrClient solrClient= new HttpSolrClient.Builder(solr_url).build();
//封装查询条件
Map<String,String> query = new HashMap<String, String>();
//q在sole中表示条件,*:*表示查询所有结果
query.put("q","*:*");
//添加到MapSolrParams对象
MapSolrParams queryParams = new MapSolrParams(query);
//执行查询,返回结果
QueryResponse response = solrClient.query(queryParams);
//获取doc文档
SolrDocumentList list = response.getResults();
for (SolrDocument solrDocument : list) {
System.out.println("id:" + solrDocument.get("id") + "\name:"
+ solrDocument.get("name"));
}
//关闭连接
solrClient.close();
}
solrJ之新增索引
/**
* @throws IOException
* @throws SolrServerException
* @Description:添加
*/
public void solrAdd() throws SolrServerException, IOException{
//获取连接
SolrClient solrClient = new HttpSolrClient.Builder(solr_url).build();
//创建文档doc
SolrInputDocument solrInputDocument = new SolrInputDocument();
//添加内容
String id = UUID.randomUUID().toString();
String name = "李四";
solrInputDocument.addField("id", id);
solrInputDocument.addField("name", name);
//添加到client
UpdateResponse updateResponse = solrClient.add(solrInputDocument);
System.out.println(updateResponse.getElapsedTime());
//提交
solrClient.commit();
solrClient.close();
}
solrJ之删除索引
/**
* @Description:通过id删除
* @throws SolrServerException
* @throws IOException
*/
public void delete() throws SolrServerException, IOException{
//获取连接
SolrClient solrClient = new HttpSolrClient.Builder(solr_url).build();
//通过id删除
String id = "1";
solrClient.deleteById(id);
//提交
solrClient.commit();
solrClient.close();
}
JavaBean绑定
SolrJ支持通过@Field注解隐式转换文档与任何类。每个实例变量在Java对象可以映射到一个相应的Solr字段.
在managed-schema文件中添加字段
managed-schema位于你的core的conf目录下
<!--自定义bean-->
<field name="user_name" type="text_ik" indexed="true" stored="true"/>
<field name="user_sex" type="string" indexed="true" stored="true"/>
<field name="user_age" type="string" indexed="true" stored="true"/>
<field name="user_like" type="string" indexed="true" stored="true" multiValued="true"/>
<field name="user_context" type="text_ik" indexed="true" stored="true"/>
在javaBean中配置注解
@Field(“value”) value与 field 中 name相同,以将javabean属性映射到field中
public class User {
@Field("id")
private String id;
@Field("user_name")
private String name;
@Field("user_sex")
private String sex;
@Field("user_age")
private String age;
@Field("user_like")
private String[] like;
@Field("user_context")
private String context;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String[] getLike() {
return like;
}
public void setLike(String[] like) {
this.like = like;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public User(String id, String name, String sex, String age, String[] like,
String context) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.like = like;
this.context = context;
}
public User(){
}
}
/**
* @Description:添加bean对象
* @throws IOException
* @throws SolrServerException
*/
public void addBean() throws IOException, SolrServerException{
//获取连接
SolrClient solrClient = new HttpSolrClient.Builder(solr_url).build();
//创建user
User user = new User();
user.setId(UUID.randomUUID().toString());
user.setName("张五");
user.setAge("22");
user.setSex("男");
String[] like = {"游泳","唱歌"};
user.setLike(like);
user.setContext("一个热情,开朗的人");
//添加对象
UpdateResponse updateResponse = solrClient.addBean(user);
//提交
solrClient.commit();
solrClient.close();
}
public void getBean() throws SolrServerException, IOException{
//获取连接
SolrClient solrClient = new HttpSolrClient.Builder(solr_url).build();
//创建solrQuery对象,设置条件
SolrQuery solrQuery = new SolrQuery("user_name:张*");
//添加回显内容
solrQuery.addField("id");
solrQuery.addField("user_name");
solrQuery.addField("user_sex");
solrQuery.addField("user_age");
solrQuery.addField("user_like");
solrQuery.addField("user_context");
//设置每页显示多少条
solrQuery.setRows(15);
//执行查询
QueryResponse queryResponse = solrClient.query(solrQuery);
//获取doc文档
List<User> users = queryResponse.getBeans(User.class);
//遍历
for (User user : users) {
System.out.println(
"id:"+user.getId()+"\t name:"
+ user.getName()+"\t age:"
+ user.getAge()+"\t sex:"
+ user.getSex()+"\t like:"
+ user.getLike()+"\t contest:"
+user.getContext()
);
}
}