步骤 2 : 模仿和排错 步骤 3 : SolrUtil 步骤 4 : TestSolr4j 
					老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。  
					
				运行TestSolr4j,可以看到如图所示的查询了10条出来的效果 
					在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 
 
					
				模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 这里提供了绿色安装和使用教程:diffmerge 下载和使用教程 
					SolrUtil 增加分页查询的方法  
					
				public static QueryResponse query(String keywords,int startOfPage, int numberOfPage) throws SolrServerException, IOException { SolrQuery query = new SolrQuery(); query.setStart(startOfPage); query.setRows(numberOfPage); query.setQuery(keywords); QueryResponse rsp = client.query(query); return rsp; } package how2java;
import java.io.IOException;
import java.util.List;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList;
public class SolrUtil {
    public static SolrClient client;
    private static String url;
    static {
        url = "http://localhost:8983/solr/how2java";
        client = new HttpSolrClient.Builder(url).build();
    }
    public static <T> boolean batchSaveOrUpdate(List<T> entities) throws SolrServerException, IOException {
        DocumentObjectBinder binder = new DocumentObjectBinder();
		int total = entities.size();
		int count=0;
        for (T t : entities) {
            SolrInputDocument doc = binder.toSolrInputDocument(t);
            client.add(doc);
            System.out.printf("添加数据到索引中,总共要添加 %d 条记录,当前添加第%d条 %n",total,++count);
		}
        client.commit();
        return true;
    }
    public static QueryResponse query(String keywords,int startOfPage, int numberOfPage) throws SolrServerException, IOException {
        SolrQuery query = new SolrQuery();
        query.setStart(startOfPage);
        query.setRows(numberOfPage);
        
        query.setQuery(keywords);
        QueryResponse rsp = client.query(query);
        return rsp;
    }
}
 
								
										
									
								
							
					拿到分页查询的结果,遍历出来
					 
					
				package how2java;
import java.io.IOException;
import java.util.Collection;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
public class TestSolr4j {
	public static void main(String[] args) throws SolrServerException, IOException {
		//查询
		QueryResponse queryResponse = SolrUtil.query("name:手机",0,10);
		SolrDocumentList documents=	queryResponse.getResults();
		System.out.println("累计找到的条数:"+documents.getNumFound());
		if(!documents.isEmpty()){
			
			Collection<String> fieldNames = documents.get(0).getFieldNames();
			for (String fieldName : fieldNames) {
				System.out.print(fieldName+"\t");
			}
			System.out.println();
		}
		
		for (SolrDocument solrDocument : documents) {
			
			Collection<String> fieldNames= solrDocument.getFieldNames();
			
			for (String fieldName : fieldNames) {
				System.out.print(solrDocument.get(fieldName)+"\t");
				
			}	
			System.out.println();
			
		}
	}
}
 
								
										
									
								
							
				HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
			 
			 
			
			
			
			
			
		
			
			提问之前请登陆
			
		 
		提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢	
	 
 | 
	|||||||||