热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

三、SpringBoot整合Solr

依赖org.springframework.bootspring-

依赖

org.springframework.bootspring-boot-starter-data-solrorg.springframework.bootspring-boot-starter-thymeleaforg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-devtoolsruntimetrueorg.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-testtestorg.junit.vintagejunit-vintage-engine

属性文件

# 应用名称
spring.application.name=mysolr
# 应用服务 WEB 访问端口
server.port=8080
#solr服务器
spring.data.solr.host=http://169.254.140.100:8983/solr/solrhome

业务类

package com.hr.mysolr.service.impl;import com.hr.mysolr.entity.Product;
import com.hr.mysolr.service.ProductService;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;/*** @Classname ProductServiceImpl* @Description TODO* @Date 2022-02-18 8:37* @Created by 汤永红*/
@Service
public class ProductServiceImpl implements ProductService {//1.创建Solr客户端, 为了查询query&#64;Resourceprivate SolrClient solrClient;&#64;Overridepublic List seachSolr(String field, String keywords, int start, int end) throws Exception {//2.创建查询条件SolrQuery query &#61; new SolrQuery();query.setQuery(field &#43; ":" &#43; keywords);// q 查询条件query.setHighlight(true);//打开高亮query.setParam("hl.fl", field);//必须写&#xff01;&#xff01;&#xff01;query.addFacetField(field);query.setHighlightSimplePre("");//设置前缀query.setHighlightSimplePost("");//设置后缀query.setQuery(field &#43; ":" &#43; keywords);// q 查询条件//3.执行QueryResponse queryResponse &#61; solrClient.query(query);//将下面带高亮的内容替换掉上面的//4.得到结果集SolrDocumentList results &#61; queryResponse.getResults();//5.得到高亮Map>> highlighting &#61; queryResponse.getHighlighting();//6.如果有结果,并且有数据&#xff0c;就装List products &#61; new ArrayList<>();if (results !&#61; null && results.getNumFound() > 0) {for (SolrDocument doc : results) {//把doc中的内容拿出来放到Product中----可用工具类中的方法代替-----Product product &#61; new Product();product.setPid(Integer.parseInt(doc.get("id") &#43; ""));product.setName(doc.get("product_name") &#43; "");product.setCatalog(Integer.parseInt(doc.get("product_catalog") &#43; ""));product.setCatalog_name(doc.get("product_catalog_name") &#43; "");product.setPrice(Double.parseDouble(doc.get("product_price") &#43; ""));product.setDescription(doc.get("product_description") &#43; "");product.setPicture(doc.get("product_picture") &#43; "");//---------------------------------------------------------------//拿高亮的内容String key &#61; doc.getFieldValue("id") &#43; "";Map> map &#61; highlighting.get(key);List value &#61; map.get(field);String highlightingField &#61; "";if (value !&#61; null && value.size() > 0) {highlightingField &#61; value.get(0);}product.setName(highlightingField);products.add(product);product &#61; null;}}return products;}
}

