搭建springBoot项目
依赖:
创建引导类
?
package com.lagou.lucene;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.lagou.lucene.mapper")
public class LuceneApplication {
public static void main(String[] args) {
SpringApplication.run(LuceneApplication.class, args);
}
}
配置properties文件
?
server:
port: 9000
Spring:
application:
name: lagou-lucene
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/es?useUnicode=true&characterEncoding=utf8&serverTimezOne=UTC
username: root
password: 123456
mybatis:
configuration:
map-underscore-to-camel-case: true
创建实体类、mapper、service
?
实体类:
package com.lagou.lucene.pojo;
import lombok.Data;
import javax.persistence.Id;
import javax.persistence.Table;
@Data
@Table(name = "job_info")
public class JobInfo {
@Id
private long id;
private String companyName;
private String companyAddr;
private String companyInfo;
private String jobName;
private String jobAddr;
private String jobInfo;
private int salaryMin;
private int salaryMax;
private String url;
private String time;
}
mapper
package com.lagou.lucene.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lagou.lucene.pojo.JobInfo;
public interface JobInfoMapper extends BaseMapper
}
service及serviceImpl
package com.lagou.lucene.service;
import com.lagou.lucene.pojo.JobInfo;
import java.util.List;
public interface JobInfoService {
/**
* 根据id查询
* @param id
* @return
*/
public JobInfo selectById(long id);
/**
* 查询所有
* @return
*/
public List
}
package com.lagou.lucene.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lagou.lucene.mapper.JobInfoMapper;
import com.lagou.lucene.pojo.JobInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class JobInfoServiceImpl implements JobInfoService {
@Autowired
private JobInfoMapper jobInfoMapper;
@Override
public JobInfo selectById(long id) {
return jobInfoMapper.selectById(id);
}
@Override
public List
QueryWrapper
List
return jobInfoList;
}
}
整体结构:
?
package com.lagou.lucene;
import com.lagou.lucene.pojo.JobInfo;
import com.lagou.lucene.service.JobInfoService;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.wltea.analyzer.lucene.IKAnalyzer;
import java.io.File;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class LuceneApplicationTests {
@Autowired
private JobInfoService jobInfoService;
/**
* 创建索引
*/
@Test
public void test() throws Exception {
// 指定索引文件的存储位置,索引具体的表现形式就是一组有规则的文件
Directory directory = FSDirectory.open(new File("E:/class/index"));
// 配置版本及其分词器 StandardAnalyzer()
Analyzer analyzer = new IKAnalyzer();
IndexWriterConfig cOnfig= new IndexWriterConfig(Version.LATEST,analyzer);
// 创建indexWriter对象,作用就是创建索引
IndexWriter indexWriter = new IndexWriter(directory,config);
// 先删除已经存在的索引库
indexWriter.deleteAll();
// 获得索引源/原始数据
List
// 遍历JobInfoList,每次遍历创建一个Document对象
for (JobInfo jobInfo : jobInfoList) {
// 创建Document对象
Document document = new Document();
// 创建Field对象,添加到document中
document.add(new LongField("id",jobInfo.getId(), Field.Store.YES));
// 切分词,索引,存储
document.add(new TextField("companyName",jobInfo.getCompanyName(), Field.Store.YES));
document.add(new TextField("companyAddr",jobInfo.getCompanyAddr(), Field.Store.YES));
document.add(new TextField("companyInfo",jobInfo.getCompanyInfo(), Field.Store.YES));
document.add(new TextField("jobName",jobInfo.getJobName(), Field.Store.YES));
document.add(new TextField("jobAddr",jobInfo.getJobAddr(), Field.Store.YES));
document.add(new TextField("jobInfo",jobInfo.getJobInfo(), Field.Store.YES));
document.add(new IntField("salaryMin",jobInfo.getSalaryMin(),Field.Store.YES));
document.add(new IntField("salaryMax",jobInfo.getSalaryMax(),Field.Store.YES));
document.add(new StringField("url",jobInfo.getUrl(),Field.Store.YES));
// 将文档追加到索引库
indexWriter.addDocument(document);
}
// 关闭资源
indexWriter.close();
System.out.println("create index success");
}
/**
* 查询索引
*/
@Test
public void query() throws Exception{
// 指定索引文件的存储位置,索引具体的表现形式就是一组有规则的文件
Directory directory = FSDirectory.open(new File("E:/class/index"));
// IndexReader对象
IndexReader indexReader = DirectoryReader.open(directory);
// 创建查询对象,IndexSearcher
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
// 使用term,查询公司名称中包含“北京”的所有文档对象
Query query = new TermQuery(new Term("companyName","北京"));
TopDocs search = indexSearcher.search(query, 100);
// 获得符合查询条件的文档数
int totalHits = search.totalHits;
System.out.println("符合条件的文档数:"+totalHits);
// 获得命中的文档
ScoreDoc[] scoreDocs = search.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
// 文档id
int id = scoreDoc.doc;
// 通过文档id获得文档对象
Document doc = indexSearcher.doc(id);
System.out.println("id:"+doc.get("id"));
System.out.println("companyName:"+doc.get("companyName"));
System.out.println("companyAddr:"+doc.get("companyAddr"));
System.out.println("companyInfo:"+doc.get("companyInfo"));
System.out.println("jobName:"+doc.get("jobName"));
System.out.println("jobAddr:"+doc.get("jobAddr"));
System.out.println("jobInfo:"+doc.get("jobInfo"));
System.out.println("salaryMin:"+doc.get("salaryMin"));
System.out.println("salaryMax:"+doc.get("salaryMax"));
System.out.println("----------------------------------------------");
}
// 释放资源
indexReader.close();
}
}
???????