Solr之搜索实现

下面来记录并分享下solr搜索接口的实现:

示例如下

public class SolrSearchTest{
	private static final String SOLR_URL = "http://localhost:8080/solr";
	private CommonsHttpSolrServer solrServer = null;
	public SolrSearchTest(){
		try{
			solrServer = new CommonsHttpSolrServer(SOLR_URL);
			solrServer.setMaxTotal(10);
			solrServer.setSoTimeout(1000);
			solrServer.setConnectionTimeout(1000);
		}catch(MalformedUTLException e){
			e.printStackTrace();
		}
	}
	public void search(){
		SolrQuery query = new SolrQuery();
		query.setQuery("title:测试");
		query.addFilterQuery("type:{0 TO 3}");
		query.addSortField("price", SolrQuery.ORDER.asc);
		try{
			QueryResponse rsp = solrServer.query(query);
			SolrDocumentList docs = rsp.getResults();
			for(SolrDocument doc : docs){
				String title = (String)doc.getFieldValue("title"):
				Integer id = (Integer)doc.getFieldValue("id");
			}
		}catch(SolrServerException e){
			e.printStackTrace();
		}
	}
	public static void main(String[] args){
		SolrSearchTest sjt = new SolrSearchTest();
		sjt.search();
	}
}

你可能感兴趣的:(solr)