package com.hr.mysolr.web;import com.hr.mysolr.entity.Product;
import com.hr.mysolr.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;
import java.util.List;/*** &#64;Classname ProductController* &#64;Description TODO* &#64;Date 2022-02-18 9:38* &#64;Created by 汤永红*/
&#64;Controller
&#64;RequestMapping("/")
public class ProductController {&#64;Resourceprivate ProductService productService;&#64;RequestMapping("/query")&#64;ResponseBodypublic List query(&#64;RequestParam("name") String name, &#64;RequestParam("keyWord") String keyWord) {List products &#61; null;try {products &#61; productService.seachSolr(name, keyWord, 0, 10);} catch (Exception e) {e.printStackTrace();}return products;}

先测试Web

再用Vue






流水号product_name操作
{{index}}
编号|删除


效果

工具类&#xff08;实体和数据库列一致&#xff09;

package com.example.demo.util;import org.apache.solr.common.SolrDocument;import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;class MyUtil {/*** SolrDocument与实体类转换** &#64;param document SolrDocument对象* &#64;param clzz 泛型类* &#64;return */public static T solrDocumentToPojo(SolrDocument document, Class clzz) {if (null !&#61; document) {try {Object obj &#61; clzz.newInstance();Method m &#61; null;Class fieldType &#61; null;for (String fieldName : document.getFieldNames()) {//需要说明的是返回的结果集中的FieldNames()比类属性多Field[] filedArrays &#61; clzz.getDeclaredFields(); //获取类中所有属性for (Field f : filedArrays) {//如果实体属性名和查询返回集中的字段名一致,填充对应的set方法/*if(fieldName.equals("_id")){fieldName&#61;"id";}*/if (f.getName().equals(fieldName)) {//获取到的属性名f &#61; clzz.getDeclaredField(fieldName);//属性类型fieldType &#61; f.getType();//构造set方法名 setIdString dynamicSetMethod &#61; dynamicMethodName(f.getName(), "set");//获取方法m &#61; clzz.getMethod(dynamicSetMethod, fieldType);//获取到的值// 如果是 int, float等基本类型&#xff0c;则需要转型if (fieldType.equals(Integer.TYPE)) {fieldType &#61; Integer.class;} else if (fieldType.equals(Float.TYPE)) {fieldType &#61; Float.class;} else if (fieldType.equals(Double.TYPE)) {fieldType &#61; Double.class;} else if (fieldType.equals(Boolean.TYPE)) {fieldType &#61; Boolean.class;} else if (fieldType.equals(Short.TYPE)) {fieldType &#61; Short.class;} else if (fieldType.equals(Long.TYPE)) {fieldType &#61; Long.class;} else if (fieldType.equals(String.class)) {fieldType &#61; String.class;} else if (fieldType.equals(Collection.class)) {fieldType &#61; Collection.class;}m.invoke(obj, fieldType.cast(document.getFieldValue(fieldName)));}}}return clzz.cast(obj);} catch (ClassCastException e) {// 请检查schema.xml中的各个field的数据类型与PO类的是否一致e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();} catch (NoSuchMethodException e) {//请检查PO类中的field对应的各个setter和getter是否存在e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();} catch (NoSuchFieldException e) {//请检查schema中的field是否不存在于PO类中e.printStackTrace();}}return null;}//动态构造getXxx setXxxpublic static String dynamicMethodName(String name, String setOrGet) {String setMethodName &#61; setOrGet&#43; name.substring(0, 1).toUpperCase()&#43; name.substring(1);return setMethodName;}
}


推荐阅读
  • 这个问题困扰了我两天,卸载Dr.COM客户端(我们学校上网要装这个客户端登陆服务器,以后只能在网页里输入用户名和密码了),问题解决了。问题的现象:在实验室机台式机上安装openfire和sp ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 首先我们在taotao-search-interface工程中新建一个SearchService接口,并在接口中添加一个方法,如下图所示。接着,我们到taotao-search-s ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • 本文介绍了在Linux下安装和配置Kafka的方法,包括安装JDK、下载和解压Kafka、配置Kafka的参数,以及配置Kafka的日志目录、服务器IP和日志存放路径等。同时还提供了单机配置部署的方法和zookeeper地址和端口的配置。通过实操成功的案例,帮助读者快速完成Kafka的安装和配置。 ... [详细]
  • Tomcat安装与配置教程及常见问题解决方法
    本文介绍了Tomcat的安装与配置教程,包括jdk版本的选择、域名解析、war文件的部署和访问、常见问题的解决方法等。其中涉及到的问题包括403问题、数据库连接问题、1130错误、2003错误、Java Runtime版本不兼容问题以及502错误等。最后还提到了项目的前后端连接代码的配置。通过本文的指导,读者可以顺利完成Tomcat的安装与配置,并解决常见的问题。 ... [详细]
  • Annotation的大材小用
    为什么80%的码农都做不了架构师?最近在开发一些通用的excel数据导入的功能,由于涉及到导入的模块很多,所以开发了一个比较通用的e ... [详细]
  • 基于分布式锁的防止重复请求解决方案
    一、前言关于重复请求,指的是我们服务端接收到很短的时间内的多个相同内容的重复请求。而这样的重复请求如果是幂等的(每次请求的结果都相同,如查 ... [详细]
  • 部署solr建立nutch索引
    2019独角兽企业重金招聘Python工程师标准接着上篇nutch1.4的部署应用,我们来部署一下solr,solr是对lucene进行了封装的企 ... [详细]
  • camel_使用Camel在来自不同来源的Solr中索引数据
    camelApacheSolr是建立在Lucene之上的“流行的,快速的开源企业搜索平台”。为了进行搜索(并查找结果),通常需要从不同的源(例如内容管理 ... [详细]
  • spring cloud eureka微服务之间如何调用
    小编给大家分享一下springcloudeureka微服务之间如何调用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇 ... [详细]
  • 一:什么是solrSolr是apache下的一个开源项目,使用Java基于lucene开发的全文搜索服务器;Lucene是一个开放源代 ... [详细]
author-avatar
mobiledu2502859073
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有