package com.su.search.client;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Date;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
/**
* Description:
* @author LiChunming
* @version V1.0
* @createDateTime:2012-2-27 下午03:49:04
* @Company: MSD.
* @Copyright: Copyright (c) 2011
**/
public class SolrServer {
private static SolrServer solrServer = null;
private static HttpSolrServer server=null;
private static String url="http://localhost:8080/solr/core0";
public static synchronized SolrServer getInstance() {
if (solrServer==null){
solrServer=new SolrServer();
}
return solrServer;
}
public static HttpSolrServer getServer(){
if(server==null){
server = new HttpSolrServer(url);
server.setSoTimeout(1000); // socket read timeout
server.setConnectionTimeout(1000);
server.setDefaultMaxConnectionsPerHost(100);
server.setMaxTotalConnections(100);
server.setFollowRedirects(false); // defaults to false
//allowCompression defaults to false.
//Server side must support gzip or deflate for this to have any effect.
server.setAllowCompression(true);
server.setMaxRetries(1); // defaults to 0. > 1 not recommended.
}
return server;
}
public static void main(String[] args) {
HttpSolrServer solrServer= SolrServer.getInstance().getServer();
SolrInputDocument doc1 = insert("b", "http:vvvvvyyvvs", "b", "b", "b", "b", "b", "b", "b", new Date(), "site");
try {
solrServer.add(doc1);
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
solrServer.commit();
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SolrInputDocument insert(String user, String link, String title,
String desc, String content, String tags, String keywords,
String site, String type, Date date, String name) {
// SitesIndexDTO sitesIndex = new SitesIndexDTO();
SolrInputDocument doc = new SolrInputDocument();
if (link != null && !"".equals(link)) {
doc.addField("link", link);
}
if (title != null && !"".equals(title)) {
doc.addField("title", title);
}
if (keywords != null && !"".equals(keywords)) {
doc.addField("keywords", keywords);
}
if (desc != null && !"".equals(desc)) {
doc.addField("desc", desc);
}
if (content != null && !"".equals(content)) {
doc.addField("content", content);
}
if (tags != null && !"".equals(tags)) {
doc.addField("tags", tags);
}
doc.addField("source", "admin");
if (site != null && !"".equals(site)) {
doc.addField("site", site);
}
if (type != null && !"".equals(type)) {
doc.addField("type", type);
}
if (date != null && !"".equals(date)) {
doc.addField("last_modified",
DateFormatUtils.format(date, "yyyy-MM-dd'T'HH:mm:ss'Z'"));
}
return doc;
}
}
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Date;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
/**
* Description:
* @author LiChunming
* @version V1.0
* @createDateTime:2012-2-27 下午03:49:04
* @Company: MSD.
* @Copyright: Copyright (c) 2011
**/
public class SolrServer {
private static SolrServer solrServer = null;
private static HttpSolrServer server=null;
private static String url="http://localhost:8080/solr/core0";
public static synchronized SolrServer getInstance() {
if (solrServer==null){
solrServer=new SolrServer();
}
return solrServer;
}
public static HttpSolrServer getServer(){
if(server==null){
server = new HttpSolrServer(url);
server.setSoTimeout(1000); // socket read timeout
server.setConnectionTimeout(1000);
server.setDefaultMaxConnectionsPerHost(100);
server.setMaxTotalConnections(100);
server.setFollowRedirects(false); // defaults to false
//allowCompression defaults to false.
//Server side must support gzip or deflate for this to have any effect.
server.setAllowCompression(true);
server.setMaxRetries(1); // defaults to 0. > 1 not recommended.
}
return server;
}
public static void main(String[] args) {
HttpSolrServer solrServer= SolrServer.getInstance().getServer();
SolrInputDocument doc1 = insert("b", "http:vvvvvyyvvs", "b", "b", "b", "b", "b", "b", "b", new Date(), "site");
try {
solrServer.add(doc1);
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
solrServer.commit();
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SolrInputDocument insert(String user, String link, String title,
String desc, String content, String tags, String keywords,
String site, String type, Date date, String name) {
// SitesIndexDTO sitesIndex = new SitesIndexDTO();
SolrInputDocument doc = new SolrInputDocument();
if (link != null && !"".equals(link)) {
doc.addField("link", link);
}
if (title != null && !"".equals(title)) {
doc.addField("title", title);
}
if (keywords != null && !"".equals(keywords)) {
doc.addField("keywords", keywords);
}
if (desc != null && !"".equals(desc)) {
doc.addField("desc", desc);
}
if (content != null && !"".equals(content)) {
doc.addField("content", content);
}
if (tags != null && !"".equals(tags)) {
doc.addField("tags", tags);
}
doc.addField("source", "admin");
if (site != null && !"".equals(site)) {
doc.addField("site", site);
}
if (type != null && !"".equals(type)) {
doc.addField("type", type);
}
if (date != null && !"".equals(date)) {
doc.addField("last_modified",
DateFormatUtils.format(date, "yyyy-MM-dd'T'HH:mm:ss'Z'"));
}
return doc;
}
}