Solr的index索引的增删改查
来源:互联网 发布:c语言自己写头文件 编辑:程序博客网 时间:2024/05/29 07:33
前提:使用Jetty容器安装的solr源码包上
新建一个类SolrIndex_Demo,完成索引的添加,具体代码如下:
package test_solr_test_wsp;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
public class SolrIndex_Demo {
public static void main(String[] args) throws Exception {
String serverUrl = (args != null && args.length > 0) ? args[0]
: "http://localhost:8983/solr/collection1";
SolrServer solrServer = new HttpSolrServer(serverUrl);
SolrInputDocument doc1 = new SolrInputDocument(); //XML文档的添加
doc1.setField("id", "solrJTest3"); //设置solrId
doc1.setField("url", "http://www.163.com/");
solrServer.add(doc1);
SolrInputDocument doc2 = new SolrInputDocument();
doc2.setField("id", "solrJTest4");
doc2.setField("url", "http://www.179.com/");
doc2.setField("name","王少平,杭州市区域,中国");
doc2.addField("manu","世界那么大,我想去看看");
solrServer.add(doc2); //提交XML文档
solrServer.optimize(); //solr的优化
solrServer.commit(true,true);
}
}
这是完成索引的添加,添加完再solr的查询上可以查看:
或者用代码也可以查看的到:
package test_solr_test_wsp;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
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 SolrSelect_Demo{
public static void main(String[] args) throws Exception {
String serverUrl = (args != null && args.length > 0) ? args[0]
: "http://localhost:8983/solr/collection1";
SolrServer solrServer = new HttpSolrServer(serverUrl);
//读取输入参数作为查询关键字,若无关键字,则查询全部内容。
String queryString = (args != null && args.length > 1) ? args[1] : "*:*";
SolrQuery solrQuery = new SolrQuery(queryString);
solrQuery.setRows(10); //设置可以查看的索引数目
QueryResponse resp = solrServer.query(solrQuery);
SolrDocumentList hits = resp.getResults(); //查询到的数据放到list链表
for(SolrDocument doc : hits ){ //遍历查询的链表
System.out.println(doc);
//System.out.println(doc.getFieldValue("id").toString() + " : " + doc.getFieldValue("url")+"::"+doc.getFieldValue("name"));
}
}
删除索引代码:
package test_solr_test_wsp;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
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 SolrDelete_Demo {
public static void main(String[] args) throws Exception {
String serverUrl = (args != null && args.length > 0) ? args[0]
: "http://localhost:8983/solr/collection1";
SolrServer solrServer = new HttpSolrServer(serverUrl);
//String delstring="163";
solrServer.deleteById("solrJTest3"); //根据ID删除索引
solrServer.commit(true,true);
}
}
- Solr的index索引的增删改查
- solr的增删改查
- Solr索引增删改查
- Solr的增删改查注意事项
- Solr索引之增删改查
- 【原创】Java+solrJ实现对solr索引的增删改查
- lucene4.4 索引的增删改查
- lucene索引的增删改查
- 02-lucene索引的增删改查
- mysql索引的增删改查
- 搜索引擎-基于solrj客户端的solr增删改查
- solr的增删改查和高亮以及分组
- lucene索引的增删改查/lucene索引维护
- mysql数据库表字段增删改查及索引的增删改查
- lucene索引库的增删改查操作
- 分布式搜索elasticsearch 索引文档的增删改查 入门
- 分布式搜索elasticsearch 索引文档的增删改查 入门
- Lucene索引的增删改查和二次检索
- android 自定义 Dialog
- 栈在表达式求值中的应用
- UILabel 总结
- AC自动机 hdu2896 hdu3065 病毒侵袭
- ucos源码分析之OSMemCreate()
- Solr的index索引的增删改查
- hdu 5381 The sum of gcd 2015多校联合训练赛#8莫队算法
- javacv使用FileStorage读写xml文件
- 解析excel文件
- Android的selector的使用
- 求无向图的割点 (poj 1144 Network)
- 分类精度评价方法
- 使用Android Studio打包含带有源码的AAR包
- jsp编码