|
1、源码
0 J4 p% n- y' C! j% d" M- ~$ limport java.nio.file.Paths;: n: ]% {: t4 T/ x; o1 ]
# N' I; S1 b# m+ m$ X% ^7 X
import org.apache.lucene.analysis.Analyzer;$ a5 s7 Q, G6 L1 ^$ l
import org.apache.lucene.analysis.standard.StandardAnalyzer;6 Z% x& m, S& X+ ~2 ^
import org.apache.lucene.document.Document;8 Q( v0 V, B2 S8 X. B. \+ e/ |
import org.apache.lucene.document.Field;
- u2 `. {5 t9 x3 }& H7 X5 Rimport org.apache.lucene.document.StringField;
' }" C- c, J4 {' C; A+ I2 S6 ~import org.apache.lucene.document.TextField;8 Z7 o- s- m4 C5 O* Z. ?* R
import org.apache.lucene.index.DirectoryReader;
) E* b9 B, j5 ?" N4 Jimport org.apache.lucene.index.IndexReader;0 _& S# {) `' C8 u6 E- n
import org.apache.lucene.index.IndexWriter;
1 K3 f( `1 |' x4 u4 e0 Uimport org.apache.lucene.index.IndexWriterConfig;
) k& h0 F6 T9 Z/ u0 R2 E* Oimport org.apache.lucene.index.Term; e! Y; `9 a7 g5 w, ^
import org.apache.lucene.store.Directory;
1 y& Q U! d/ R" D# R* Y3 O: D Iimport org.apache.lucene.store.FSDirectory;
2 E1 S+ J" W! t V1 a0 f. ?import org.junit.Before;9 ~1 A9 ]3 @! j& E! R; c
import org.junit.Test;* G( l* Q# D4 I6 [* _' C
" n/ M) q; T; @& |# bpublic class IndexingTest {: Y& ]7 w6 p j* X6 Z0 h3 Q
# }( H6 r6 }9 ^. @+ P0 p private String ids[]={"1","2","3"};. C& h: B. l+ x% X
private String citys[]={"qingdao","nanjing","shanghai"};
- F& l$ l% |$ S. Z" o private String descs[]={# N. s/ U, Z# ^ O2 L
"Qingdao is a beautiful city.",& r- k6 V9 ]" S) Z
"Nanjing is a city of culture.",
7 H5 N+ h# q% e0 K "Shanghai is a bustling city."2 {& {( W% d9 t. j( I$ ]$ Q
};
% i( A' `; e9 j V% |( G8 L0 a( T" l, j# ]/ R1 e3 i5 i
private Directory dir;
; \ q' g/ ^1 a- |9 K. E# K a& r3 M) @1 Q
@Before" ]! ]4 t: _4 U X8 f1 Y* K& p
public void setUp() throws Exception {
; K$ l; V( A( Y5 {2 `& _ dir=FSDirectory.open(Paths.get("D:\\lucene2")); }1 K6 T" G: R# ]
IndexWriter writer=getWriter();* d4 f+ `- k( E* [, |2 E
for(int i=0;i<ids.length;i++){
3 q; y1 J* f+ b5 z+ y Document doc=new Document();
. M7 U/ k( Y0 `) M% ^ doc.add(new StringField("id", ids, Field.Store.YES));( K% c8 U9 C! r: n# T- ^4 p' Y
doc.add(new StringField("city",citys,Field.Store.YES));9 m) w, i" @6 w/ K
doc.add(new TextField("desc", descs, Field.Store.NO));
S" M+ _ e. @8 ^' W3 k, t/ D2 _ writer.addDocument(doc); // 添加文档( f6 p) R( U* k8 B: M& o9 n# R1 ~
}
* p0 j" f% {" V7 v- n, z7 ]( U writer.close();9 G' `8 c' n" y( t5 s
}
( _3 U. j5 C' o" d. X: q( a3 `$ x$ T2 z6 W6 k8 T6 P$ y W. K
/**
( X, t# x) c% o W * 获取IndexWriter实例8 @ @+ q% I0 ?/ Q; N
* @return: N+ i ^$ e/ g9 i# C
* @throws Exception- l) ?1 l0 h, {: n+ h
*/2 @' i m7 l& m4 E' y! x* ]- Y) S/ a
private IndexWriter getWriter()throws Exception{
$ J# O& m$ [6 a% G+ d Analyzer analyzer=new StandardAnalyzer(); // 标准分词器$ q1 Y0 O$ Y$ W8 O; o4 f) s( U
IndexWriterConfig iwc=new IndexWriterConfig(analyzer);' P9 V! n4 F, H( u- H+ N+ j
IndexWriter writer=new IndexWriter(dir, iwc);
! C; U4 [6 T" Y/ @) r4 P4 F return writer;. }2 C% S: @% a4 S4 \7 p- C
}
0 o2 o9 Q" w* j$ G# E& |' w/ W
/ F. w* v/ `( U* |& l8 m6 ^ /**
+ ^9 ~& B$ u- l! m * 测试写了几个文档5 D' l n' `" J) ]
* @throws Exception* D* a4 \, w' ]; G
*/
: i' w8 R6 r- z7 I @Test
, m+ `5 K: |3 @9 F; ^4 z public void testIndexWriter()throws Exception{
& T6 @6 B W" R8 e" k# P IndexWriter writer=getWriter();
1 F4 j4 a, r+ _ Z$ |* |3 x" S System.out.println("写入了"+writer.numDocs()+"个文档");
' C$ |2 I. c' H& Y) y/ m( t. W writer.close();
& m- p+ P/ E2 d- V# b) c) R' B& [' X }# I( \+ h7 R% O6 j- o+ v8 u6 C# {
J" ?" y0 S- w! b8 ^9 d
/**! T1 t* p# l, w; s! ]/ f1 q
* 测试读取文档& E! s+ x9 o9 `5 `
* @throws Exception% b5 T6 {% `0 E
*/! W) R; M7 n$ w- g. Q! }
@Test
0 X* W; b8 j5 V+ e+ B; p } public void testIndexReader()throws Exception{
/ n; p4 r- ]3 Y3 D! r4 ]9 V; ` IndexReader reader=DirectoryReader.open(dir);
+ m: H% K$ \0 {9 a4 I2 u9 w System.out.println("最大文档数:"+reader.maxDoc());
: d; ~, ~' [+ k! X+ S System.out.println("实际文档数:"+reader.numDocs());
9 A: M& h1 W4 @2 p% k+ c$ u+ B" B reader.close();
) l& p, @. G" e; q1 r/ g }
& M! X3 v5 A _
* M9 s# G+ w' j9 q( k5 U3 @2 E /**6 t$ k; w2 ]; e/ v! G F
* 测试删除 在合并前
0 [4 R P( v8 q2 u! t' A; a5 d * @throws Exception5 D3 z F) p& T, R. w+ G# Z4 t6 D, h9 n
*/
+ @' K$ P" e. |8 o0 k2 @& g @Test# ~7 r( ~5 W9 [2 t
public void testDeleteBeforeMerge()throws Exception{" M( r8 V, O% H6 B/ T0 H9 s i
IndexWriter writer=getWriter();, q3 @+ m; c- U, v; n( Y* N
System.out.println("删除前:"+writer.numDocs());; g( V/ k0 T" s a1 w7 ]
writer.deleteDocuments(new Term("id","1"));3 C6 L$ ?/ U- ]$ A, W( c5 g, ]+ N
writer.commit();
2 I- ^3 @4 `6 A. y3 H% ` System.out.println("writer.maxDoc():"+writer.maxDoc());4 l8 i0 E7 X& Z2 T% v4 Q1 n
System.out.println("writer.numDocs():"+writer.numDocs());
' F) u% e# T3 ]5 ` writer.close();
) B6 E! y( {+ d }
/ m; h4 h" n7 d/ w. a+ z3 a1 F/ T6 S' Z) \
/**) m. a4 ^# k' y/ u7 b7 t0 {- D
* 测试删除 在合并后
( L- h6 o* F% b2 z9 ~' h, d * @throws Exception
4 y X" x; @* \$ i2 o */
' z* w7 t. m1 J2 P- G @Test
7 B7 `( S" l0 g+ B9 \ public void testDeleteAfterMerge()throws Exception{. d$ K4 p8 u! B; M7 [
IndexWriter writer=getWriter();
+ f/ o& E* N# i: d1 A: X System.out.println("删除前:"+writer.numDocs());
6 g1 r( s! ~1 E6 a writer.deleteDocuments(new Term("id","1"));: v5 C) \' v, {
writer.forceMergeDeletes(); // 强制删除
, X( }8 ?; C* \( R1 k writer.commit();
" L' W" g( s1 h/ Y* l System.out.println("writer.maxDoc():"+writer.maxDoc());( L- h$ q4 `! Z( D4 N; O
System.out.println("writer.numDocs():"+writer.numDocs());+ G6 r9 w' `, X. s! n. S- J
writer.close();' g6 G& a& B+ y" O; l% A0 F, A
}7 @' @1 U0 z4 w, y
N/ ?+ I; H0 u. Y
/**
# J" q1 n, m- p4 o0 ] M * 测试更新
+ V% I! J* D, R) w" F9 O * @throws Exception
2 x; ]$ O) d6 o) I0 |7 z+ \1 x& n */
, k% r9 g; T7 Q- U; D @Test+ P7 q; u0 {% L0 A& y* T3 @' H
public void testUpdate()throws Exception{& Q3 {2 O; }: G
IndexWriter writer=getWriter();" v6 v7 e3 d) c$ [6 O/ i$ _
Document doc=new Document();8 \$ ?9 T' z' b" D4 Y
doc.add(new StringField("id", "1", Field.Store.YES));# K1 u Q$ f; A. e, y
doc.add(new StringField("city","qingdao",Field.Store.YES));; ^4 c o+ |% j. l8 l9 F4 S9 t- T* J5 G
doc.add(new TextField("desc", "dsss is a city.", Field.Store.NO));
1 e3 W& z0 ? P6 |. X3 N1 a" k$ w writer.updateDocument(new Term("id","1"), doc);# |- C. d4 d6 |
writer.close();$ R! X% m7 a( X4 x) i+ U8 R0 ]
}6 o" y' w$ w1 e9 q2 ], R7 a* W; P
}
. K' e, ?3 o4 q0 ]
# `, K8 L3 L- {$ g( y9 k. I3 D. Q& G# k3 A& f1 a9 A* x* J
7 Y; S2 D5 A" H# m. ]( R7 P |
|