您的位置:首页 > 数据库 > Redis

J2EE项目系列(四)--SSM框架构建积分系统和基本商品检索系统(Spring+SpringMVC+MyBatis+Lucene+Redis+MAVEN)(2)建立商品数据库和Lucene的搭建

2017-04-01 22:17 1401 查看

继续搭建我们的项目吧。现在是搭建商品的数据库以及Lucene框架整合。

本系列:

(一)SSM框架构建积分系统和基本商品检索系统(Spring+SpringMVC+MyBatis+Lucene+Redis+MAVEN)(1)框架整合构建

文章结构:(1)商品数据库的构建;(2)Lucene的整合与测试;

一、商品数据库的构建:(数据库文件会在工程文件给出。)

(1)商品层级索引表

商品是涉及多层结构的。比如:家装->住宅家具->床上用品







(2)商品信息表:





以及最后添加的size,商品大小款。

大家拿着这两个图对着我将会给大家导出的数据库文件看就好。清晰明了。

二、Lucene的整合与测试

(1)官网下载先。下载多少个包呢??

IKAnalyzer2012FF_u1.jar         //中文分词
lucene-analyzers-common-4.4.0.jar  //其余是lucene核心包。词语的切割,core内容包,高亮包,查询包
lucene-core-4.4.0.jar
lucene-highlighter-4
lucene-queryparser-4.4.0.jar


(2)导入到我们的项目中(有朋友问为什么不用maven,我也想用啊,可是maven下来找不到那堆类)

IDEA导入:先把包放到WEB-INF下面的lib包中,然后导入



(3)编写我们的测试代码:

public class LuceneTest {
private static Version matchVersion = Version.LUCENE_44;
private static Analyzer analyzer = new IKAnalyzer();
/*
建立索引
*/
public void index() {
IndexWriter writer = null;
try {
//1.创建Directory(是建立在内存中还是硬盘中)
//            Directory directory = new RAMDirectory();//索引建立在内存中!!!
Directory directory = FSDirectory.open(new File("e:/lucene/index01"));//索引创建在硬盘上。
//2.创建IndexWriter,用它来写索引
IndexWriterConfig iwc = new IndexWriterConfig(matchVersion, analyzer);
writer = new IndexWriter(directory, iwc);
//3.创建Document对象
Document doc = null;
//4.为Document添加Field(Field是Document的一个子元素)(Field是那些大小、内容、标题)
File f = new File("F:/lucene/example");
for (File file : f.listFiles()) {
doc = new Document();
doc.add(new Field("content",new FileReader(file)));
doc.add(new Field("filename",file.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("path",file.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
//5.通过IndexWriter添加文档到索引中
writer.addDocument(doc);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}


调用:

public class TestLuceneIndex {
@Test
public void testIndex(){
LuceneTest luceneTest = new LuceneTest();
luceneTest.index();
}
}


(4)按照我们自己编写的目录,建立个我们需要索引的文件先:随便几个txt(具体文件要求看官方文档吧!)



(5)然后就跑起来啊。Junit测试!!!



(6)然后我们就看到索引文件自动创建了



这样就完成我们整个项目需要用到的框架整合了!!!很愉快简单吧??

源码下载:J2EE项目系列(四)–SSM框架构建积分系统和基本商品检索系统(Spring+SpringMVC+MyBatis+Lucene+Redis+MAVEN)

好了,J2EE项目系列(四)–SSM框架构建积分系统和基本商品检索系统(Spring+SpringMVC+MyBatis+Lucene+Redis+MAVEN)(2)建立商品数据库和Lucene的搭建讲完了。本博客系列是我做其中一个项目时候需要整合的部分东西(分布式的往后补上),我把里面的整合,以及部分非算法性的业务逻辑写出来(大家别看这边博客这么少东西,其实就那个数据库搞了我几个钟),分享经验给大家。欢迎在下面指出错误,共同学习!!你的点赞是对我最好的支持!!

更多内容,可以访问JackFrost的博客

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