@Test
public void addDocument() throws Exception{
SolrServer solr = new HttpSolrServer("http://localhost:8080/solr");
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "001");
document.addField("title", "这是新的域");
solr.add(document);
solr.commit();
}
@Test
public void deleteDocument() throws Exception{
SolrServer solr = new HttpSolrServer("http://localhost:8080/solr");
solr.deleteById("001");
solr.commit();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
/**
* 使用@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 + "]";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
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);
}
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();
params.set("q", "*:*");
params.set("start", 0);
params.set("rows", "5");
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"));
}
}
}
public static void main(String[] args) {
init();
SolrTest.testQueryAll();
}
}