solr 4.4 TWO solr一些基本操作

package data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
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;

public class Test {
	private final static String URL = "http://localhost:1234/solr";
	private static SolrServer server;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		server = new HttpSolrServer(URL);
		test06();
	}
	
	public static void test06() {
        try {
            SolrQuery query = new SolrQuery("msg_title:solr");
            query.setHighlight(true).setHighlightSimplePre("<span class='highligter'>")
                .setHighlightSimplePost("</span>").setStart(0).setRows(5);

            //设置哪些字段高亮 hl.fl= highlight的缩写 固定的
            query.setParam("hl.fl", "msg_title,msg_content");
            QueryResponse resp = server.query(query);
            //查询出来的结果都保存在SolrDocumentList中
            SolrDocumentList sdl = resp.getResults();
            System.out.println(sdl.getNumFound());
            for (SolrDocument sd : sdl) {
                String id = (String) sd.getFieldValue("id");

                    //取出高亮字段 
                System.out.println(resp.getHighlighting().get(id).get("msg_title"));
            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
    }
	
	 public static void test04() {
	        try {
	            //定义查询字符串
	            SolrQuery query = new SolrQuery("*");

	               //分页 
	            query.setStart(0);
	            query.setRows(100);
                    //添加搜索条件
                   query.addFilterQuery("msg_title:工作");
	            QueryResponse resp = server.query(query);
	            //查询出来的结果都保存在SolrDocumentList中
	            SolrDocumentList sdl = resp.getResults();
	            System.out.println(sdl.getNumFound());
	            for (SolrDocument sd : sdl) {
	                //                System.out.println(sd);
	                System.out.println("***************");
	                System.out.println("msg_title:"+sd.getFieldValue("msg_title"));
	                System.out.println("msg_content:"+sd.getFieldValue("msg_content"));
	            }
	        } catch (SolrServerException e) {
	            e.printStackTrace();
	        }
	    }
	
	public static void test02() {
        try {
            List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id", "7");
            doc.addField("msg_title", "好好看看001");
            doc.addField("msg_content", "slor总算可以正式工作了");
            docs.add(doc);
            doc = new SolrInputDocument();
            doc.addField("id", "8");
            doc.addField("msg_title", "<p>测试一下solr的添加</p>");
            doc.addField("msg_content", "看看能不能添加一个列表信息");
            docs.add(doc);
            server.add(docs);
            server.optimize();
            server.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}



参考文章: http://www.myexception.cn/program/1131616.html

你可能感兴趣的:(Solr)