• 【springboot】【solr】springboot+solr集群


    在此基础上:win10下solr7.2.1+tomcat9.0.33+zookeeper3.5.6集群

    或者直接添加依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-solr</artifactId>
    </dependency>

    application.yml

    spring:
      data:
        solr:
          host: http://127.0.0.1:8800/solr,http://127.0.0.1:8801/solr,http://127.0.0.1:8802/solr
          repositories:
            enabled: true
          zk-host: 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183

    测试类

     1 package com.xiaostudy.solr;
     2 
     3 import org.apache.solr.client.solrj.SolrQuery;
     4 import org.apache.solr.client.solrj.SolrRequest;
     5 import org.apache.solr.client.solrj.SolrServerException;
     6 import org.apache.solr.client.solrj.impl.CloudSolrClient;
     7 import org.apache.solr.client.solrj.request.QueryRequest;
     8 import org.apache.solr.client.solrj.response.QueryResponse;
     9 import org.apache.solr.common.SolrDocumentList;
    10 import org.junit.jupiter.api.Test;
    11 import org.springframework.beans.factory.annotation.Autowired;
    12 import org.springframework.boot.test.context.SpringBootTest;
    13 
    14 import java.io.IOException;
    15 
    16 @SpringBootTest
    17 class SolrApplicationTests {
    18 
    19     @Autowired
    20     private CloudSolrClient cloudSolrClient;
    21 
    22     @Test
    23     void contextLoads() {
    24         System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    25         SolrDocumentList query = query("id:1");
    26         System.out.println(query.getNumFound());
    27         System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
    28     }
    29 
    30     public SolrDocumentList query(String value) {
    31         SolrDocumentList st = null;
    32         SolrQuery param = new SolrQuery();
    33         param.setQuery( value );
    34         param.setRows(5);
    35         QueryResponse rsp = null;
    36         try {
    37             QueryRequest req = new QueryRequest( param, SolrRequest.METHOD.POST );
    38 //            req.setBasicAuthCredentials("username", "password");//如果有密码的话
    39             cloudSolrClient.setDefaultCollection("test2");
    40             rsp = req.process(cloudSolrClient);
    41             st = rsp.getResults();
    42         } catch (SolrServerException e) {
    43             e.printStackTrace();
    44         } catch (IOException e) {
    45             e.printStackTrace();
    46         }
    47         return st;
    48     }
    49 
    50 }

     

     

     

     

     

     

    另外:单机版

    application.yml

    spring:
      data:
        solr:
          host: http://127.0.0.1:8080/solr

    测试类

    package com.xiaostudy.solr;
    
    import org.apache.solr.client.solrj.SolrClient;
    import org.apache.solr.client.solrj.SolrQuery;
    import org.apache.solr.client.solrj.SolrRequest;
    import org.apache.solr.client.solrj.SolrServerException;
    import org.apache.solr.client.solrj.response.QueryResponse;
    import org.apache.solr.common.SolrDocumentList;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.io.IOException;
    
    @SpringBootTest
    class SolrApplicationTests {
    
        @Autowired
        private SolrClient solrClient;
    
        @Test
        void contextLoads() {
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            SolrDocumentList query = query("id:2");
            System.out.println(query.getNumFound());
            System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        }
    
        public SolrDocumentList query(String value) {
            SolrDocumentList st = null;
            SolrQuery param = new SolrQuery();
            param.setQuery( value );
            param.setRows(5);
            try {
                QueryResponse archives = solrClient.query("test2", param, SolrRequest.METHOD.POST);
                st = archives.getResults();
            } catch (SolrServerException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return st;
        }
    
    }
  • 相关阅读:
    windbg javascript脚本--将内存内容保存到文件
    js补全前置零
    javascript打印对象(object)
    c++ primer学习指导(23)--4.2算术运算符
    c++ primer学习指导(22)--3.2.1直接初始化和拷贝初始化
    c++ primer学习指导(21)--2.2.4名字的作用域
    c++ primer学习指导(19)--2.1.2类型转换
    c++ primer学习指导(18)-- 2.1.1算术类型
    搭建sock5代理
    Centos 6 yum源
  • 原文地址:https://www.cnblogs.com/xiaostudy/p/12745511.html
Copyright © 2020-2023  润新知