添加信息对象
User.java---------注意需要使用@Field注解(对应配置文件managed-schema中域的name名称)
package com.kennosaur.pojo;
import java.io.Serializable;
import org.apache.solr.client.solrj.beans.Field;
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Field("id")
private String id;
@Field("user_name")
private String name;
@Field("user_age")
private String age;
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 getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
配置managed-schema的配置文件
<field name="id" type="string" multiValued="false" indexed="true" required="true" stored="true"/>
<field name="user_age" type="string" multiValued="false" indexed="false" stored="false"/>
<field name="user_name" type="string" multiValued="false" indexed="false" stored="false"/>
测试代码
1.向solr中添加user对象---------solr.addBean(user);
2.从solr中取出user对象-----------List<User> beans = solrServer.getBinder().getBeans(User.class, sds);
package com.kennosaur.solr;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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 com.kennosaur.pojo.User;
public class Testsolr1 {
public Testsolr1() {
}
public static void main(String[] args) throws Exception {
Testsolr1 testsolr1 = new Testsolr1();
testsolr1.solrToUser();
// List<String> querySolr = testsolr1.querySolr("张");
// System.out.println(querySolr.toString());
// testsolr1.deleteDocumentById();
}
// 指定solr服务器的地址
private final static String SOLR_URL = "http://localhost:8080/solr/";
// 创建一个客户端
public HttpSolrClient createSolrServer() {
HttpSolrClient solr = null;
solr = new HttpSolrClient.Builder(SOLR_URL).withConnectionTimeout(10000).withSocketTimeout(60000).build();
return solr;
}
//将solr中的document读取出来并转换成java对象
public void solrToUser() throws SolrServerException, IOException {
HttpSolrClient solrServer = new HttpSolrClient.Builder(SOLR_URL + "mycore/").withConnectionTimeout(10000)
.withSocketTimeout(60000).build();
SolrQuery query = new SolrQuery();
List<User> list = new ArrayList<User>();
// 设置查询条件
query.set("q", "*:*");
// 获取查询结果
QueryResponse response = solrServer.query(query);
SolrDocumentList sds = response.getResults();
System.out.println("===sds.size()====="+sds.size()+"====sds.getNumFound()===="+sds.getNumFound());
List<User> beans = solrServer.getBinder().getBeans(User.class, sds);
for (User user : beans) {
System.out.println(user.toString());
}
}
//添加javaBean对象到solr
public void addUser() throws IOException, SolrServerException {
HttpSolrClient solr = new HttpSolrClient.Builder(SOLR_URL + "mycore").withConnectionTimeout(10000)
.withSocketTimeout(60000).build();
User user = new User();
user.setId("user_001");
user.setName("用户001");
user.setAge("22");
solr.addBean(user);
solr.commit();
}
// 添加文档
public void addDoc() throws SolrServerException, IOException {
// 构造一篇文档
SolrInputDocument document = new SolrInputDocument();
// 往doc中添加字段,在客户端这边添加的字段必须在服务端中有过定义
document.addField("id", "1001");
document.addField("name", "solr搜索框架");
// 获得一个solr服务端的请求,去提交 ,选择具体的某一个solr core
HttpSolrClient solr = new HttpSolrClient.Builder(SOLR_URL + "mycore").withConnectionTimeout(10000)
.withSocketTimeout(60000).build();
solr.add(document);
solr.commit();
solr.close();
}
// 删除Solr中新建立的索引
public void deleteDocumentById() throws Exception { // 选择具体的某一个solr core
HttpSolrClient server = new HttpSolrClient.Builder(SOLR_URL + "mycore").withConnectionTimeout(10000)
.withSocketTimeout(60000).build();
// server.deleteById("4"); // 删除所有的索引
server.deleteByQuery("*:*");
// 提交修改
server.commit();
server.close();
}
// 查询
public List<String> querySolr(String name) throws Exception {
HttpSolrClient solrServer = new HttpSolrClient.Builder(SOLR_URL + "mycore/").withConnectionTimeout(10000)
.withSocketTimeout(60000).build();
SolrQuery query = new SolrQuery();
List<String> list = new ArrayList<String>();
// 设置查询条件
// query.set("q", "*:*");
// query.set("fl", "title");
//query.set("q", "title:" + name + "OR author:" + name);
query.set("q", "title:" + name );
// 获取查询结果
QueryResponse response = solrServer.query(query);
SolrDocumentList sds = response.getResults();
System.out.println("===sds.size()====="+sds.size()+"====sds.getNumFound()===="+sds.getNumFound());
for (SolrDocument per : sds) {
String artical_id = (String) per.get("id");
list.add(artical_id);
}
return list;
}
}