solr查询索引

import java.net.MalformedURLException;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

public class Testsolrj {

	private static final String SOLR_URL = "http://localhost/solr";
	// "http://10.0.8.10:8081/solr/spacearticle/";
	private CommonsHttpSolrServer solrServer = null;

	public Testsolrj() {
		try {
			solrServer = new CommonsHttpSolrServer(SOLR_URL);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

	public void search() {
		SolrQuery query = new SolrQuery();
		query.setQuery("geo_title:业务");
		
		query.set("start", 0);
		query.set("rows", 1);
		
		// 排序用的
		// 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("geo_title");
				String id = (String) doc.getFieldValue("geo_content");
				System.out.println(id);
				System.out.println(title);
			}
		} catch (SolrServerException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Testsolrj sj = new Testsolrj();
		sj.search();
	}

}

 

你可能感兴趣的:(Solr)