solr的java代码

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

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
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.wltea.analyzer.lucene.IKAnalyzer;


public class SolrUtil {
	//solr服务器所在的地址,core0为自己创建的文档库目录
	private final static String SOLR_URL = "http://localhost:8080/solr/";

	
	IKAnalyzer ik;
	
	/**
	 * 获取客户端的连接
	 * 
	 * @return
	 */
	public HttpSolrClient createSolrServer() {
	    HttpSolrClient solr = null;
	    solr = new HttpSolrClient.Builder(SOLR_URL).withConnectionTimeout(10000).withSocketTimeout(60000).build();
	    return solr;
	}

	/**
	 * 往索引库添加文档
	 * 
	 * @throws SolrServerException
	 * @throws IOException
	 */
	public void addDoc() throws SolrServerException, IOException {
	    SolrInputDocument document = new SolrInputDocument();
	    document.addField("id", "7");
	    document.addField("name", "钢铁侠");
	    document.addField("description", "一个逗比的码农");
	    HttpSolrClient solr = new HttpSolrClient.Builder(SOLR_URL).withConnectionTimeout(10000)
	            .withSocketTimeout(60000).build();
	    solr.add(document);
	    solr.commit();
	    solr.close();
	    System.out.println("添加成功");
	}

	/**
	 * 根据ID从索引库删除文档
	 * 
	 * @throws SolrServerException
	 * @throws IOException
	 */
	public void deleteDocumentById() throws SolrServerException, IOException {
	    HttpSolrClient server = new HttpSolrClient.Builder(SOLR_URL)
	                .withConnectionTimeout(10000)
	                .withSocketTimeout(60000).build();

	    server.deleteById("6");
	    server.commit();
	    server.close();
	}

	/**
	 * 根据设定的查询条件进行文档字段的查询
	 * @throws Exception
	 */
	public void querySolr() throws Exception {

	    HttpSolrClient server = new HttpSolrClient.Builder(SOLR_URL)
	                .withConnectionTimeout(10000)
	                .withSocketTimeout(60000).build();
	    SolrQuery query = new SolrQuery();

	    //下面设置solr查询参数

	    //query.set("q", "*:*");// 参数q  查询所有   
	    query.set("q", "钢铁侠");//相关查询,比如某条数据某个字段含有周、星、驰三个字  将会查询出来 ,这个作用适用于联想查询

	    //参数fq, 给query增加过滤查询条件 
	    query.addFacetQuery("id:[0 TO 9]");
	    query.addFilterQuery("description:一个逗比的码农"); 

	    //参数df,给query设置默认搜索域,从哪个字段上查找
	    query.set("df", "name"); 

	    //参数sort,设置返回结果的排序规则
	    query.setSort("id",SolrQuery.ORDER.desc);

	    //设置分页参数
	    query.setStart(0);
	    query.setRows(10);

	    //设置高亮显示以及结果的样式
	    query.setHighlight(true);
	    query.addHighlightField("name");  
	    query.setHighlightSimplePre("<font color='red'>");  
	    query.setHighlightSimplePost("</font>"); 

	    //执行查询
	    QueryResponse response = server.query(query);

	    //获取返回结果
	    SolrDocumentList resultList = response.getResults();

	    for(SolrDocument document: resultList){
	        System.out.println("id:"+document.get("id")+"   document:"+document.get("name")+"    description:"+document.get("description"));
	    }

	    //获取实体对象形式
	    List<Person> persons = response.getBeans(Person.class);

	    System.out.println(persons.get(0).getName());

	}

	public static void main(String[] args) throws Exception {
	    SolrUtil solr = new SolrUtil();
	    solr.addDoc();
//	    solr.querySolr();
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值