java 测试 solr更新和查询

@Test
    public void addDocument() throws Exception{
        //1.创建链接
        SolrServer solr = new HttpSolrServer("http://localhost:8080/solr");

        //2.创建一文档对象
        SolrInputDocument document = new SolrInputDocument();

        //3.向文档对象中添加域 (先定义后使用)
        document.addField("id", "001");
        document.addField("title", "这是新的域");

        //4.提交文档到索引库
        solr.add(document);

        //5.提交
        solr.commit();  
    }

    @Test
    public void deleteDocument() throws Exception{
        //1.创建链接
        SolrServer solr = new HttpSolrServer("http://localhost:8080/solr");

        //2.根据id删除
        solr.deleteById("001");

        //根据查询删除
        //solr.deleteByQuery("*:*");

        //.提交
        solr.commit();  
    }
/**
 * 使用@Field注解的属性要和Solr配置的Field对应。
    用于更新、插入 到索引库中的bean类
 * @author w7
 *
 */
public class Books {

    @Field
    private String id;
    @Field
    private String bname;
    @Field
    private String bauthor;
    @Field
    private String bprice;
    @Field
    private String bcurrprice;
    @Field
    private String bdiscount;
    @Field
    private String bpress;
    @Field
    private String bpublishtime;
    @Field
    private String bedition;
    @Field
    private String bpagenum;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getBname() {
        return bname;
    }
    public void setBname(String bname) {
        this.bname = bname;
    }
    public String getBauthor() {
        return bauthor;
    }
    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }
    public String getBprice() {
        return bprice;
    }
    public void setBprice(String bprice) {
        this.bprice = bprice;
    }
    public String getBcurrprice() {
        return bcurrprice;
    }
    public void setBcurrprice(String bcurrprice) {
        this.bcurrprice = bcurrprice;
    }
    public String getBdiscount() {
        return bdiscount;
    }
    public void setBdiscount(String bdiscount) {
        this.bdiscount = bdiscount;
    }
    public String getBpress() {
        return bpress;
    }
    public void setBpress(String bpress) {
        this.bpress = bpress;
    }
    public String getBpublishtime() {
        return bpublishtime;
    }
    public void setBpublishtime(String bpublishtime) {
        this.bpublishtime = bpublishtime;
    }
    public String getBedition() {
        return bedition;
    }
    public void setBedition(String bedition) {
        this.bedition = bedition;
    }
    public String getBpagenum() {
        return bpagenum;
    }
    public void setBpagenum(String bpagenum) {
        this.bpagenum = bpagenum;
    }
    @Override
    public String toString() {
        return "Books [id=" + id + ", bname=" + bname + ", bauthor=" + bauthor + ", bprice=" + bprice + ", bcurrprice="
                + bcurrprice + ", bdiscount=" + bdiscount + ", bpress=" + bpress + ", bpublishtime=" + bpublishtime
                + ", bedition=" + bedition + ", bpagenum=" + bpagenum + "]";
    }


}
/**
 * solr测试
 * 
 * @author w7
 *
 */
public class SolrTest {
    private static SolrServer server;

    private static final String DEFAULT_URL = "http://localhost:8080/solr";

    public static void init() {
        server = new HttpSolrServer(DEFAULT_URL);
    }

    // 添加 book 到索引库
    public static void indexBooks(Books book) {
        try {
            UpdateResponse response = server.addBean(book);

            server.commit();
            System.err.println(response.getStatus());// 响应状态

        } catch (IOException | SolrServerException e) {
            e.printStackTrace();
        }
    }

    // 查询
    public static void testQueryAll() {
        SolrQuery params = new SolrQuery();

        // 查询关键词,*:*代表所有属性、所有值,即所有index
        params.set("q", "*:*");

        // 分页,start=0就是从0开始,rows=5当前返回5条记录,第二页就是变化start这个值为5就可以了。
        params.set("start", 0);
        params.set("rows", "5");

        // 排序,如果按照id排序,那么 写为: id desc(or asc)
        params.set("sort", "id asc");

        QueryResponse response = null;
        try {

            response = server.query(params);
        } catch (SolrServerException e) {
            e.printStackTrace();
        }

        if (response != null) {
            System.out.println("Search Results: ");
            SolrDocumentList list = response.getResults();
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).get("bpress"));//根据key取值
            }
        }

    }

//测试:
    public static void main(String[] args) {
        init();
        SolrTest.testQueryAll();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值