作者:mobiledu2502859073 | 来源:互联网 | 2023-07-08 09:03
依赖
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;}
}