solrcloud分布式搜索对文档内容的索引java
来源:互联网 发布:淘宝代办食品流通证 编辑:程序博客网 时间:2024/05/01 20:18
addDoc:
package solr.addDocument;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
/**
* =用sql语句查询各个类别的文件(环评批复,报告书。。。),把路径存在list里面,在同一调用不同的core生成各个的索引,
* 查询结合类SolrJSearcheDemo即可。
*
* @author Administrator
*
*/
public class TestCreate {
private static Logger log = Logger.getLogger(TestCreate.class);
/**
* 生成文件索引方法
*
* @param id
* @param fileurl
* @param filename
* @param homename
* @param mytitle
* @param savetime
* @param myindextype
* @throws Exception
*/
public static void main(String args[]) {
// File file = new File("D:\\logs");
try {
indexFilesSolr("C:/Users/gnet/Desktop/SRCA根证书安装说明手册.doc", "C:/Users/gnet/Desktop/SRCA根证书安装说明手册.doc", "SRCA根证书安装说明手册.doc", "","filecore",
"2014-09-11 15:19:06", "B", "2014" );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void indexFilesSolr(String id, String fileurl,
String filename, String homename, String mytitle, String savetime,
String myindextype, String myyears) throws Exception {
String zkHost = "192.168.4.77:2181";
String defaultCollection = "collection1";
CloudSolrServer server = new CloudSolrServer(zkHost);
server.setDefaultCollection(defaultCollection);
// SolrServer solr=new HttpSolrServer(getServerurl()+homename);
ContentStreamUpdateRequest up = new ContentStreamUpdateRequest(
"/update/extract");
String contenttype = getFileContentType(filename);
if (!contenttype.equals("othertype")) {
File file = new File(fileurl);
if (file.exists()) {
log.info("开始建索引:" + fileurl);
up.addFile(file, contenttype);
up.setParam("literal.id", id);
up.setParam("literal.mytitle", mytitle);
up.setParam("literal.mytime", dataTurntoLong(savetime));
up.setParam("literal.myindextype", myindextype);
up.setParam("literal.myyears", myyears);
up.setParam("fmap.content", "content");
up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
server.request(up);
log.info("结束建索引:" + fileurl);
} else {
// log.info("文件不存在");
}
}
}
/**
* 获得毫秒数
*
* @param date
* @return
*/
public static String dataTurntoLong(String date) {
Date d = null;
try {
d = new SimpleDateFormat("yyyyMMddHHmmss").parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return String.valueOf(d.getTime());
}
/**
* 获取系统路径
*
* @return
*/
public static String getServerurl() {
ResourceBundle res = ResourceBundle.getBundle("solrserver");
return res.getString("serverurl");
}
/**
* 根据文件名获取文件的ContentType类型
*
* @param filename
* @return
*/
public static String getFileContentType(String filename) {
String contentType = "";
String prefix = filename.substring(filename.lastIndexOf(".") + 1);
if (prefix.equals("xlsx")) {
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
} else if (prefix.equals("pdf")) {
contentType = "application/pdf";
} else if (prefix.equals("doc")) {
contentType = "application/msword";
} else if (prefix.equals("txt")) {
contentType = "text/plain";
} else if (prefix.equals("xls")) {
contentType = "application/vnd.ms-excel";
} else if (prefix.equals("docx")) {
contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
} else if (prefix.equals("ppt")) {
contentType = "application/vnd.ms-powerpoint";
} else if (prefix.equals("pptx")) {
contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
}
else {
contentType = "othertype";
}
return contentType;
}
/**
* 返回文件ContentType
*
* @param paths
* @return
*/
public static String getContentType(String paths) {
Path path = Paths.get(paths);
String contentType = null;
try {
contentType = Files.probeContentType(path);
} catch (IOException e) {
e.printStackTrace();
}
log.info("文件类型 : " + contentType);
return contentType;
}
// public static void main(String args[]) {
// File file = new File("D:\\logs");
// indexFilesSolr(path, path, a, "filecore", a,
// "2014-09-11 15:19:06", type, "200" + i);
// String files[] = file.list();
// for (int i = 0; i < files.length; i++) {
// String a = files[i];
// String path = "D:/logs/" + files[i];
// String type = "A";
// try {
// if (i % 2 == 0) {
// type = "B";
// }
// indexFilesSolr(path, path, a, "filecore", a,
// "2014-09-11 15:19:06", type, "200" + i);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// }
}
addindex:
package solr.addindex;
import java.io.IOException;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.common.SolrInputDocument;
public class SolrCloudSolrjPopulator {
public static void main(String[] args) throws IOException, SolrServerException {
String zkHost = "192.168.233.128:2181";
String defaultCollection = "collection1";
CloudSolrServer server = new CloudSolrServer(zkHost);
server.setDefaultCollection(defaultCollection);
for (int i = 0; i < 1000; ++i) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("cat", "book");
doc.addField("id", "book-" + i);
doc.addField("name", "The Legend of Po part " + i);
server.add(doc);
if (i % 100 == 0)
System.out.println(i);
server.commit(); // periodically flush
}
server.commit();
}
}
search:
import java.net.MalformedURLException;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.ModifiableSolrParams;
public class SolrCloudSolrJSearcher {
public static void main(String[] args) throws MalformedURLException,
SolrServerException {
String zkHost = "localhost:2181";
String defaultCollection = "collection1";
CloudSolrServer solr = new CloudSolrServer(zkHost);
solr.setDefaultCollection(defaultCollection);
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("q", "cat:electronics");
params.set("defType", "edismax");
params.set("start", "0");
QueryResponse response = solr.query(params);
SolrDocumentList results = response.getResults();
for (int i = 0; i < results.size(); ++i) {
System.out.println(results.get(i));
}
}
}
- solrcloud分布式搜索对文档内容的索引java
- 分布式搜索elasticsearch 索引文档的增删改查 入门
- 分布式搜索elasticsearch 索引文档的检索 入门
- 分布式搜索elasticsearch 索引文档的增删改查 入门
- 分布式搜索elasticsearch 索引文档的增删改查 入门
- SolrCloud之分布式索引及与Zookeeper的集成
- SolrCloud之分布式索引及与Zookeeper的集成
- SolrCloud之分布式索引及与Zookeeper的集成
- 基于Solr和Zookeeper的分布式搜索方案SolrCloud
- 修正Windows XP对文档内容搜索的支持问题
- SolrCloud的分布式实现
- Solr分布式索引SolrCloud原理总结
- Solr分布式索引SolrCloud原理总结
- 分布式搜索方案选型之三:SolrCloud
- solr solrcloud 检索 搜索 zookeeper 分布式 部署
- solrCloud 4.7 分布式搜索重要bug
- 分布式搜索方案选型之三:SolrCloud
- 分布式搜索之搭建Solrcloud(Solr集群)
- 交叉编译和交叉调试环境搭建及使用
- Python笔记
- 算是新发现吗
- 给独立开发者的经验分享——编程语言
- IT公司面试之字符串常见面试题
- solrcloud分布式搜索对文档内容的索引java
- APUE之实际用户ID、有效用户ID和保存设置用户ID
- 如何在Struts中配置数据源在什么文件?用什么标签?如何取出Data Source?
- c#字符串操作
- 使用SSH无密码验证访问树莓派
- 在一个int数组里查找出所有这样的数,它大于等于左侧所有数,小于等于右侧所有数。
- 纯 java 实现 Http 资源读取工具,支持发送和接收数据
- 状压DP poj 2288 Islands and Bridges
- jboss历史版本地址url