【Solr in Action】eclipse+java+solrj环境下实现solr客户端数据上传
来源:互联网 发布:阿卡波糖 减肥 知乎 编辑:程序博客网 时间:2024/06/05 17:08
-------------------------------------------------说明------------------------------------------------
solr服务器版本为7.1,程序引用的jar包是4.7版本。
-------------------------------------------------图示------------------------------------------------
运行程序上传数据后,在solr服务端查询数据的图示如下:
-------------------------------------------------程序------------------------------------------------
import java.io.PrintStream;
import java.util.ArrayList;import java.util.Collections;
import java.util.List;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
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;
import org.apache.solr.common.SolrInputDocument;
/**
* Minimal example client to index documents using SolrJ version 4.x+
*/
public class ExampleSolrJClient {
public static void main(String[] args) throws Exception{
String serverUrl= "http://localhost:8983/solr/newcore1";
// Get a connection to Solr
SolrServer solr = new HttpSolrServer(serverUrl);
// Add some example docs
SolrInputDocument doc1 = new SolrInputDocument();
doc1.setField("id", "1");
doc1.setField("screen_name_s", "@thelabdude");
doc1.setField("type_s", "post");
doc1.setField("lang_s", "en");
doc1.setField("timestamp_tdt", "2012-05-22T09:30:22Z/HOUR");
doc1.setField("favourites_count_ti", "10");
doc1.setField("text_t", "#Yummm :) Drinking a latte at Caffe Grecco in SF's" +
" historic North Beach... Learning text analysis with #SolrInAction by @Manning on my i-Pad");
solr.add(doc1);
SolrInputDocument doc2 = new SolrInputDocument();
doc2.setField("id", "2");
doc2.setField("screen_name_s", "@thelabdude");
doc2.setField("type_s", "post");
doc2.setField("lang_s", "en");
doc2.setField("timestamp_tdt", "2012-05-22T09:30:22Z/HOUR");
doc2.setField("favourites_count_ti", "10");
doc2.setField("text_t", "Just downloaded the MEAP for #SolrInAction from" +
" @Manning http://bit.ly/15tzw to learn more about #Solr http://bit.ly/3ynriE");
doc2.addField("link_ss", "http://manning.com/");
doc2.addField("link_ss", "http://lucene.apache.org/solr/");
solr.add(doc2);
// Make the docs we just added searchable using a "hard" commit
solr.commit(true, true);
// Send and process the first 10 rows of the match all docs query
for (SolrDocument next : simpleSolrQuery(solr, "*:*", 10)) {
prettyPrint(System.out, next);
}
}
static SolrDocumentList simpleSolrQuery(SolrServer solr, String query, int rows) throws SolrServerException {
SolrQuery solrQuery = new SolrQuery(query);
solrQuery.setRows(rows);
QueryResponse resp = solr.query(solrQuery);
SolrDocumentList hits = resp.getResults();
return hits;
}
public String getDescription() {
return "Use SolrJ client API to index some example documents and then query Solr.";
}
static void prettyPrint(PrintStream out, SolrDocument doc) {
List<String> sortedFieldNames = new ArrayList<String>(doc.getFieldNames());
Collections.sort(sortedFieldNames);
out.println();
for (String field : sortedFieldNames) {
out.println(String.format("\t%s: %s", field, doc.getFieldValue(field)));
}
out.println();
}
}
-------------------------------------------------结束------------------------------------------------
阅读全文
0 0
- 【Solr in Action】eclipse+java+solrj环境下实现solr客户端数据上传
- Solr Java客户端(SolrJ)
- Solr之客户端SolrJ
- SolrJ是操作Solr的JAVA客户端
- Solr JAVA客户端SolrJ的使用
- Solr java客户端solrJ的CRUD操作
- solr的客户端调用solrj
- JAVA环境下利用solrj二次开发SOlR搜索的环境部署常见错误
- Solr JAVA客户端SolrJ 4.9使用示例教程
- Solr JAVA客户端SolrJ 4.9使用示例教程
- eclipse下新建一个工程利用solrj来操作solr
- Solr 读写(Java SolrJ)
- 【Apache Solr系列之五】使用Solr客户端SolrJ实现多层面统计
- Solr In Action
- Solr in action 1
- Solr in action 2
- solr.in.action-ch05
- solr.in.action-ch06
- 在VS.NET 的项目中使用生成事件
- 35. Search Insert Position
- redis源码分析(一)复习redis命令、持久化方案、主从同步原来、配置
- MyEclipse8.5 Java代码破解
- JAVA 对象引用&对象赋值
- 【Solr in Action】eclipse+java+solrj环境下实现solr客户端数据上传
- java poi 的excel的导出
- 一些测试工具
- 贝塞尔曲线弹性绳模拟伸长和收缩
- SQL ORDER BY(排序)
- 利用小技巧用于跟踪函数的进出过程
- Algorithm之路十九:Remove Nth Node From End of List
- 20171220记录我的程序猿工作。。。
- 按住鼠标左键直接拖动页面-竖向滚动