|
1、源码
0 ~9 U3 `( s5 I) n0 F; yimport java.nio.file.Paths;8 U# x* M, h |7 S/ |
! a% ~. I( ^5 Z7 {1 C, V7 [! Timport org.apache.lucene.analysis.Analyzer;
8 H1 p: K. U1 |7 cimport org.apache.lucene.analysis.standard.StandardAnalyzer;
- V6 t# u. J5 D5 ?2 z$ e6 aimport org.apache.lucene.document.Document;
! L4 m f, H9 n6 e9 Qimport org.apache.lucene.document.Field;5 O3 k- ]3 Q8 ~2 O! u. c0 X8 m
import org.apache.lucene.document.StringField;/ |: v& \+ o$ H6 w5 @ K
import org.apache.lucene.document.TextField;. r2 e& `" c/ Q; p! q5 l* y
import org.apache.lucene.index.DirectoryReader;& H. `% L5 k4 n: H! f
import org.apache.lucene.index.IndexReader;
) j9 O1 u/ N4 a! \7 q0 j; a/ Limport org.apache.lucene.index.IndexWriter;' F$ e f6 k# K- v
import org.apache.lucene.index.IndexWriterConfig;
. W: L8 w& ?4 ~0 p) `8 eimport org.apache.lucene.index.Term;
8 o3 u1 n7 d* E' Z2 Vimport org.apache.lucene.store.Directory;
' f. H" @: f. @+ Q. Vimport org.apache.lucene.store.FSDirectory;
/ w" W! p6 l/ W, k, |9 Pimport org.junit.Before;
& H2 W8 O- O( c) W# T6 j U2 nimport org.junit.Test;
; }, K/ C7 ^7 l7 D1 M, S; p1 {7 X6 H; _ B H# E
public class IndexingTest {
5 A: U/ c0 V s {2 T% E; F% |( g G) O
private String ids[]={"1","2","3"};! g1 w. N$ T2 a) ]% _3 g
private String citys[]={"qingdao","nanjing","shanghai"};1 u. o# G2 u) b3 ^% M
private String descs[]={
9 A' B; {7 V1 j "Qingdao is a beautiful city.",8 ~' c5 B( L% m' q
"Nanjing is a city of culture.",
`9 Q- a; E: ^* ]. u3 @ "Shanghai is a bustling city."5 v* Y( V, b4 X- y6 J
};2 {+ Z0 W4 o/ p/ D
" h! E' s; G1 W" H) d4 n: h
private Directory dir;. \9 U7 \" R0 j8 e* H' R
+ @) u. j: p/ L! K5 a @Before% u2 q, u) `, d K9 e; j
public void setUp() throws Exception {
* S2 [/ j% y( d) ]3 y6 m: i6 G; r dir=FSDirectory.open(Paths.get("D:\\lucene2"));/ N9 @! n" l1 u. _% U
IndexWriter writer=getWriter();' s; F# @' l: K( t/ C) x A
for(int i=0;i<ids.length;i++){
% x* V% p5 E, C& d Document doc=new Document();" r" t# u" U. ?
doc.add(new StringField("id", ids, Field.Store.YES));
6 p1 i/ t- B6 n7 [) ~ doc.add(new StringField("city",citys,Field.Store.YES));
8 N& q9 }/ c8 X& }! j doc.add(new TextField("desc", descs, Field.Store.NO));
, b* x* A+ b9 x0 C& x, m writer.addDocument(doc); // 添加文档6 x/ k' n% P- ?9 M* a: F0 n8 b
}% z: e. H0 u: w V
writer.close();
) ^* ~5 i9 `+ E5 b- m% f }- k# p" S# V8 K
( [& K0 S6 j5 s& L /**
; J/ g. P! p" N+ h * 获取IndexWriter实例6 C( r( G9 Q# s, T
* @return
4 |, i; J- n$ u) D * @throws Exception
% m! I' d: b3 _7 i4 w */
6 F( L. ^: ?; o" e4 S% p' k private IndexWriter getWriter()throws Exception{6 g* N/ x% u: K$ R& n9 G
Analyzer analyzer=new StandardAnalyzer(); // 标准分词器
4 v3 W; J# H; a IndexWriterConfig iwc=new IndexWriterConfig(analyzer);
2 M2 c% i K/ N3 v1 O$ e% U IndexWriter writer=new IndexWriter(dir, iwc); a- v9 R U* {' K- A% W9 N& k9 \
return writer;) \( m3 e1 `4 r3 C" O4 n
}
) {$ t4 e2 |. e9 O, E* C* p9 y' v7 ?8 q3 x2 w; K
/**
4 r( o3 x) i% `; t * 测试写了几个文档; o- Y+ T) f% K8 B9 B
* @throws Exception
- n- R" `" s2 V( A0 W" _3 a */) s- O2 y4 M" s
@Test! U0 R- z7 O* R, N" Z, y9 K3 d
public void testIndexWriter()throws Exception{8 [' g% l H5 q- w; E. H
IndexWriter writer=getWriter();
, `, a- f, i- w System.out.println("写入了"+writer.numDocs()+"个文档");$ g7 @) o" M4 Y* i- G
writer.close();: z" |% j0 ^# l$ ^: ]
}
! F0 m2 G3 O$ |7 @' I$ J& j* p7 g6 f& D# ~' \0 x7 v
/**
3 J1 g" J; b+ {+ g * 测试读取文档$ D% X0 ]# L8 n8 J' E9 H& n
* @throws Exception
) F# \! L1 b- h */9 Y! N6 L, ?# h% z; l
@Test
0 m- t* e; a# z- z& \ public void testIndexReader()throws Exception{2 D, v/ l( e) k* X3 v0 x
IndexReader reader=DirectoryReader.open(dir);
! T/ c9 g' M9 D5 C System.out.println("最大文档数:"+reader.maxDoc());# L2 Q* i- l2 M3 n
System.out.println("实际文档数:"+reader.numDocs());
( X9 i5 T+ Y8 `& _" G8 o reader.close();
/ V' C% [3 j; G, ]( M8 Y }
% [& K( D# _- W
4 ~- s0 G( E1 i- T; b' f /**
* o( D+ X" b7 H9 S) x$ R( g * 测试删除 在合并前. j. s4 p$ N- \, O4 c0 K4 v
* @throws Exception. s% G# P1 p7 Z J! o9 x
*/" P+ V7 p; X3 j% i
@Test. k$ f T+ a- [2 J1 T* D% e! R
public void testDeleteBeforeMerge()throws Exception{
) x8 m* x: Y4 \# h% j* y5 | IndexWriter writer=getWriter();' }" ]0 ^8 r$ ^" c# c
System.out.println("删除前:"+writer.numDocs());4 q4 f! \ B; _* _" {; w- K6 Y
writer.deleteDocuments(new Term("id","1")); x5 \$ C, Q; l' b: {( C. v
writer.commit();2 w1 X- ]2 N8 J7 m( y
System.out.println("writer.maxDoc():"+writer.maxDoc()); x' W K% F+ [5 ^5 {
System.out.println("writer.numDocs():"+writer.numDocs());
6 T( D! E, s. m- q- W G writer.close();
: x. O' S2 I2 @ }
& S& S: I' L+ x' `5 s8 N* i6 o8 |. i$ ^4 t I; Q! C
/**; V* p- @" B3 O; r. l* X1 k
* 测试删除 在合并后; k2 U0 Y' E( \8 C7 C6 p
* @throws Exception- O: { A, \! j. F
*/
' U9 d% z6 W; f @Test; @& y4 V# N2 ?& I. K3 }
public void testDeleteAfterMerge()throws Exception{
$ ]- I4 J P p IndexWriter writer=getWriter();* g" Q$ @/ w( ^- L) K7 n
System.out.println("删除前:"+writer.numDocs());. \) K& H) @% |7 V1 m
writer.deleteDocuments(new Term("id","1"));- S0 y* {, R1 A! E: m* A. e
writer.forceMergeDeletes(); // 强制删除, I* {1 O7 O Y& B, R% N' |
writer.commit();& Z5 U j/ h5 M" @3 A9 z
System.out.println("writer.maxDoc():"+writer.maxDoc());1 G- u# y. |) X
System.out.println("writer.numDocs():"+writer.numDocs());
$ H2 P$ q! M# i7 B4 \ writer.close();/ `& e8 c; p- {5 w; |$ e( ]
}
& [2 U. n! e/ W. ^
! G+ x- }) R, a' ?" t' } /**2 d6 a( u8 i/ s; |. w& Q
* 测试更新
8 k% P. Y z2 ]; U8 j * @throws Exception# c5 B6 W- K b/ W- l! h7 V7 a% P* v
*/
( e5 H; r G9 Y; i, E @Test/ p7 B5 I1 \* a/ e& |
public void testUpdate()throws Exception{
1 R; e7 H* ?& h* \% X IndexWriter writer=getWriter();. [8 B1 S; A: z: ]9 Z
Document doc=new Document();
( k: ? d) M; U* g* ~) y* \) M( W5 K doc.add(new StringField("id", "1", Field.Store.YES)); w1 j2 `) k, _/ C3 a& x
doc.add(new StringField("city","qingdao",Field.Store.YES));3 W- [5 K! i# L
doc.add(new TextField("desc", "dsss is a city.", Field.Store.NO));
& m' T1 I, ^. F8 w% O0 Q2 L writer.updateDocument(new Term("id","1"), doc);
, K% r$ D o8 k, K" a3 D writer.close();) E) R8 b* s$ B3 }( o$ T0 Q
}/ H1 Q. S: v7 @/ ^- P, ?+ A" M
}) x& _5 {/ A) C. c6 j
$ y) |( Q: H. F2 P8 H( F
7 H* I+ C6 R4 b } u4 O( N3 V
9 O1 q6 ~5 l- f! s. F: H |
|