zoukankan      html  css  js  c++  java
  • solr单元测试

    package com.taotao.rest.solr;
    
    import java.io.IOException;
    
    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;
    import org.junit.Test;
    
    public class SolrJTest {
        
        @Test
        public void addDocument() throws SolrServerException, IOException {
            
            //创建一个连接
            HttpSolrServer solrServer = new HttpSolrServer("http://192.168.29.102:8080/solr");
            //创建一个文档对象
            SolrInputDocument document = new SolrInputDocument();
            document.addField("id", "test009");
            document.addField("item_title", "测试商品9");
            document.addField("item_price", 12999);
            //把文档对象写入数据库
            solrServer.add(document);
            //提交
            solrServer.commit();
        }
        
        @Test
        public void deleteDocument() throws Exception {
            //创建一个连接
            HttpSolrServer solrServer = new HttpSolrServer("http://192.168.29.102:8080/solr");
            //删除
    //        solrServer.deleteById("test002");
            solrServer.deleteByQuery("*:*");
            //提交
            solrServer.commit();
        }
        
        @Test
        public void queryDocument() throws Exception {
            //创建连接
            SolrServer solrServer = new HttpSolrServer("http://192.168.29.102:8080/solr");
            //创建一个查询对象
            SolrQuery query = new SolrQuery();
            //设置查询条件
            query.setQuery("*:*");
            //下面这两个即使不设置也会有默认值
            query.setStart(20);
            query.setRows(50);
            //执行查询
            QueryResponse response = solrServer.query(query);
            //取查询结果
            SolrDocumentList solrDocumentList = response.getResults();
            for (SolrDocument document : solrDocumentList) {
                System.err.println(document.get("id"));
                System.err.println(document.get("item_title"));
                System.err.println(document.get("item_price"));
                System.err.println(document.get("item_image"));
            }
        }
        
    }
  • 相关阅读:
    mysql5.6 TIME,DATETIME,TIMESTAMP
    CMake 简单介绍 图
    mysql 源码编绎修改 FLAGS,调试MYSQL
    CHAR 详解
    关于MySQL的各种总结
    Shell编程速查手册
    cmake 手册系列
    编译安装GCC 5.2.0
    宽字符相关的输入输出
    Makefile
  • 原文地址:https://www.cnblogs.com/libin6505/p/9796730.html
Copyright © 2011-2022 走看看