solrj客户端例子 自己备注

package test.src;


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


public class Business {
@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_;
}

}


package test.src;


import java.util.ArrayList;


import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.DisMaxParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.handler.RequestHandlerBase;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;


public class CustomizedRequestHandler extends RequestHandlerBase{


public String getDescription() {
return "Solr customized";
}


public String getSource() {
return "20150929.1545";
}


public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse res)throws Exception {
SolrParams params = req.getParams();
System.out.println("customized ......");
String uri = params.get(CommonParams.DF);
String fq = params.get(CommonParams.FQ);
String addr = params.get("f.address.河北省三河市");
String name = "fd";//params.get("name");
res.add("query", req.getParamString());
res.add("desc", name+"-"+addr+"-"+uri+"-"+fq);
}


}


package test.src;


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


public class Goods {


@Field
private String id;

@Field
private String name;

@Field
private String desc;

@Field
private String businessId;

@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 getDesc() {
return desc;
}


public void setDesc(String desc) {
this.desc = desc;
}


public String getBusinessId() {
return businessId;
}


public void setBusinessId(String businessId) {
this.businessId = businessId;
}


public Long getVersion() {
return version;
}


public void setVersion(Long version) {
this.version = version;
}



}


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 SolrExamples {


public static void main(String[] args) {
HttpSolrServer server = getServer("goods");
try {
/*queryDom(server);
queryBean(server);
delete(server);
addBusiness(server);*/
addGoods(server);
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private static void delete(HttpSolrServer server) throws SolrServerException, IOException {
server.deleteByQuery("*:*");
}


private static void addBusiness(HttpSolrServer server) throws IOException, SolrServerException {
Business business = new Business();
business.setId("20151002");
business.setName("团车网");
business.setAddress("北京市海淀区羊坊店路");
server.addBean(business);
server.commit();
}

private static void addGoods(HttpSolrServer server)throws IOException,SolrServerException{
Goods goods= new Goods();
goods.setId("20151004");
goods.setName("团车养车");
goods.setBusinessId("20151002");
goods.setDesc("养车4S超优惠");
server.addBean(goods);
server.commit();
}


private static void queryDom(HttpSolrServer server)throws SolrServerException {
SolrQuery q = new SolrQuery();
q.set("q", "name:ti");
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)throws SolrServerException {
SolrQuery q = new SolrQuery();
q.set("q", "name:燕郊新闻");
QueryResponse qr = server.query(q);
List<Business> beanList = qr.getBeans(Business.class);
for(Business person2:beanList){
System.out.println("===="+person2.getAddress()+"=="+person2.getName());
}
}


private static HttpSolrServer getServer(String core) {
String url = "http://localhost:8983/solr";
if(core!=null && !core.trim().equals("")){
url += "/"+core;//多核
}
HttpSolrServer server = new HttpSolrServer(url);
server.setSoTimeout(3000); 
server.setConnectionTimeout(1000);
server.setDefaultMaxConnectionsPerHost(1000);
server.setMaxTotalConnections(10);
server.setFollowRedirects(false); 
server.setAllowCompression(true);
server.setMaxRetries(1);
return server;
}


}


package test.src;


import java.util.ResourceBundle;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.springframework.beans.factory.FactoryBean;


public class SolrServerFactory implements FactoryBean<SolrServer>{


private static SolrServer solrServer = null;

private static ResourceBundle res = null;

private static final Object lock = new Object();

public SolrServer getObject() throws Exception {
if(solrServer == null){
synchronized (lock) {
if(solrServer == null){
solrServer = getServer();
}
}
return solrServer;
}
return solrServer;
}



public Class<?> getObjectType() {
return SolrServer.class;
}



public boolean isSingleton() {
return true;
}

public void init()throws Exception{
res = ResourceBundle.getBundle("solr.properties");
}

public void destroy()throws Exception{
solrServer = null;
}

private static HttpSolrServer getServer() {
String url = res.getString("solr.url");
HttpSolrServer server = new HttpSolrServer(url);
server.setSoTimeout(Integer.valueOf(res.getString("solr.sotimeout")));
server.setConnectionTimeout(Integer.valueOf(res.getString("solr.connecttimeout")));
server.setDefaultMaxConnectionsPerHost(Integer.valueOf(res.getString("solr.defaultmaxconnectionsperhost")));
server.setMaxTotalConnections(Integer.valueOf(res.getString("solr.maxtotalconnections")));
server.setFollowRedirects(false);
server.setAllowCompression(true);
server.setMaxRetries(Integer.valueOf(res.getString("solr.maxretries")));
return server;
}
}


solr.url=http://localhost:8983/solr
solr.sotimeout=3000
solr.connecttimeout=1000
solr.defaultmaxconnectionsperhost=1000
solr.maxtotalconnections=10
solr.maxretries=1

你可能感兴趣的:(solrj客户端例子 自己备注)