zoukankan      html  css  js  c++  java
  • SolrJ的入门

    什么是SolrJ?

      solrj是访问solr服务的java客户端,提供索引和搜索的请求方法,

    SolrJ和图形界面操作的区别就类似于数据库中使用jdbc和mysql客户端的区别一样.

    我在测试Solrj时候,使用的是java工程

    然后添加的jar包分别为:SolrJ  solr-4.10.3dist目录下的:

    solrj依赖包,solr-4.10.3distsolrj-lib

     

    Solr服务的依赖包,solrexamplelibext

    添加&修改索引

    1.创建HttpSolrServer对象,通过它和Solr服务器建立连接

    2.创建SolrInputDocument对象,然后通过它来添加域

    3.通过HttpSolrServer对象将SolrInputDocument添加到索引库

    4.提交.

    下面是实现的代码:

    package com.qingmu;
    
    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;
    
    import java.io.IOException;
    
    /**
     * @Auther:qingmu
     * @Description:脚踏实地,只为出人头地
     * @Date:Created in 16:01 2019/4/19
     */
    public class SolrDemo {
    
        //    新增和更新
        @Test
        public void solrJDemoAddTest() throws IOException, SolrServerException {
            //        设置solr服务接口,浏览客户端地址http://192.168.200.128/solr
            String baseURL = "http://192.168.200.128:8080/solr";
    //        创建Httpserver对象,通过它和Solr服务器创建连接
            SolrServer httpSolrServer = new HttpSolrServer(baseURL);
    //        在创建SolrInputDocument对象,然后通过它来添加域
            SolrInputDocument document = new SolrInputDocument();
            document.addField("id", "1");
            document.addField("content", "qingmu");
    //        把SolrInputDocument对象,添加到索引库中
            httpSolrServer.add(document);
    //      提交
            httpSolrServer.commit();
        }
    
        //    删除
        @Test
        public void deleteSolr() throws IOException, SolrServerException {
    //        设置solr服务接口,浏览客户端地址为http://192.168.200.128:8080/solr
            String baseURL ="http://192.168.200.128:8080/solr";
    //        创建HttpSolrServer对象,通过它和Solr对象连接
            HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL);
    //        连接上以后,创建SolrInputDocument对象,然后通过它来添加到域对象
    //        httpSolrServer.deleteById("1");
    //        全部删除
            httpSolrServer.deleteByQuery("*:*");
            httpSolrServer.commit();
        }
    
        //测试查询
        @Test
        public void testQuery() throws SolrServerException {
            String baseURL = "http://192.168.200.128:8080/solr";
            HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL);
    //        创建查询对象
            SolrQuery solrQuery = new SolrQuery();
    //        设置条件
            solrQuery.setQuery("*:*");
    //        发起搜索请求
            QueryResponse query = httpSolrServer.query(solrQuery);
    //        处理搜索结果
            SolrDocumentList response = query.getResults();
    //        打印总条数
            System.out.println("查询的总条数为:"+response.getNumFound());
            for (SolrDocument entries : response) {
                System.out.println("id = "+ entries.get("id"));
                System.out.println("content = "+ entries.get("product_name"));
            }
    
        }
    
    
    }
  • 相关阅读:
    pip本地源搭建
    linux 创建 bootable iso 文件
    yum 源本地化 (two)
    linux 网络配置
    linux 设置root可以远程登陆
    察看linux 发行版
    mysql bin-log 设置
    samba 奇怪问题
    delphi中的临界区
    ligerGrid 取得选中行的数据
  • 原文地址:https://www.cnblogs.com/qingmuchuanqi48/p/10739040.html
Copyright © 2011-2022 走看看