您的位置:首页 > 编程语言 > Java开发

java 测试 solr更新和查询

2016-02-20 16:29 369 查看
@Test
public void addDocument() throws Exception{
//1.创建链接
SolrServer solr = new HttpSolrServer("http://localhost:8080/solr");

//2.创建一文档对象
SolrInputDocument document = new SolrInputDocument();

//3.向文档对象中添加域 (先定义后使用)
document.addField("id", "001");
document.addField("title", "这是新的域");

//4.提交文档到索引库
solr.add(document);

//5.提交
solr.commit();
}

@Test
public void deleteDocument() throws Exception{
//1.创建链接
SolrServer solr = new HttpSolrServer("http://localhost:8080/solr");

//2.根据id删除
solr.deleteById("001");

//根据查询删除
//solr.deleteByQuery("*:*");

//.提交
solr.commit();
}


/**
* 使用@Field注解的属性要和Solr配置的Field对应。
用于更新、插入 到索引库中的bean类
* @author w7
*
*/
public class Books {

@Field
private String id;
@Field
private String bname;
@Field
private String bauthor;
@Field
private String bprice;
@Field
private String bcurrprice;
@Field
private String bdiscount;
@Field
private String bpress;
@Field
private String bpublishtime;
@Field
private String bedition;
@Field
private String bpagenum;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getBauthor() {
return bauthor;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
public String getBprice() {
return bprice;
}
public void setBprice(String bprice) {
this.bprice = bprice;
}
public String getBcurrprice() {
return bcurrprice;
}
public void setBcurrprice(String bcurrprice) {
this.bcurrprice = bcurrprice;
}
public String getBdiscount() {
return bdiscount;
}
public void setBdiscount(String bdiscount) {
this.bdiscount = bdiscount;
}
public String getBpress() {
return bpress;
}
public void setBpress(String bpress) {
this.bpress = bpress;
}
public String getBpublishtime() {
return bpublishtime;
}
public void setBpublishtime(String bpublishtime) {
this.bpublishtime = bpublishtime;
}
public String getBedition() {
return bedition;
}
public void setBedition(String bedition) {
this.bedition = bedition;
}
public String getBpagenum() {
return bpagenum;
}
public void setBpagenum(String bpagenum) {
this.bpagenum = bpagenum;
}
@Override
public String toString() {
return "Books [id=" + id + ", bname=" + bname + ", bauthor=" + bauthor + ", bprice=" + bprice + ", bcurrprice="
+ bcurrprice + ", bdiscount=" + bdiscount + ", bpress=" + bpress + ", bpublishtime=" + bpublishtime
+ ", bedition=" + bedition + ", bpagenum=" + bpagenum + "]";
}

}


/**
* solr测试
*
* @author w7
*
*/
public class SolrTest {
private static SolrServer server;

private static final String DEFAULT_URL = "http://localhost:8080/solr";

public static void init() {
server = new HttpSolrServer(DEFAULT_URL);
}

// 添加 book 到索引库
public static void indexBooks(Books book) {
try {
UpdateResponse response = server.addBean(book);

server.commit();
System.err.println(response.getStatus());// 响应状态

} catch (IOException | SolrServerException e) {
e.printStackTrace();
}
}

// 查询
public static void testQueryAll() {
SolrQuery params = new SolrQuery();

// 查询关键词,*:*代表所有属性、所有值,即所有index
params.set("q", "*:*");

// 分页,start=0就是从0开始,rows=5当前返回5条记录,第二页就是变化start这个值为5就可以了。
params.set("start", 0);
params.set("rows", "5");

// 排序,如果按照id排序,那么 写为: id desc(or asc)
params.set("sort", "id asc");

QueryResponse response = null;
try {

response = server.query(params);
} catch (SolrServerException e) {
e.printStackTrace();
}

if (response != null) {
System.out.println("Search Results: ");
SolrDocumentList list = response.getResults();
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).get("bpress"));//根据key取值
}
}

}

//测试:
public static void main(String[] args) {
init();
SolrTest.testQueryAll();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: