提交 c112d855 编写于 作者: N nicky

修改项目工程架构

上级 23140e29
......@@ -42,6 +42,8 @@
<module>taoshop-common</module>
<module>project-search</module>
<module>project-redis</module>
<module>project-mongodb</module>
<module>project-dubbo</module>
</modules>
<build>
......
### MongoDB通用功能框架
//TODO 要求实现简便,高性能
\ No newline at end of file
<?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">
<parent>
<artifactId>taoshop</artifactId>
<groupId>org.muses</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>project-mongodb</artifactId>
<packaging>jar</packaging>
</project>
\ No newline at end of file
package com.muses.base.search;
public class LuceneConstants {
}
package com.muses.base.search;
public class TestLucene {
}
package com.muses.base.search.biz;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.springframework.stereotype.Component;
/**
* <pre>
* Lucene创建索引服务类
* </pre>
*
* @author nicky
* @version 1.00.00
*
* <pre>
* 修改记录
* 修改后版本: 修改人: 修改日期:2018年04月18日 修改内容:
* </pre>
*/
@Component
public class LuceneIndexer {
private volatile static LuceneIndexer instance;
//
// private LuceneIndexer(){}
/**
* 双检锁/双重校验锁(DCL,即 double-checked locking)
* @return instance
*/
// public static LuceneIndexer getInstance(){
// if(instance == null){
// synchronized (LuceneIndexer.class) {
// if(instance == null){
// instance = new LuceneIndexer();
// }
// }
// }
// return instance;
// }
// private static Analyzer analyzer;
// private static Directory directory;
private IndexWriter indexWriter;
// private static IndexWriterConfig config;
private final static String INDEX_DIR = "D:\\lucene";
private static class SingletonHolder{
private final static LuceneIndexer instance=new LuceneIndexer();
}
public static LuceneIndexer getInstance(){
return SingletonHolder.instance;
}
public boolean createIndex(String indexDir) throws IOException{
//加点测试的静态数据
Integer ids[] = {1 , 2 , 3};
String titles[] = {"标题1" , "标题2" , "标题3"};
String tcontents[] = {
"内容1内容啊哈哈哈",
"内容2内容啊哈哈哈",
"内容3内容啊哈哈哈"
};
long startTime = System.currentTimeMillis();//记录索引开始时间
Analyzer analyzer = new SmartChineseAnalyzer();
Directory directory = FSDirectory.open(Paths.get(indexDir));
IndexWriterConfig config = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(directory, config);
for(int i = 0; i < ids.length;i++){
Document doc = new Document();
//添加字段
doc.add(new IntField("id", ids[i],Field.Store.YES)); //添加内容
doc.add(new TextField("title", titles[i], Field.Store.YES)); //添加文件名,并把这个字段存到索引文件里
doc.add(new TextField("tcontent", tcontents[i], Field.Store.YES)); //添加文件路径
indexWriter.addDocument(doc);
}
indexWriter.commit();
System.out.println("共索引了"+indexWriter.numDocs()+"个文件");
indexWriter.close();
System.out.println("创建索引所用时间:"+(System.currentTimeMillis()-startTime)+"毫秒");
return true;
}
// private void addDocument(File file) throws IOException{
// Document doc = new Document();
// //添加字段
// doc.add(new TextField("contents", new FileReader(file))); //添加内容
// doc.add(new TextField("fileName", file.getName(), Field.Store.YES)); //添加文件名,并把这个字段存到索引文件里
// doc.add(new TextField("fullPath", file.getCanonicalPath(), Field.Store.YES)); //添加文件路径
//
// indexWriter.addDocument(doc);
// }
//
// private void closeWriter() throws IOException{
// if (indexWriter != null) {
// indexWriter.close();
// }
// }
public static void main(String[] args) {
try {
boolean r = LuceneIndexer.getInstance().createIndex(INDEX_DIR);
if(r){
System.out.println("索引创建成功!");
}else{
System.out.println("索引创建失败!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.muses.base.search.biz;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Paths;
/**
* <pre>
* Lucene全局搜索服务类
* </pre>
*
* @author nicky
* @version 1.00.00
*
* <pre>
* 修改记录
* 修改后版本: 修改人: 修改日期:2018年04月18日 修改内容:
* </pre>
*/
public class SearchBuilder {
public static void doSearch(String indexDir , String queryStr) throws IOException, ParseException, InvalidTokenOffsetsException {
Directory directory = FSDirectory.open(Paths.get(indexDir));
DirectoryReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new SmartChineseAnalyzer();
QueryParser parser = new QueryParser("tcontent",analyzer);
Query query = parser.parse(queryStr);
long startTime = System.currentTimeMillis();
TopDocs docs = searcher.search(query,10);
System.out.println("查找"+queryStr+"所用时间:"+(System.currentTimeMillis()-startTime));
System.out.println("查询到"+docs.totalHits+"条记录");
//加入高亮显示的
SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<b><font color=red>","</font></b>");
QueryScorer scorer = new QueryScorer(query);//计算查询结果最高的得分
Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);//根据得分算出一个片段
Highlighter highlighter = new Highlighter(simpleHTMLFormatter,scorer);
highlighter.setTextFragmenter(fragmenter);//设置显示高亮的片段
//遍历查询结果
for(ScoreDoc scoreDoc : docs.scoreDocs){
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("title"));
System.out.println(doc.get("tcontent"));
String tcontent = doc.get("tcontent");
if(tcontent != null){
TokenStream tokenStream = analyzer.tokenStream("tcontent", new StringReader(tcontent));
String summary = highlighter.getBestFragment(tokenStream, tcontent);
System.out.println(summary);
}
}
reader.close();
}
public static void main(String[] args){
String indexDir = "D:\\lucene";
String q = "内容1"; //查询这个字符串
try {
doSearch(indexDir, q);
} catch (Exception e) {
e.printStackTrace();
}
}
}
......@@ -10,6 +10,7 @@ import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
......@@ -59,9 +60,7 @@ public class LuceneIndexer {
// private static IndexWriterConfig config;
private final static String INDEX_DIR = "D:\\lucene";
private final static String DATA_DIR = "D:\\lucene\\data";
private static class SingletonHolder{
private final static LuceneIndexer instance=new LuceneIndexer();
}
......@@ -70,7 +69,16 @@ public class LuceneIndexer {
return SingletonHolder.instance;
}
public static boolean createIndex(String indexDir , String dataDir) throws IOException{
public boolean createIndex(String indexDir) throws IOException{
//加点测试的静态数据
Integer ids[] = {1 , 2 , 3};
String titles[] = {"标题1" , "标题2" , "标题3"};
String tcontents[] = {
"内容1内容啊哈哈哈",
"内容2内容啊哈哈哈",
"内容3内容啊哈哈哈"
};
long startTime = System.currentTimeMillis();//记录索引开始时间
Analyzer analyzer = new SmartChineseAnalyzer();
......@@ -78,48 +86,43 @@ public class LuceneIndexer {
IndexWriterConfig config = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(directory, config);
File[] files = new File(dataDir).listFiles();
for(File file : files){
for(int i = 0; i < ids.length;i++){
Document doc = new Document();
//添加字段
doc.add(new TextField("contents", new FileReader(file))); //添加内容
doc.add(new TextField("fileName", file.getName(), Field.Store.YES)); //添加文件名,并把这个字段存到索引文件里
doc.add(new TextField("fullPath", file.getCanonicalPath(), Field.Store.YES)); //添加文件路径
doc.add(new IntField("id", ids[i],Field.Store.YES)); //添加内容
doc.add(new TextField("title", titles[i], Field.Store.YES)); //添加文件名,并把这个字段存到索引文件里
doc.add(new TextField("tcontent", tcontents[i], Field.Store.YES)); //添加文件路径
indexWriter.addDocument(doc);
}
System.out.println("共索引了"+indexWriter.numDocs()+"个文件");
indexWriter.commit();
System.out.println("共索引了"+indexWriter.numDocs()+"个文件");
indexWriter.close();
System.out.println("创建索引所用时间:"+(System.currentTimeMillis()-startTime)+"毫秒");
return true;
}
private void addDocument(File file) throws IOException{
Document doc = new Document();
//添加字段
doc.add(new TextField("contents", new FileReader(file))); //添加内容
doc.add(new TextField("fileName", file.getName(), Field.Store.YES)); //添加文件名,并把这个字段存到索引文件里
doc.add(new TextField("fullPath", file.getCanonicalPath(), Field.Store.YES)); //添加文件路径
indexWriter.addDocument(doc);
}
private void closeWriter() throws IOException{
if (indexWriter != null) {
indexWriter.close();
}
}
// private void addDocument(File file) throws IOException{
// Document doc = new Document();
// //添加字段
// doc.add(new TextField("contents", new FileReader(file))); //添加内容
// doc.add(new TextField("fileName", file.getName(), Field.Store.YES)); //添加文件名,并把这个字段存到索引文件里
// doc.add(new TextField("fullPath", file.getCanonicalPath(), Field.Store.YES)); //添加文件路径
//
// indexWriter.addDocument(doc);
// }
//
// private void closeWriter() throws IOException{
// if (indexWriter != null) {
// indexWriter.close();
// }
// }
public static void main(String[] args) {
try {
boolean r = LuceneIndexer.getInstance().createIndex(INDEX_DIR,DATA_DIR);
boolean r = LuceneIndexer.getInstance().createIndex(INDEX_DIR);
if(r){
System.out.println("索引创建成功!");
}else{
......
package com.test.lucene;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
......@@ -10,20 +11,22 @@ import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Paths;
public class SearchBuilder {
public static void doSearch(String indexDir , String queryStr) throws IOException, ParseException {
public static void doSearch(String indexDir , String queryStr) throws IOException, ParseException, InvalidTokenOffsetsException {
Directory directory = FSDirectory.open(Paths.get(indexDir));
DirectoryReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new SmartChineseAnalyzer();
QueryParser parser = new QueryParser("contents",analyzer);
QueryParser parser = new QueryParser("tcontent",analyzer);
Query query = parser.parse(queryStr);
long startTime = System.currentTimeMillis();
......@@ -32,16 +35,31 @@ public class SearchBuilder {
System.out.println("查找"+queryStr+"所用时间:"+(System.currentTimeMillis()-startTime));
System.out.println("查询到"+docs.totalHits+"条记录");
//加入高亮显示的
SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<b><font color=red>","</font></b>");
QueryScorer scorer = new QueryScorer(query);//计算查询结果最高的得分
Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);//根据得分算出一个片段
Highlighter highlighter = new Highlighter(simpleHTMLFormatter,scorer);
highlighter.setTextFragmenter(fragmenter);//设置显示高亮的片段
//遍历查询结果
for(ScoreDoc scoreDoc : docs.scoreDocs){
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("fullPath"));
System.out.println(doc.get("title"));
String tcontent = doc.get("tcontent");
if(tcontent != null){
TokenStream tokenStream = analyzer.tokenStream("tcontent", new StringReader(tcontent));
String summary = highlighter.getBestFragment(tokenStream, tcontent);
System.out.println(summary);
}
}
reader.close();
}
public static void main(String[] args){
String indexDir = "D:\\lucene";
String q = "test"; //查询这个字符串
String q = "内容1"; //查询这个字符串
try {
doSearch(indexDir, q);
} catch (Exception e) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册