solrj 增删改查

jar包

commons-io-2.1.jar

httpclient-4.2.3.jar

httpcore-4.2.2.jar

httpmime-4.2.3.jar

jcl-over-slf4j-1.6.6.jar

jul-to-slf4j-1.6.6.jar

log4j-1.2.16.jar

noggit-0.5.jar

slf4j-api-1.6.6.jar

slf4j-log4j12-1.6.6.jar

wstx-asl-3.2.7.jar

zookeeper-3.4.5.jar

solr-solrj-4.5.1.jar


1、Person类

package test.src;



import org.apache.solr.client.solrj.beans.Field;


public class Person {
@Field
private String id;
@Field
private String name;
@Field
private String address;
@Field
private Long _version_;


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 getAddress() {
return address;
}


public void setAddress(String address) {
this.address = address;
}


public Long get_version_() {
return _version_;
}


public void set_version_(Long _version_) {
this._version_ = _version_;
}

}


2、Solr-Test

package test.src;




import java.io.IOException;
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.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;


public class SolrTest {


public static void main(String[] args) {
HttpSolrServer server = getServer();
//query(server);
//delete(server);
add(server);
}

private static void delete(HttpSolrServer server) {
try {
server.deleteByQuery("*:*");
} catch (SolrServerException | IOException e) {
e.printStackTrace();
}
}


private static void add(HttpSolrServer server) {
Person person = new Person();
person.setId("20150918");
person.setName("燕郊新闻");
person.setAddress("河北省三河市");
try {
server.addBean(person);
} catch (IOException | SolrServerException e) {
e.printStackTrace();
}
try {
server.commit();
} catch (SolrServerException | IOException e) {
e.printStackTrace();
}
}


private static void query(HttpSolrServer server) {
SolrQuery q = new SolrQuery();
q.set("q", "name:燕郊新闻");
try {
queryDom(server, q);
queryBean(server, q);
} catch (SolrServerException e1) {
e1.printStackTrace();
}
}


private static void queryDom(HttpSolrServer server, SolrQuery q)throws SolrServerException {
QueryResponse qr = server.query(q);
SolrDocumentList list = qr.getResults();
System.out.println(list.getNumFound());
System.out.println(qr.getQTime());
for(SolrDocument solrDocument:list){
String address = String.valueOf(solrDocument.getFieldValue("address"));
System.out.println(address);
}
}

private static void queryBean(HttpSolrServer server, SolrQuery q)throws SolrServerException {
QueryResponse qr = server.query(q);
List<Person> beanList = qr.getBeans(Person.class);
for(Person person2:beanList){
System.out.println("===="+person2.getAddress()+"=="+person2.getName());
}
}


private static HttpSolrServer getServer() {
String url = "http://localhost:8983/solr";
HttpSolrServer server = new HttpSolrServer(url);
server.setSoTimeout(3000); // socket read timeout
server.setConnectionTimeout(1000);
server.setDefaultMaxConnectionsPerHost(1000);
server.setMaxTotalConnections(10);
server.setFollowRedirects(false); // defaults to false
server.setAllowCompression(true);
server.setMaxRetries(1);
return server;
}


}

你可能感兴趣的:(solrj 增删改查)