|
1、源码9 u/ _" H# ?' p1 l# N2 [
import java.nio.file.Paths;9 v+ |$ P6 A4 i/ Q+ f: c
0 ~4 V# o! P' X. r; r# M6 W- G
import org.apache.lucene.analysis.Analyzer;
7 G Q$ g2 C ^import org.apache.lucene.analysis.standard.StandardAnalyzer;
3 z1 `- I$ i- ]% `# |0 V5 |; ximport org.apache.lucene.document.Document;
0 J9 {% _5 |( r0 M) yimport org.apache.lucene.document.Field;% Z" k4 S+ u# Z; j/ c1 o( c7 p
import org.apache.lucene.document.StringField;
k( C/ ~) `& I6 Iimport org.apache.lucene.document.TextField;
+ W: W% e Y, |7 R! Q2 }2 aimport org.apache.lucene.index.DirectoryReader;
8 S0 H x) a0 S2 k2 @5 H |/ himport org.apache.lucene.index.IndexReader;0 C2 R/ _. P" ~' j% O$ k
import org.apache.lucene.index.IndexWriter;
# e- E% L! P/ @( {6 t& _& A1 Fimport org.apache.lucene.index.IndexWriterConfig;/ i3 V& M+ P3 e; e+ ]
import org.apache.lucene.index.Term;1 X% V' [" N% r/ r1 ^* w
import org.apache.lucene.store.Directory;
) Q9 ?: ?7 u9 @7 L) iimport org.apache.lucene.store.FSDirectory;
& ^2 J ~& b3 c3 S4 o5 t2 a! ^import org.junit.Before;
2 k, z9 K- |1 b0 }import org.junit.Test;2 X* \! W9 U& D; a3 V
* e7 g9 l8 i8 q; ?7 W% U% ~6 r
public class IndexingTest {
, w9 b$ x6 W2 m+ d1 i3 Y0 a
3 T) g* U; R8 R" Q8 Q/ E. ~+ U private String ids[]={"1","2","3"};( }+ w0 H+ [4 V% C0 a4 H4 v
private String citys[]={"qingdao","nanjing","shanghai"};
+ E6 v* [, E# F; { private String descs[]={" q) [, c0 ^; l+ v+ }
"Qingdao is a beautiful city.",3 l% c( y) [* {; I
"Nanjing is a city of culture.",
) C, N( J' Z9 c( u8 F5 Z "Shanghai is a bustling city."
) L; {+ C2 Y9 k! y( P% L3 ^ };
& n; N& h! B* Q0 D& N$ r
+ M! `. W, L7 f" j! C private Directory dir;* M; H9 `- h7 f" i2 e+ Z
# B/ G' O9 S! }! o. l2 F @Before" h6 L6 \9 a* @% d
public void setUp() throws Exception {
! j' p1 ?! u1 U' g dir=FSDirectory.open(Paths.get("D:\\lucene2"));
4 l% x0 a& V! _% k2 `* U4 T' Y IndexWriter writer=getWriter();* ]; P2 b. M! Y
for(int i=0;i<ids.length;i++){
1 X3 G& `$ P5 ]) l U Document doc=new Document();# C; j) I3 c4 f; Y, q2 e M4 V
doc.add(new StringField("id", ids, Field.Store.YES));# u! L+ H; w* T; ?. S q' o
doc.add(new StringField("city",citys,Field.Store.YES));- B0 f9 ~* y& J% ]. A. U6 C
doc.add(new TextField("desc", descs, Field.Store.NO));
: {( O+ f' x: }; V& V+ V: n {& h M3 d writer.addDocument(doc); // 添加文档
5 A3 |9 j8 e x# b) U9 D$ c1 ] }
! ]# h6 V/ D. u' y7 z6 h writer.close();& ~) g# C% U4 o2 U
}
. b5 b7 \. T1 q6 ?
, Y7 R' [0 a: r /**
9 t" Y0 J! L5 O% `6 i$ m" ~# x6 C. O * 获取IndexWriter实例
4 B |0 B: ^/ e- N' H$ \ * @return6 U% k, w/ k4 ? d2 Y7 ?/ t( Z0 l4 e
* @throws Exception# q+ B& ^8 r r0 R4 H8 g
*/
0 ^1 l. L) |# ~3 j1 i8 @ private IndexWriter getWriter()throws Exception{
3 I, d4 r1 v$ Y7 G Analyzer analyzer=new StandardAnalyzer(); // 标准分词器
. j1 U% i* }5 u4 L: M9 s! Q" I IndexWriterConfig iwc=new IndexWriterConfig(analyzer);
+ b" U+ k4 k, `# c/ { IndexWriter writer=new IndexWriter(dir, iwc);1 K; m7 K0 s) A3 q& _% d: g
return writer;; `9 p) M2 B8 W
}
6 D6 f) W3 r" ?0 F$ H3 e( V+ c. T0 ~ r( Z
/**8 }# n$ g0 {) c# Y
* 测试写了几个文档
2 }) z) c# k$ r2 ? * @throws Exception
3 P8 G- B* b- _& c */+ U. |& X8 T! k( l, F
@Test
, {) X: b& B+ B( N- s public void testIndexWriter()throws Exception{
2 J" b" z( n- Q IndexWriter writer=getWriter();
8 b" I) Z% t" w! j System.out.println("写入了"+writer.numDocs()+"个文档");
$ s" g, i' V1 H, | P# W @" S8 z writer.close();2 t5 U4 j/ i; m! c9 D7 D$ f( Q
}
* d7 \; F, i5 n, [" Q" L1 v
7 v, \3 \. B V6 N /**+ n4 V% P; F1 m8 O0 X
* 测试读取文档9 x" D1 L* D, B' e1 R* v
* @throws Exception
: B6 g2 U) r. F8 j9 D+ N */- f$ B. w) X* j# z' }% H
@Test
7 I _; ^0 K0 Z9 ?4 N: ` public void testIndexReader()throws Exception{2 P# {) }9 S* T e
IndexReader reader=DirectoryReader.open(dir);# b& p" t7 N0 m( V5 Y
System.out.println("最大文档数:"+reader.maxDoc());
6 O$ ~" l; r5 p0 m System.out.println("实际文档数:"+reader.numDocs());. t6 y/ X' D o( ~
reader.close();4 D0 {% c! |. Z4 b/ m* g& K; }
}/ C7 V' T$ v. B" ]* h Q! Y
! i0 \/ q& ~4 v: Q" O /**
! Y! c; w% G, F0 C! D/ `4 z3 t2 G * 测试删除 在合并前, n5 K2 n7 w1 U0 X/ V; t- l
* @throws Exception
7 {) K% g- w. q, [) ~$ N7 B */$ F2 _) a! R$ K: `
@Test% `- d7 u2 P2 ]( G4 G/ M4 C$ f1 H
public void testDeleteBeforeMerge()throws Exception{4 R) u) U8 M8 ^; }& g7 S
IndexWriter writer=getWriter();
4 U! _+ M+ p1 m System.out.println("删除前:"+writer.numDocs());
' ?4 j! d1 o5 |$ W7 a writer.deleteDocuments(new Term("id","1"));" G( [5 D1 _/ s8 i: ?
writer.commit();5 ]7 | f5 I+ d
System.out.println("writer.maxDoc():"+writer.maxDoc());
1 w+ e- t) c' m1 m" `+ r System.out.println("writer.numDocs():"+writer.numDocs());, s8 w+ u0 d. r. |3 x
writer.close();
7 i$ v( \! @" l# `' S$ ] }
" P0 `# s4 i: I7 L# N3 p3 e1 j
( G* W: ^3 V. b4 a% u* x2 M1 o /**6 d: a5 _2 A: O- @
* 测试删除 在合并后+ h6 Y* O: A2 M7 t4 h7 d
* @throws Exception, Z$ ~# w% P+ K( `' _
*/
8 N) s* b# ~# c" p! [. ]8 F @Test' A! F- R- D5 |6 C/ c# ~* G4 X9 d: p
public void testDeleteAfterMerge()throws Exception{$ x! Y- D' C" ~: R/ b. E
IndexWriter writer=getWriter();
- Q) Q o* V9 ]" r( k- o System.out.println("删除前:"+writer.numDocs());
9 f* E* P1 S6 }/ B writer.deleteDocuments(new Term("id","1"));
. v d% q6 z% O4 I writer.forceMergeDeletes(); // 强制删除
! P3 H& H% }+ P& |: O8 B& e- W! K, | writer.commit();
' N4 b1 K+ T4 h* D System.out.println("writer.maxDoc():"+writer.maxDoc());
8 H1 b, D5 g8 }; W# i& T3 B System.out.println("writer.numDocs():"+writer.numDocs());
% J/ K" |3 g# A writer.close();- N s. j, j: m0 V3 Z
}
- e8 E" ~0 C3 C/ W4 t
; W! i9 h0 w$ C, `& ` /**8 F. A4 t# j- A6 f0 Z, G, Q" a' i4 W+ j
* 测试更新0 \! L5 f- P) \6 F7 o8 R- x
* @throws Exception
" M+ T7 ]3 |+ s% a. l */
4 {3 B+ D/ h# I* o- Z' R @Test
1 n" ~& f0 d& b: z+ z public void testUpdate()throws Exception{( j" b' ?5 h% N) ~% x5 B
IndexWriter writer=getWriter();
2 d8 q; @7 |" m" v1 r Document doc=new Document();
' r1 b" }3 t7 a$ a+ {* A" y+ ~3 Y9 ^ doc.add(new StringField("id", "1", Field.Store.YES));
+ y# q" g, [' Q doc.add(new StringField("city","qingdao",Field.Store.YES));, }" V# J$ z j0 D( z1 w& M. o
doc.add(new TextField("desc", "dsss is a city.", Field.Store.NO));
" v' [: W3 }0 |$ L! ]3 j writer.updateDocument(new Term("id","1"), doc);& `1 h. z4 X# R7 w1 K
writer.close();2 X2 A) t# ?) f" T" A# b- [
}9 }; E/ {( O T/ L4 U
}
5 ]( l" u; Z9 ?9 t
& _' x2 U( ~1 e" G8 ?/ G# ^$ t o) C: y( `9 o# W
9 M" _: o. n/ y |
|