|
1、源码
% O5 d$ r; c& [import java.nio.file.Paths;; _, G$ B. A/ i0 Y$ j; ]1 F
: R7 A. q* V& f. h0 l1 X2 t Vimport org.apache.lucene.analysis.Analyzer;
' U3 j" f- H) i3 W" }& }# t# Mimport org.apache.lucene.analysis.standard.StandardAnalyzer;
1 S8 i9 k5 o6 Eimport org.apache.lucene.document.Document;. Z& x [( N: A; f1 J6 F5 V
import org.apache.lucene.document.Field;
. x9 Y7 ?- k' V7 Y& Ximport org.apache.lucene.document.StringField;8 H+ l/ C1 i! s+ N3 ^
import org.apache.lucene.document.TextField;9 e( [3 ]: ?% f$ n/ l
import org.apache.lucene.index.DirectoryReader;2 v# z8 R# z, M4 f+ f1 `
import org.apache.lucene.index.IndexReader;0 Y" K" C/ u, {- j1 z% @7 n
import org.apache.lucene.index.IndexWriter;
. W9 y6 `- H( f3 a& wimport org.apache.lucene.index.IndexWriterConfig;- R: R4 ?5 i- @1 p6 R J; ~
import org.apache.lucene.index.Term;6 q* s8 q: S2 v$ \# M9 G' v0 {1 |
import org.apache.lucene.store.Directory;; R, J/ `% Y) q+ |) A8 s" W1 B7 D# A
import org.apache.lucene.store.FSDirectory;8 l% d% N5 H' f( J2 Q8 S
import org.junit.Before;% F4 w3 Q& M3 ^! [% z
import org.junit.Test;
; L4 \" K" k# U5 O( U' A, C3 p3 ~0 T, N8 P, T8 R' a
public class IndexingTest {3 q5 p. ?# B2 @. ^6 ]
5 a1 A( U- o( `5 `2 J0 `0 i private String ids[]={"1","2","3"};6 S9 `% a: S7 i9 {* i8 J! t
private String citys[]={"qingdao","nanjing","shanghai"};5 F2 r. j) J0 y$ ^0 H# ?7 C
private String descs[]={
9 N3 ?: d: c( H# [7 w9 d "Qingdao is a beautiful city.",8 i, f) D' {4 _
"Nanjing is a city of culture.",$ a0 J! {/ C1 ?
"Shanghai is a bustling city."2 Z# ~8 K/ i/ S8 @/ e5 E
};9 s+ k$ ~3 U. m* G4 G+ L
( \! O- I, E, T
private Directory dir;5 A8 D Q. M( q8 Z+ _7 j6 K& J% ~
F5 q" k1 ~4 _+ G
@Before
0 R0 u1 |7 U& p) h' H! L9 W public void setUp() throws Exception {
$ h. g. P- |7 H8 n% J dir=FSDirectory.open(Paths.get("D:\\lucene2"));; v7 o8 w, r7 P5 o" B! m
IndexWriter writer=getWriter();. f. w" O4 S [
for(int i=0;i<ids.length;i++){* l6 I0 {- Q f I" g! J
Document doc=new Document();
" R8 ]* h, J* Z doc.add(new StringField("id", ids, Field.Store.YES));
* _7 ]7 t& h2 Z8 x, b, r/ y( x doc.add(new StringField("city",citys,Field.Store.YES));
# b% r0 w* b2 O- V V3 W doc.add(new TextField("desc", descs, Field.Store.NO));: n2 z8 O/ \) z
writer.addDocument(doc); // 添加文档
, v/ E5 o8 C# z$ `" ^8 {6 D# H }' P. {+ _- U' z2 ? J, ]
writer.close();, F, g7 A% D; x$ R
}
6 k& u4 h9 [( Q- W( S# T E$ m* a; n- s* I" w
/**
6 D! w4 ]1 i: D9 |2 S$ m * 获取IndexWriter实例5 }( u7 n1 k+ R: H& X
* @return4 P v) }( s5 C' f \! [) w5 m$ q
* @throws Exception
0 B7 _! b) M+ b# O+ [- h */
, @0 E; Q& I4 B0 r! D' @0 D' A& z private IndexWriter getWriter()throws Exception{
5 o7 M: C% R1 V Analyzer analyzer=new StandardAnalyzer(); // 标准分词器. w Z0 J: A. T: n
IndexWriterConfig iwc=new IndexWriterConfig(analyzer);
* r. I( ^1 t0 p" Q IndexWriter writer=new IndexWriter(dir, iwc);# s" U! @& ^7 f) c) B$ y
return writer;1 ~! O1 `1 v) j: p
}
( b) r/ x/ Z$ ]2 R5 K- H0 y) T7 k7 s
/**; D9 P$ P/ l2 c5 x# Q2 H
* 测试写了几个文档
. Q( r- j/ m T6 O- F. H * @throws Exception8 h. n& D: r& |% P, o L, Q# Q
*/% @, o$ K3 m* V% ]7 S9 A8 b1 d( m
@Test, f+ `2 y/ I! q' m
public void testIndexWriter()throws Exception{
+ e' _% R6 k; H' x; a IndexWriter writer=getWriter();
; c: Q z; n, T System.out.println("写入了"+writer.numDocs()+"个文档");
* X; P6 c/ b! z! }9 O2 ^ writer.close();
% {9 Q* U/ Z; T8 s }+ x# ?* C" w! f5 M# d; f4 ~ F
3 P& s4 Q4 f" G- Z /** j( c; U* u& ?6 s0 h3 |% }
* 测试读取文档
7 t" g: ]2 l$ l. [ * @throws Exception
% A- ~, c) ?3 w% T */
~0 H& z1 ~; j4 `) q& k6 m @Test# M7 u/ x- U& r( C4 U. U. a
public void testIndexReader()throws Exception{
& M4 Z9 t# o T6 @5 n IndexReader reader=DirectoryReader.open(dir);
9 [3 e3 D" O( g, Y+ _ System.out.println("最大文档数:"+reader.maxDoc());3 }4 u' W$ R+ l/ T3 N \/ d
System.out.println("实际文档数:"+reader.numDocs());8 E0 Z2 z7 }# z M& V
reader.close();
& j& x0 o9 k: b5 } }9 R# `8 X, e7 y% c% `5 O
0 [0 M& a j# i; I2 S1 H
/**, O- i# ` D1 [( m5 j
* 测试删除 在合并前
$ x0 S t6 I- h0 P' G: l4 u& n/ d. a * @throws Exception& U* {! Y p% u+ g) C
*/
* y e/ K: {, q! \1 `+ G: ] @Test
* t6 N- t" E" {1 l" g public void testDeleteBeforeMerge()throws Exception{+ H8 F+ l+ r7 X* z" b9 c
IndexWriter writer=getWriter();) D- ?* `9 X; G { b
System.out.println("删除前:"+writer.numDocs());" p* U% F! s" J6 w7 u
writer.deleteDocuments(new Term("id","1"));; |( X1 a$ J4 O" a* C* d
writer.commit();
4 \: T& i9 h2 ^$ a3 m System.out.println("writer.maxDoc():"+writer.maxDoc());3 R) w: q. S: H0 X! W
System.out.println("writer.numDocs():"+writer.numDocs());
- }1 C8 P' g. Y writer.close();
3 ?8 D5 z% s3 S8 ~* {! b: O2 h0 B }
l: C- o! J% T+ u9 ~- s0 a# n4 H
3 c* X+ M( q( k: X! l /**; Y3 i8 e0 v% e6 K; e. u. g U
* 测试删除 在合并后
: a: E/ r- N8 C * @throws Exception
4 Z8 z1 B5 _/ u( Z& F */3 T6 t; o! L+ I
@Test0 j* |& Z8 g. F1 l6 Z
public void testDeleteAfterMerge()throws Exception{; G6 a j. l, r
IndexWriter writer=getWriter();* @1 i Y- r0 j; v0 `' A9 J
System.out.println("删除前:"+writer.numDocs());4 J- u( }5 K% U
writer.deleteDocuments(new Term("id","1"));
- q/ u' F3 P) k; J0 ?+ K1 R writer.forceMergeDeletes(); // 强制删除. h0 C a! r( k, j
writer.commit();( U) q* |! k2 \( j/ k( B6 @+ s
System.out.println("writer.maxDoc():"+writer.maxDoc());" z& Z7 |3 _: r' b! Z; A
System.out.println("writer.numDocs():"+writer.numDocs());- D$ V3 C3 I" b4 h& h/ a
writer.close();$ N* i- K" |5 U- d' ?3 G D
}
, ~9 A* p3 t: U6 o. o% C/ u6 p, e$ r2 o: y
/**
. C* w2 p6 d" Y4 _" U' w2 C6 m% o * 测试更新4 N% ?, D$ E; h" u# O
* @throws Exception
. d$ [/ Y! I3 j */
3 A! W% }! K3 g8 E# K9 B) J @Test
0 r7 Y+ Q! Q6 [* F public void testUpdate()throws Exception{
/ M/ n2 y0 j* o V; e) G2 W9 e IndexWriter writer=getWriter();
$ k0 i" P- f6 `/ B7 H7 E3 z- F Document doc=new Document();
" j! ]4 t$ T4 z1 U doc.add(new StringField("id", "1", Field.Store.YES));
: V- ~1 Z5 L& j/ r" d doc.add(new StringField("city","qingdao",Field.Store.YES));
& g7 {" v" Z. b* o" w- J$ \1 R9 D1 f doc.add(new TextField("desc", "dsss is a city.", Field.Store.NO));
% F5 i6 y: s! S& h) D8 ?+ ~ writer.updateDocument(new Term("id","1"), doc);
1 B5 c `& A( i) ~ writer.close();3 z. ~% B3 L& \5 V
}
9 U/ G0 |# u& J) S}/ j& `; p: o0 P" ?- b- H
2 F8 W- a( @4 y" x% E r
! y3 m( O7 q( R" V/ r3 k3 ^
& D, u. x2 J f |
|