Skip to content

Commit 5db72dc

Browse files
committed
lucene
1 parent a1a02de commit 5db72dc

4 files changed

Lines changed: 500 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.ymq.lucene.run;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.ComponentScan;
6+
7+
/**
8+
* 描述:
9+
*
10+
* @author: yanpenglei
11+
* @create: 2017/11/7 17:02
12+
*/
13+
@SpringBootApplication
14+
@ComponentScan(value = {"io.ymq.lucene"})
15+
public class Startup {
16+
17+
public static void main(String[] args) {
18+
SpringApplication.run(Startup.class, args);
19+
}
20+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package io.ymq.lucene.utils;
2+
3+
4+
import org.apache.lucene.analysis.Analyzer;
5+
import org.apache.lucene.analysis.standard.StandardAnalyzer;
6+
import org.apache.lucene.document.Document;
7+
import org.apache.lucene.document.Field;
8+
import org.apache.lucene.document.TextField;
9+
import org.apache.lucene.index.IndexWriter;
10+
import org.apache.lucene.index.IndexWriterConfig;
11+
import org.apache.lucene.store.Directory;
12+
import org.apache.lucene.store.FSDirectory;
13+
14+
import java.io.File;
15+
import java.io.FileReader;
16+
import java.nio.file.Paths;
17+
18+
/**
19+
* 描述:
20+
*
21+
* @author yanpenglei
22+
* @create 2017-11-07 17:50
23+
**/
24+
public class Indexer {
25+
26+
private IndexWriter writer; // 写索引实例
27+
28+
/**
29+
* 构造方法 实例化IndexWriter
30+
*
31+
* @param indexDir
32+
* @throws Exception
33+
*/
34+
public Indexer(String indexDir) throws Exception {
35+
Directory dir = FSDirectory.open(Paths.get(indexDir));
36+
Analyzer analyzer = new StandardAnalyzer(); // 标准分词器
37+
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
38+
writer = new IndexWriter(dir, iwc);
39+
}
40+
41+
/**
42+
* 关闭写索引
43+
*
44+
* @throws Exception
45+
*/
46+
public void close() throws Exception {
47+
writer.close();
48+
}
49+
50+
/**
51+
* 索引指定目录的所有文件
52+
*
53+
* @param dataDir
54+
* @throws Exception
55+
*/
56+
public int index(String dataDir) throws Exception {
57+
File[] files = new File(dataDir).listFiles();
58+
for (File f : files) {
59+
indexFile(f);
60+
}
61+
return writer.numDocs();
62+
}
63+
64+
/**
65+
* 索引指定文件
66+
*
67+
* @param f
68+
*/
69+
private void indexFile(File f) throws Exception {
70+
System.out.println("索引文件:" + f.getCanonicalPath());
71+
Document doc = getDocument(f);
72+
writer.addDocument(doc);
73+
}
74+
75+
/**
76+
* 获取文档,文档里再设置每个字段
77+
*
78+
* @param f
79+
*/
80+
private Document getDocument(File f) throws Exception {
81+
Document doc = new Document();
82+
doc.add(new TextField("contents", new FileReader(f)));
83+
doc.add(new TextField("fileName", f.getName(), Field.Store.YES));
84+
doc.add(new TextField("fullPath", f.getCanonicalPath(), Field.Store.YES));
85+
return doc;
86+
}
87+
88+
public static void main(String[] args) {
89+
String indexDir = "F:\\lucene\\index";
90+
String dataDir = "F:\\lucene\\data";
91+
Indexer indexer = null;
92+
int numIndexed = 0;
93+
long start = System.currentTimeMillis();
94+
try {
95+
indexer = new Indexer(indexDir);
96+
numIndexed = indexer.index(dataDir);
97+
} catch (Exception e) {
98+
e.printStackTrace();
99+
} finally {
100+
try {
101+
indexer.close();
102+
} catch (Exception e) {
103+
e.printStackTrace();
104+
}
105+
}
106+
long end = System.currentTimeMillis();
107+
System.out.println("索引:" + numIndexed + " 个文件 花费了" + (end - start) + " 毫秒");
108+
}
109+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.ymq.lucene.utils;
2+
3+
/**
4+
* 描述: Lucene工具类
5+
*
6+
* @author: yanpenglei
7+
* @create: 2017/11/7 17:47
8+
*/
9+
public class LuceneUtils {
10+
11+
12+
}

0 commit comments

Comments
 (0)