Solr-使用 SolrJ 向 Solr 增加14万条索引记录

  1. 如何创建索引
    solr 提供了一种方式向其中增加索引的界面,但是呢。。。不太方便,也和实际工作环境不相符合。
    实际工作环境一般都是从数据库里读取数据,然后加入到索引的。很少会通过界面添加索引,因为这样维护更新删除也不方便,尤其是数据量比较大的时候。
    那么本教材就会讲解,如何通过程序把数据加入到Solr 索引里。
  2.  SolrJ
    Solr 支持通过各种各样的语言(如php,javascript, c#, )把数据加入到索引里,因为本教程主要是基于Java的,所以会使用一个第三方工具SolrJ,使用 Java 语言来把数据加入到索引里。

  3.  14万条数据
    为了模仿真实环境,花了很多精力,四处搜刮来了14万条天猫的产品数据,接下来我们就会把这14万条记录加入到 Solr,然后观察搜索效果。
  4.  关于数据库
    本来应该先把这14万条记录保存进数据库,然后再从数据库中取出来的,不过考虑到不是每个同学都有JDBC基础,以及放进数据库的繁琐,和14万条数据从数据库里读取出来的时间消耗,就改成直接从文件里读取出来,然后转换为泛型是Product的集合的形式,相当于从数据库里读取出来了,不过会快很多。
  5.  140k_products.txt
    首先下载 140k_products.rar,并解压为140k_products.txt, 然后放在项目目录下。 这个文件里一共有14万条产品记录。
  6.  Product.java
    准备实体类来存放产品信息
    注: 每个字段上都有@Field 注解,用来告诉Solr 这些和 how2java core里的字段对应
    public class Product {
         
        @Field
        int id;
        @Field
        String name;
        @Field
        String category;
        @Field
        float price;
        @Field
        String place;
        @Field
        String code;
         
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getCategory() {
            return category;
        }
        public void setCategory(String category) {
            this.category = category;
        }
        public float getPrice() {
            return price;
        }
        public void setPrice(float price) {
            this.price = price;
        }
        public String getPlace() {
            return place;
        }
        public void setPlace(String place) {
            this.place = place;
        }
     
        public String getCode() {
            return code;
        }
        public void setCode(String code) {
            this.code = code;
        }
        @Override
        public String toString() {
            return "Product [id=" + id + ", name=" + name + ", category=" + category + ", price=" + price + ", place="
                    + place + ", code=" + code + "]";
        }
     
    }
  7. ProductUtil
    工具类,把 140k_products.txt 文本文件,转换为泛型是Product的集合
    public class ProductUtil {
         
        public static void main(String[] args) throws IOException, InterruptedException, AWTException {
     
            String fileName = "140k_products.txt";
             
            List<Product> products = file2list(fileName);
             
            System.out.println(products.size());
                 
        }
     
        public static List<Product> file2list(String fileName) throws IOException {
            File f = new File(fileName);
            List<String> lines = FileUtils.readLines(f,"UTF-8");
            List<Product> products = new ArrayList<>();
            for (String line : lines) {
                Product p = line2product(line);
                products.add(p);
            }
            return products;
        }
         
        private static Product line2product(String line) {
            Product p = new Product();
            String[] fields = line.split(",");
            p.setId(Integer.parseInt(fields[0]));
            p.setName(fields[1]);
            p.setCategory(fields[2]);
            p.setPrice(Float.parseFloat(fields[3]));
            p.setPlace(fields[4]);
            p.setCode(fields[5]);
            return p;
        }
     
    }
  8.  SolrUtil
    工具类,用来把产品集合批量增加到Solr. 这里就用到了SolrJ第三方包里的api了。
    public class SolrUtil {
        public static SolrClient client;
        private static String url;
        static {
            url = "http://localhost:8983/solr/how2java";
            client = new HttpSolrClient.Builder(url).build();
        }
     
        public static <T> boolean batchSaveOrUpdate(List<T> entities) throws SolrServerException, IOException {
     
            DocumentObjectBinder binder = new DocumentObjectBinder();
            int total = entities.size();
            int count=0;
            for (T t : entities) {
                SolrInputDocument doc = binder.toSolrInputDocument(t);
                client.add(doc);
                System.out.printf("添加数据到索引中,总共要添加 %d 条记录,当前添加第%d条 %n",total,++count);
            }
            client.commit();
            return true;
        }
     
    }
  9.  TestSolr4j
    得到14万个产品对象,然后通过SolrUtil 工具类提交到Solr 服务器
    public class TestSolr4j {
        public static void main(String[] args) throws SolrServerException, IOException {
            List<Product> products = ProductUtil.file2list("140k_products.txt");
            SolrUtil.batchSaveOrUpdate(products);
        }
    }
  10. 验证提交效果
    打开
    http://127.0.0.1:8983/solr/#/how2java
    左边点击 Query -> 点击 Execute Query 查询之后,可以看到右侧显示查询结果,总数是 147939 条





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值