|
1、源码
, z4 _9 D3 b5 N2 Nimport java.nio.file.Paths;& _ m9 l) d5 n' ~1 P' f
1 V0 N* |/ ^% U
import org.apache.lucene.analysis.Analyzer;8 O# r" a. K0 R# s
import org.apache.lucene.analysis.standard.StandardAnalyzer; a! ~7 Q- T+ u1 h& [
import org.apache.lucene.document.Document;) |7 {2 r& o7 _. S
import org.apache.lucene.document.Field;- V' z( q5 ^8 j4 \5 w
import org.apache.lucene.document.StringField;7 |0 M O& |$ ]& r+ w
import org.apache.lucene.document.TextField;
% ~: ]8 k- P+ i8 H/ o+ r5 D5 \; b* Y3 Gimport org.apache.lucene.index.DirectoryReader;
8 S; k% G P0 aimport org.apache.lucene.index.IndexReader;: @( X/ K4 Y! |3 s3 w
import org.apache.lucene.index.IndexWriter;) A* X+ \0 R& s7 x3 x3 ^7 x& j
import org.apache.lucene.index.IndexWriterConfig;
b5 O. y+ d# [( kimport org.apache.lucene.index.Term;" q9 _, q6 o9 j' a& y. B7 S
import org.apache.lucene.store.Directory;
, K) p& p3 W3 `+ c' simport org.apache.lucene.store.FSDirectory;
9 ^$ k5 U4 k5 Yimport org.junit.Before;
5 g0 c2 \+ K7 simport org.junit.Test;
* t f7 q" i2 F
% F a$ }1 O! A; p2 }public class IndexingTest {
8 N. E* G! u5 }" ?7 P
$ j) e* |3 G; s! u private String ids[]={"1","2","3"};
9 W0 W% y$ ]$ q. u private String citys[]={"qingdao","nanjing","shanghai"};
3 D( v7 X" k- ~" V4 o private String descs[]={$ m% q9 Z1 ]* Q$ k5 H
"Qingdao is a beautiful city.",
5 G4 E/ i3 {- s7 t1 b- F% l "Nanjing is a city of culture.",5 g7 j" k0 `$ r' f* h, B
"Shanghai is a bustling city."; h9 F( F M% u# h# N* U# H& I
};
: C0 ?$ Q( W; @9 m' t4 x% D4 E+ E, k" z! F
private Directory dir;7 H+ T7 x& e# e' @$ n) P
' D; [+ D1 q$ X5 {1 U
@Before! A4 \5 D0 }- j6 m* b
public void setUp() throws Exception {
/ H8 y7 i) H7 _* j+ y dir=FSDirectory.open(Paths.get("D:\\lucene2"));
' Q. ?# [; i9 d6 y& |4 K5 M IndexWriter writer=getWriter();8 w) |# q8 C+ D6 \) D
for(int i=0;i<ids.length;i++){) }* {& ], l5 [3 f: Y$ @
Document doc=new Document();
6 t! T P" B. D1 L0 U2 _( u7 v doc.add(new StringField("id", ids, Field.Store.YES));
# X3 b1 O) u& \0 R/ v0 j$ x doc.add(new StringField("city",citys,Field.Store.YES));/ P! J5 h# i! x& V
doc.add(new TextField("desc", descs, Field.Store.NO));8 C) X6 K; ~! E7 L
writer.addDocument(doc); // 添加文档4 u0 x" l$ k/ x. p
}
1 b0 U* E( W9 t, N% H9 m+ f1 n writer.close();
8 z! ~6 v, {* z' {# e }7 r# R* F$ [8 w4 U4 t
) [; B8 x, {; r /**
6 _2 P/ h/ r1 X+ w! i; R * 获取IndexWriter实例1 u' ]6 n3 p+ k; d5 F2 i( V9 { m
* @return, a9 T: I5 _- U" E' G- d1 ~
* @throws Exception
) _8 G& L, j4 o */# a, V4 R$ x/ R9 K
private IndexWriter getWriter()throws Exception{
& W: d7 }2 e* M. k4 X* | Analyzer analyzer=new StandardAnalyzer(); // 标准分词器" O6 V( Y2 L7 ^5 i+ ]: I
IndexWriterConfig iwc=new IndexWriterConfig(analyzer);
3 K* i" ~9 D, i/ X& ~2 V: [ IndexWriter writer=new IndexWriter(dir, iwc);
' a; J( a ?' N: [2 @0 ^; \, X4 X return writer;% B- p9 \ S2 d3 F# ^5 h
}
' ^7 x7 }" J; v* W; n: z' o4 }' S' J2 p
/**
1 e7 K# S4 c+ z9 W! ]) L U4 c9 v) J * 测试写了几个文档5 T8 w/ V5 ?, j; t" c2 _; }. R: a' G
* @throws Exception! d3 X7 b0 L; ~2 w$ A
*/
* D ^. x$ b# `* |/ w: a @Test
$ k8 ^( \) J% v* E( V public void testIndexWriter()throws Exception{; a6 k0 E" E. R' B4 j% X X$ A0 C
IndexWriter writer=getWriter();
- S- s9 i3 m' l. s: Z5 F+ W System.out.println("写入了"+writer.numDocs()+"个文档");6 q! j. p/ J) {$ `3 Y6 H
writer.close();
% O' @6 I" m" h% ]# d. m9 } }
; v% O+ O5 V2 x4 J+ {
( R& \$ k$ `! s' N }3 H /**) X/ V8 I" G, u$ d# z
* 测试读取文档0 v6 r- Q5 X) a3 H, ]0 z
* @throws Exception) S$ {; T8 b4 |/ D6 n; H6 c. C+ ?
*/( R/ H: B; M: W) ^' q+ h0 d
@Test9 x+ a5 _7 }1 y! j/ e/ x/ k
public void testIndexReader()throws Exception{
" r* I% I# s/ Z) p/ m IndexReader reader=DirectoryReader.open(dir);0 v8 a, a( c8 @7 Y; D
System.out.println("最大文档数:"+reader.maxDoc());% s! D' b3 l# E0 [
System.out.println("实际文档数:"+reader.numDocs());
4 U( ]+ U6 s5 J9 K& @8 b/ M \, X reader.close();; c" N+ q( {6 N8 Y) c5 E/ x
}, Q) V7 W1 K, q8 G( Y0 |2 s8 E
& u/ S" [/ u' S4 O* K
/**
% s" H6 \! J& b& p' Z * 测试删除 在合并前' i+ F& j( o) x! H& \ W4 R' f" @- F9 j
* @throws Exception
5 c7 y7 F, ]6 z */( z: Z/ V& a% ]. G1 a; g
@Test1 d5 o" V+ D. o0 b* c
public void testDeleteBeforeMerge()throws Exception{
8 z: Q# H' U& k+ X% ^3 U7 b% V IndexWriter writer=getWriter();
O6 X& i0 p- ^1 J$ M System.out.println("删除前:"+writer.numDocs());
2 `( {6 U# v# ? |3 G8 j writer.deleteDocuments(new Term("id","1"));
6 y9 M/ K- N3 v+ N, a) b writer.commit();
' l$ K, ~0 j" G5 l$ _0 w& T9 _ System.out.println("writer.maxDoc():"+writer.maxDoc());: T! N' h0 }8 N
System.out.println("writer.numDocs():"+writer.numDocs());9 f% D. Q5 E$ v N( I0 t
writer.close();* R2 G4 ^2 g9 S# M8 m6 d: y$ o
}9 } K- ?4 Q, @+ W8 U" g" X
* p: |) }, y0 Z /**: o1 [# |8 o) M, m
* 测试删除 在合并后" L9 m$ O+ {) Y8 w! ~
* @throws Exception
/ _: \* ]. {: h1 M) ^. G1 M */# O! l, Q$ \; c7 S5 e
@Test0 N3 k; c Z" \" W2 A |4 }
public void testDeleteAfterMerge()throws Exception{
+ Q1 P5 T3 A' i IndexWriter writer=getWriter();
: `; x4 \; u; d% C" f& v. G System.out.println("删除前:"+writer.numDocs());
Z# D5 r7 b; h writer.deleteDocuments(new Term("id","1"));
6 B4 G3 U; v0 p/ O# k writer.forceMergeDeletes(); // 强制删除
5 x- D7 l6 M* g writer.commit();
% g* S, c. z) ?- c& A2 w, q System.out.println("writer.maxDoc():"+writer.maxDoc());
& R. `2 b# C+ H" i$ F3 y/ q System.out.println("writer.numDocs():"+writer.numDocs());
1 L, J& O$ k. W writer.close();; U/ V2 i0 C4 C$ {' r% Z
}
" k% j7 N$ V5 A" z+ G2 f* C) j2 n; E5 G2 x! I3 o" h5 R
/**
3 Q. S7 A( l; b0 k1 O+ B * 测试更新
6 z; K9 V% G9 c0 C, u! I * @throws Exception# p H3 u% U2 ^3 }6 K& L3 M
*/
0 J/ O& c" Y, Q+ y1 P; S( K @Test) p- l/ |/ K1 D) ~1 e9 n F
public void testUpdate()throws Exception{
2 n+ |; J ?6 X3 f2 b! k IndexWriter writer=getWriter(); O7 k; I9 c: n( L9 L3 \
Document doc=new Document();0 G- f ~, k2 n& I
doc.add(new StringField("id", "1", Field.Store.YES));
5 O/ T0 |' y& n, e G( X+ |6 [: u doc.add(new StringField("city","qingdao",Field.Store.YES));) {" n: q' s% B' f1 e- P4 |; C1 X/ o
doc.add(new TextField("desc", "dsss is a city.", Field.Store.NO));0 x7 ?: w% j" H9 O1 @6 Z
writer.updateDocument(new Term("id","1"), doc);
+ v+ N l/ W0 O, g3 i- _" x8 F% J writer.close();0 W5 W5 `4 } c$ V) W
}
b+ m+ d* o- `2 @}
; A5 o' \8 Z$ x
/ Z9 H3 t! a# S& ]; w, f) f8 I9 D! V4 [4 [
) i, G. d+ h3 I+ h
|
|