Solr的index索引的增删改查

前提:使用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);
}  
 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值