solr的增删改查加分页的基本使用

solr的增改删查 ,分页 ,高亮

package dao;

import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
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.junit.Test;

import bean.Article;

/**
 * 使用solrJ来调用solr的服务。。。
 * @author root 
 *
 */
public class SolrJ {
    /**
     * 插入和更新 id相同不会生成两个,只会进行修改,
     * @throws Exception
     */
    @Test
    public void addIndex() throws Exception{
        String urlString = "http://192.168.8.20:8080/solr";
        SolrServer solr = new HttpSolrServer(urlString);

        //第一种添加方式
        /*SolrInputDocument document = new SolrInputDocument();
        document.addField("id", "23");
        document.addField("name", "吴淑静");
        document.addField("content", "很有文艺范的一个女孩子");*/

        //第二种添加方式
        Article article = new  Article();
        article.setId(30);
        article.setTitle("高大上");
        article.setContent("很高很富很帅");
        article.setPrice(19);

        solr.addBean(article);
        solr.commit();
    }
    /**
     * 删除
     * @throws Exception
     */
    @Test
    public void Del() throws Exception{
        String solrUrl= "http://192.168.8.20:8080/solr";
        SolrServer solr = new HttpSolrServer(solrUrl);
        solr.deleteById("30");
        solr.commit();
    }

    /**
     * 查找
     * @throws Exception 
     */
    @Test
    public void testFind() throws Exception{
        String solrUrl= "http://192.168.8.20:8080/solr";
        SolrServer solr = new HttpSolrServer(solrUrl);
        SolrQuery solrParams = new SolrQuery();
        solrParams.setQuery("name:吴淑静");
        QueryResponse queryResponse=solr.query(solrParams);
        //得到记过,进行遍历
        SolrDocumentList results = queryResponse.getResults();
        for(SolrDocument document:results){
            Object id = document.get("id");
            Object name = document.get("name");
            Object content = document.get("content");
            System.out.println(id);
            System.out.println(name);
            System.out.println(content);
        }
    }

    /**
     *     分页      
     * @throws Exception 
     */
    @Test
    public void fenYe() throws Exception{
        String solrUrl= "http://192.168.8.20:8080/solr";
        SolrServer solr = new HttpSolrServer(solrUrl);
        SolrQuery solrParams = new SolrQuery();
        solrParams.setQuery("name:吴淑静");
        //分页
        solrParams.setStart(0);
        solrParams.setRows(1);

        QueryResponse queryResponse=solr.query(solrParams);

        //得到记过,进行遍历
        SolrDocumentList results = queryResponse.getResults();
        for(SolrDocument document:results){
            Object id = document.get("id");
            Object name = document.get("name");
            Object content = document.get("content");
            System.out.println(id);
            System.out.println(name);
            System.out.println(content);
        }
    }

    /**
     *    高亮      
     * @throws Exception 
     */
    @Test
    public void hightLight() throws Exception{
        String solrUrl= "http://192.168.8.20:8080/solr";
        SolrServer solr = new HttpSolrServer(solrUrl);
        SolrQuery solrParams = new SolrQuery();
        solrParams.setQuery("name:吴淑静");

        //开启高亮
        solrParams.setHighlight(true);
        //高亮的显示的格式。。。。
        solrParams.setHighlightSimplePre("<font color='red'>");
        solrParams.setHighlightSimplePost("</font>");
        //需要哪些字段显示高亮
        solrParams.setParam("hl.fl", "name");

        QueryResponse queryResponse=solr.query(solrParams);
        SolrDocumentList documentList = queryResponse.getResults();
        Map<String, Map<String, List<String>>> mapList = queryResponse.getHighlighting();

        //遍历
        for(SolrDocument document:documentList){
            Object id = document.get("id");
            Map<String, List<String>> fieldMap = mapList.get(id);
            List<String> list = fieldMap.get("name");
            System.out.println(id);
            System.out.println(list.toString());
        }
    }
}

bean.Article类

package bean;

import org.apache.solr.client.solrj.beans.Field;

public class Article {
    @Field(value="id")
    private int id;
    @Field(value="title")
    private String title;
    @Field(value="content")
    private String content;
    @Field(value="price")
    private int price;



    //自动生成的get 和 set 方法 无需关系
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值