1.在MySQL数据中建立lagou_db数据库, 将position.sql中的数据导入到mysql 数据中。
2.使用Solr的DIH 将mysql中的数据导入到Solr中。
3.使用SpringBoot 访问Solr 使用positionName 字段检索职位信息 如果检索到的职位信息不够5条 则需要
启用positionAdvantage 查找 美女多、员工福利好 的企业职位信息进行补充够5条。
依赖包:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.lagou</groupId><artifactId>solr_springboot</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.0.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-solr</artifactId></dependency></dependencies></project>
配置文件:application.yml
spring:data:solr:host: http://127.0.0.1:8983/solr/newCore
查询对象:Postion.java
package com.lagou.bean;import org.apache.solr.client.solrj.beans.Field;public class Position {@FieldString id;@FieldString companyName;@FieldString positionAdvantage;@FieldString positionName;@Overridepublic String toString() {return "Position{" +"id='" + id + '\'' +", companyName='" + companyName + '\'' +", positionAdvantage='" + positionAdvantage + '\'' +", positionName='" + positionName + '\'' +'}';}
}
service层:PositionService.java
package com.lagou.service;import com.lagou.bean.Position;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;@Service
public class PositionService {@AutowiredSolrClient solrClient;public List<Position> search(String keyword) throws IOException, SolrServerException {SolrQuery solrQuery = new SolrQuery();// 设置默认搜索域solrQuery.set("df","positionName");solrQuery.setQuery(keyword);QueryResponse queryResponse = solrClient.query(solrQuery);List<Position> positionList = queryResponse.getBeans(Position.class);if (positionList.size() < 5){solrQuery.set("df", "positionAdvantage");QueryResponse appendQueryResponse = solrClient.query(solrQuery);List<Position> appendPositionList = appendQueryResponse.getBeans(Position.class);positionList.addAll(appendPositionList);}ArrayList<Position> positions = new ArrayList<>(new HashSet<>(positionList));return positions;}}
启动类:ApplicationTest.java
package com.lagou;import com.lagou.bean.Book;
import com.lagou.bean.Position;
import com.lagou.service.BookService;
import com.lagou.service.PositionService;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;import java.io.IOException;
import java.util.List;@SpringBootApplication
public class ApplicationTest {public static void main(String[] args) throws IOException, SolrServerException {ApplicationContext app = SpringApplication.run(ApplicationTest.class,args);PositionService positionService = app.getBean("positionService", PositionService.class);List<Position> positions = positionService.search("美女多");positions.forEach(e -> System.out.println(e));}
}
控制台打印