java之学习记录 9 - 2 - lecene 全文检索

搭建springBoot项目

依赖:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lagou</groupId>
    <artifactId>lucene</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>lucene</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
        <!--pojo持久化使用-->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--引入Lucene核心包及分词器包-->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>4.10.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-analyzers-common</artifactId>
            <version>4.10.3</version>
        </dependency>
        <!--IK中文分词器-->
        <dependency>
            <groupId>com.janeluo</groupId>
            <artifactId>ikanalyzer</artifactId>
            <version>2012_u6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--编译插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <!--打包插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
创建引导类  
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<JobInfo> {
}
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<JobInfo> selectAll();
}
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<JobInfo> selectAll() {
        QueryWrapper<JobInfo> wrapper = new QueryWrapper<>();

        List<JobInfo> jobInfoList = jobInfoMapper.selectList(wrapper);
        return jobInfoList;
    }
}
整体结构:   java之学习记录 9 - 2 - lecene 全文检索

在test下创建一个包测试

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<JobInfo> jobInfoList = jobInfoService.selectAll();
        // 遍历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();
    }
}

​​​​​​​

上一篇:银行转账案例-Spring纯XML模式代码示例


下一篇:机器学习-支持向量机