solrJ查询使用方法

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)