• SPRINGMVC整合SOLR


    1. <dependency>
    2.  
      <groupId>org.apache.solr</groupId>
    3.  
      <artifactId>solr-solrj</artifactId>
    4.  
      <version>7.0.1</version>
    5.  
      </dependency>

    2、配置一个spring-solr.xml

    1.  
      <?xml version="1.0" encoding="UTF-8"?>
    2.  
      <beans xmlns="http://www.springframework.org/schema/beans"
    3.  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:http="http://www.springframework.org/schema/c"
    4.  
      xsi:schemaLocation="http://www.springframework.org/schema/beans
    5.  
      http://www.springframework.org/schema/beans/spring-beans.xsd">
    6.  
       
    7.  
      <bean id="httpSolrClient"
    8.  
      class="org.apache.solr.client.solrj.impl.HttpSolrClient">
    9.  
      <constructor-arg index="0" value="http://192.168.52.13:8983/solr/comInfo_core"/>
    10.  
      <!-- 建立连接的最长时间 -->
    11.  
      <!--<property name="connectionTimeout"
    12.  
      value="${solr.connectionTimeout}"/>-->
    13.  
      <property name="connectionTimeout" value="3000"/>
    14.  
      </bean>
    15.  
       
    16.  
      </beans>

    3、在这里我是将solr交给spring管理的,只写了service层跟serviceImpl

    1.  
      @Service
    2.  
      public class SolrImpl implements IsolrService {
    3.  
      @Autowired
    4.  
      HttpSolrClient client;
    5.  
      @Override
    6.  
      public List<CommodityInfoDto> solr(String text) throws IOException, SolrServerException {
    7.  
       
    8.  
      SolrQuery query=new SolrQuery();
    9.  
       
    10.  
      String queryText="infoName:"+text+"* or infoArtist:"+text+" or infoAddress:"+text+"*";
    11.  
      query.set("q",queryText);
    12.  
      //1、过滤器
    13.  
      //query.set("fq","infoPrice:[1 TO 1000]");
    14.  
      //2、排序
    15.  
      //query.set("sort","infoPrice desc","id asc");
    16.  
      //3、设置查询到的文档返回的域对象
    17.  
      query.set("fi","id,infoName,infoArtist,infoAddress");
    18.  
       
    19.  
      //4、设置默认查询的域
    20.  
      query.set("df","infoName","infoArtist","infoAddress");
    21.  
       
    22.  
      //5.分页
    23.  
      query.set("start",0);
    24.  
      query.set("rows",10);
    25.  
       
    26.  
      //6、高亮
    27.  
      //设置高亮域(设置的域必须在查询条件中存在)
    28.  
      query.addHighlightField("infoName");
    29.  
      query.addHighlightField("infoArtist");
    30.  
      query.addHighlightField("infoAddress");
    31.  
      //前缀
    32.  
      query.setHighlightSimplePre("<em style='color:red'>");//这里的话,如果用thymeleaf模板,css样式生效需要加th:utext...
    33.  
      //后缀
    34.  
      query.setHighlightSimplePost("</em>");
    35.  
       
    36.  
      QueryResponse response = client.query(query);
    37.  
      //普通查询
    38.  
      SolrDocumentList results = response.getResults();
    39.  
      List<CommodityInfoDto> comList=new ArrayList<CommodityInfoDto>();
    40.  
      //高亮查询
    41.  
      NamedList<SimpleOrderedMap> namedList= (NamedList<SimpleOrderedMap>) response.getResponse().get("highlighting");
    42.  
      //k是id,内部的map的key是域名,其value是高亮的值集合
    43.  
      /*Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();*/
    44.  
      System.out.println("匹配的结果总数是-------"+results.getNumFound());
    45.  
      for(SolrDocument document:results){
    46.  
      CommodityInfoDto comInfo=new CommodityInfoDto();
    47.  
      Integer id=Integer.parseInt((String)document.get("id"));
    48.  
      comInfo.setInfoId(id);;
    49.  
      comInfo.setInfoName((String)document.get("infoName"));
    50.  
      comInfo.setInfoImg((String)document.get("infoImg"));
    51.  
      comInfo.setInfoArtist((String)document.get("infoArtist"));
    52.  
      comInfo.setInfoTime((String)document.get("infoTime"));
    53.  
      comInfo.setInfoAddress((String)document.get("infoAddress"));
    54.  
      comInfo.setInfoPrice((String)document.get("infoPrice"));
    55.  
      comInfo.setInfoTicket((String)document.get("infoTicket"));
    56.  
      for(int i=0;i<namedList.size();i++){
    57.  
      String nameId = namedList.getName(i);
    58.  
      if(id.toString().equals(nameId)){
    59.  
      SimpleOrderedMap map=namedList.getVal(i);//获取某个商品的高亮
    60.  
      //这里是需要加判断的,不然会有空值加进去
    61.  
      ArrayList infoName = (ArrayList) map.get("infoName");
    62.  
      if(infoName!=null&& infoName.size()>0){
    63.  
      String highInfoName=(String)infoName.get(0);
    64.  
      comInfo.setInfoName(highInfoName);
    65.  
      }
    66.  
       
    67.  
       
    68.  
      ArrayList infoArtist = (ArrayList) map.get("infoArtist");
    69.  
      if(infoArtist!=null&& infoArtist.size()>0){
    70.  
      String highinfoArtist=(String)infoArtist.get(0);
    71.  
      comInfo.setInfoArtist(highinfoArtist);
    72.  
      }
    73.  
       
    74.  
       
    75.  
      ArrayList infoAddress = (ArrayList) map.get("infoAddress");
    76.  
      if(infoAddress!=null&& infoAddress.size()>0){
    77.  
      String highinfoAddress=(String)infoAddress.get(0);
    78.  
      comInfo.setInfoArtist(highinfoAddress);
    79.  
      }
    80.  
      }
    81.  
      }
    82.  
      comList.add(comInfo);
    83.  
      }
    84.  
      return comList;
    85.  
      }
    86.  
      }

    4、我这里是通过用户输入一个字符,里面内容包括商品名字+商品艺人姓名+商品地址,联合查询,查询出来的对象也不会是重复的数据

    1.  
      //查询商品
    2.  
      @Test
    3.  
      public void test11(){
    4.  
      try {
    5.  
      List<CommodityInfoDto> list = solr.solr("演唱");
    6.  
      for(CommodityInfoDto com:list){
    7.  
      System.out.println("-------------->"+com);
    8.  
      }
    9.  
      } catch (IOException e) {
    10.  
      e.printStackTrace();
    11.  
      } catch (SolrServerException e) {
    12.  
      e.printStackTrace();
    13.  
      }
    14.  
      }
    15.  
      }
  • 相关阅读:
    运行期优化
    虚拟机字节码执行引擎
    虚拟机类加载机制
    类文件结构
    垃圾收集机制
    浅析Spring MVC工作机制
    TomCat系统架构
    docker安装mongo初体验
    微服务笔记--概念
    使用maven构建多模块项目_记录
  • 原文地址:https://www.cnblogs.com/wangwenlong8/p/13022002.html
Copyright © 2020-2023  润新知