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);
}  
 
}


0 0