欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

solrJ查询使用方法

程序员文章站 2024-02-22 12:03:10
...

	public class SolrJSearch {  
	      
	    private static final String SOLR_URL =   
	        //"http://10.0.8.10:8081/solr/spacearticle/";  
	        "http://localhost:8080/solr/";  
	    private CommonsHttpSolrServer solrServer = null;  
	      
	    public SolrJSearch(){  
	        try {  
	            solrServer = new CommonsHttpSolrServer(SOLR_URL);  
	        } catch (MalformedURLException e) {  
	            e.printStackTrace();  
	        }  
	    }  
	      
	    public void search(){  
	        SolrQuery query = new SolrQuery();  
	        query.setQuery("A_Title:新浪科技");  
	        //排序用的  
	        //query.addSortField( "price", SolrQuery.ORDER.asc );    
	        try {  
	            QueryResponse rsp = solrServer.query( query );  
	              
	            SolrDocumentList docs = rsp.getResults();  
	            System.out.println("文档个数:" + docs.getNumFound());  
	            System.out.println("查询时间:" + rsp.getQTime());  
	            for (SolrDocument doc : docs) {  
	                String title = (String) doc.getFieldValue("A_Title");  
	                Integer id = (Integer) doc.getFieldValue("A_ID");  
	                System.out.println(id);  
	                System.out.println(title);  
	            }  
	        } catch (SolrServerException e) {  
	            e.printStackTrace();  
	        }   
	    }  
	      
	    public static void main(String[] args) {  
	        SolrJSearch sj = new SolrJSearch();  
	        sj.search();  
	    }  
	}  
 
相关标签: Solr