lucene封装的web应用
目录
一.提供的访问接口
创建内部一个叫做index01的索引,添加easydb中商品数据的document到索引文件;
访问一个叫做index01的索引,利用field值和value值传递的参数,做一个搜索商品数据返回的功能;TermQuery
二.创建一个springboot的工程
1.springboot标准工程
1.相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mysql -->
<!-- mybatis -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency> <!-- 查询相关jar包 -->
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>6.0.0</version>
</dependency>
<dependency> <!-- lucene自带只能中文分词器jar包 -->
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-smartcn</artifactId>
<version>6.0.0</version>
</dependency>
<dependency> <!-- 测试用到的lucene工具包 -->
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>6.0.0</version>
</dependency>
<dependency> <!-- 测试用到的lucene核心包 -->
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
2.application.properties
server.port=8093
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///easydb?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
mybatis.configuration.mapUnderscoreToCamelCase=true
mybatis.mapperLocations=classpath:mapper/*.xml
mybatis.typeAliasesPackage=com.jt.pojo
3.启动类StarterLucene
package com.jt;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.jt.mapper")
public class StarterLucene {
public static void main(String[] args) {
SpringApplication.run(StarterLucene.class, args);
}
}
4.引入lucene依赖
查看上次博客写的关于Lucene博客,手动配置
三.编写具体功能
3.1.1创建 IndexController类
@RestController
public class IndexController {
@Autowired
private IndexService indexService;
//根据参数创建本地文件夹索引文件位置输入数据库数据
@RequestMapping("{indexName}/add")
public String createIndex(@PathVariable String indexName) throws Exception{
indexService.addIndex(indexName);
return "success";
}
3.1.2IndexService类
@Service
public class IndexService {
@Autowired
private IndexMapper indexMapper;
public void addIndex(String indexName) throws Exception {
//指向这个文件 定义当前工程根目录
FSDirectory dir =
FSDirectory.open(Paths.get(indexName)); //相对路径Paths.get(indexName)
//准备一个输入流对象
Analyzer a=new SmartChineseAnalyzer(); //分词器
IndexWriterConfig config=new IndexWriterConfig(a);
//追加或者创建,有就追加没有就创建
config.setOpenMode(OpenMode.CREATE);
IndexWriter writer=new IndexWriter(dir,config);
//准备document数据写入到索引文件中
//调用持久层将t_product表格数据封装成document写入索引
//查询数据库数据 select * from t_product
List<Product> pList=indexMapper.queryProducts(); //去数据库查询
for (Product product : pList) {
//name,id,image,price 数据库里面的数据 大概60来条
Document doc=new Document();
doc.add(new TextField("name",product.getProductName(),Store.YES));
doc.add(new StringField("id", product.getProductId(), Store.YES));
doc.add(new StringField("image",product.getProductImgurl(),Store.YES));
doc.add(new StringField("price",product.getProductPrice()+"",Store.YES));
doc.add(new DoublePoint("price",product.getProductPrice()));//搜到,还要拿到
//添加到索引中
writer.addDocument(doc);
}
writer.commit();
}
3.1.3IndexMapper接口
package com.jt.mapper;
import java.util.List;
import com.jt.pojo.Product;
public interface IndexMapper {
List<Product> queryProducts();
}
3.1.4IndexMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jt.mapper.IndexMapper">
<select id="queryProducts" resultType="Product">
select * from t_product;
</select>
</mapper>
3.1.5测试结果:
3.2.1 现实查询条件功能
利用termquery查询返回查询的结果集 List<product>
3.2.2IndexController类
@RequestMapping("{indexName}/search")
public List<Product> search(@PathVariable String indexName,String field,String value){
List<Product> pList=indexService.search(indexName,field,value);
return pList;
}
3.2.3IndexService类
public List<Product> search(String indexName,String field, String value) {
try{
Path path = Paths.get(indexName);
FSDirectory dir = FSDirectory.open(path);
//2 构造一个搜索对象,包装一个reader输入流
IndexReader reader=DirectoryReader.open(dir);
IndexSearcher search=new IndexSearcher(reader);
//3 构造词项查询条件TermQuery
//查询的对应域名称,和使用的词语,封装词项对象
Term term=new Term(field,value);//content name
Query query=new TermQuery(term);
//4查询获取结果集,从结果集中获取document的id
TopDocs topDocs = search.search(query, 10);//results 从结果中查到最前的几条
//遍历获取id,通过id获取整整的数据对象document,观察每个document
//对不不同查询条件的评分是不一样的;
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
//获取的document封装productList
List<Product> pList=new ArrayList<Product>();
for (ScoreDoc scoreDoc : scoreDocs) {
Product p=new Product();
int id = scoreDoc.doc;
Document doc=search.doc(id);
p.setProductId(doc.get("id"));
p.setProductName(doc.get("name"));
p.setProductImgurl(doc.get("image"));
p.setProductPrice(Double.parseDouble(doc.get("price")+""));
pList.add(p);
}
return pList;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
3.2.4测试结果
ps:可以使用多域查询
总结:
只是为了抛砖引玉Elasticsearch,通过自己定义的Web应用方式实现提供对外的两个接口简单功能,了解整体流程即可