Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
smileNicky
taoshop
提交
c112d855
T
taoshop
项目概览
smileNicky
/
taoshop
通知
4
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
2
Wiki
1
Wiki
分析
仓库
DevOps
项目成员
Pages
T
taoshop
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
2
合并请求
2
Pages
分析
分析
仓库分析
DevOps
Wiki
1
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c112d855
编写于
4月 19, 2018
作者:
N
nicky
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改项目工程架构
上级
23140e29
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
290 addition
and
53 deletion
+290
-53
src/pom.xml
src/pom.xml
+2
-0
src/project-cache/ReadMe.md
src/project-cache/ReadMe.md
+0
-0
src/project-cache/pom.xml
src/project-cache/pom.xml
+0
-0
src/project-mongodb/ReadMe.md
src/project-mongodb/ReadMe.md
+0
-2
src/project-mongodb/pom.xml
src/project-mongodb/pom.xml
+0
-15
src/project-remote/ReadMe.md
src/project-remote/ReadMe.md
+0
-0
src/project-remote/pom.xml
src/project-remote/pom.xml
+0
-0
src/project-search/src/main/java/com/muses/base/search/LuceneConstants.java
.../src/main/java/com/muses/base/search/LuceneConstants.java
+4
-0
src/project-search/src/main/java/com/muses/base/search/TestLucene.java
...earch/src/main/java/com/muses/base/search/TestLucene.java
+5
-0
src/project-search/src/main/java/com/muses/base/search/biz/LuceneIndexer.java
...rc/main/java/com/muses/base/search/biz/LuceneIndexer.java
+139
-0
src/project-search/src/main/java/com/muses/base/search/biz/SearchBuilder.java
...rc/main/java/com/muses/base/search/biz/SearchBuilder.java
+83
-0
src/project-search/src/test/java/com/test/lucene/LuceneIndexer.java
...t-search/src/test/java/com/test/lucene/LuceneIndexer.java
+35
-32
src/project-search/src/test/java/com/test/lucene/SearchBuilder.java
...t-search/src/test/java/com/test/lucene/SearchBuilder.java
+22
-4
未找到文件。
src/pom.xml
浏览文件 @
c112d855
...
...
@@ -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>
...
...
src/project-
redis
/ReadMe.md
→
src/project-
cache
/ReadMe.md
浏览文件 @
c112d855
文件已移动
src/project-
redis
/pom.xml
→
src/project-
cache
/pom.xml
浏览文件 @
c112d855
文件已移动
src/project-mongodb/ReadMe.md
已删除
100644 → 0
浏览文件 @
23140e29
### MongoDB通用功能框架
//TODO 要求实现简便,高性能
\ No newline at end of file
src/project-mongodb/pom.xml
已删除
100644 → 0
浏览文件 @
23140e29
<?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
src/project-
dubbo
/ReadMe.md
→
src/project-
remote
/ReadMe.md
浏览文件 @
c112d855
文件已移动
src/project-
dubbo
/pom.xml
→
src/project-
remote
/pom.xml
浏览文件 @
c112d855
文件已移动
src/project-search/src/main/java/com/muses/base/search/LuceneConstants.java
0 → 100644
浏览文件 @
c112d855
package
com.muses.base.search
;
public
class
LuceneConstants
{
}
src/project-search/src/main/java/com/muses/base/search/TestLucene.java
0 → 100644
浏览文件 @
c112d855
package
com.muses.base.search
;
public
class
TestLucene
{
}
src/project-search/src/main/java/com/muses/base/search/biz/LuceneIndexer.java
0 → 100644
浏览文件 @
c112d855
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
();
}
}
}
src/project-search/src/main/java/com/muses/base/search/biz/SearchBuilder.java
0 → 100644
浏览文件 @
c112d855
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
();
}
}
}
src/project-search/src/test/java/com/test/lucene/LuceneIndexer.java
浏览文件 @
c112d855
...
...
@@ -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
{
...
...
src/project-search/src/test/java/com/test/lucene/SearchBuilder.java
浏览文件 @
c112d855
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录