|
1、源码6 o2 m% b* b# b
import java.nio.file.Paths;
7 ~( {$ r. ~" @5 d5 y* j, w: Y- f: Q$ u
import org.apache.lucene.analysis.Analyzer;
: U9 i! K. ^1 U# [( M) u; Ximport org.apache.lucene.analysis.standard.StandardAnalyzer;
T$ [3 m- g5 c) _3 d% X% d3 M2 }: ?+ \import org.apache.lucene.document.Document;
" I$ J' Z- w$ v3 {4 _import org.apache.lucene.document.Field;( M, ]* s6 \- d2 x: q# z# p0 L5 J
import org.apache.lucene.document.StringField;
* t" T9 ^4 q, c: H2 h9 t1 oimport org.apache.lucene.document.TextField;
% Y3 d. Q7 e6 yimport org.apache.lucene.index.DirectoryReader;
8 J, `' w' [* x( _! m1 X% zimport org.apache.lucene.index.IndexReader;1 i+ X E& I; ]; o. ~
import org.apache.lucene.index.IndexWriter;: l5 a$ p' {8 d$ V
import org.apache.lucene.index.IndexWriterConfig;
1 `, y+ [, `! R% M Mimport org.apache.lucene.index.Term;4 o+ n5 N2 ?) ~
import org.apache.lucene.store.Directory;0 P, V9 ^2 r* U+ y
import org.apache.lucene.store.FSDirectory;
$ x/ c6 ~9 m( F0 P% O' oimport org.junit.Before;
8 r. o3 O9 _0 ?import org.junit.Test;
, a2 a0 m; d) ^, B6 ~
' g- ^# q! K% M" }# g/ npublic class IndexingTest {8 g4 S0 G+ f& ~+ A' H
Y6 R3 a5 T$ S private String ids[]={"1","2","3"}; r ^$ T1 `4 V
private String citys[]={"qingdao","nanjing","shanghai"};8 B7 ~! T+ o: M$ U8 c$ R- m
private String descs[]={
/ U# g: U3 B- }7 o) \ "Qingdao is a beautiful city.",
- {- D- r, ^ l: x% [7 l/ i "Nanjing is a city of culture.",* D. _3 i2 Z r) I/ N% |
"Shanghai is a bustling city."
1 O- N1 [) [: ]0 a };
, h0 L/ Z6 A3 V4 }. Z$ ]! J$ _, l2 q4 d% Y A0 H% h9 D, m
private Directory dir;
# _7 @0 {. Z; x4 |. g4 a3 s+ P( k t3 O7 {+ b, L+ i0 h
@Before# K6 d+ h$ w4 n" n" e' a
public void setUp() throws Exception { c! ^+ e d/ z7 E! r7 [* K2 S
dir=FSDirectory.open(Paths.get("D:\\lucene2"));
8 C; A) Z7 f$ Y IndexWriter writer=getWriter();
& f$ @9 v/ `5 Z$ B for(int i=0;i<ids.length;i++){
4 l) L" e# F ]; n( Z Document doc=new Document();
" s6 \$ F* [3 P- X8 T {$ o" ~+ R doc.add(new StringField("id", ids, Field.Store.YES));/ [ Z7 M. y: x9 k* M/ [% Y
doc.add(new StringField("city",citys,Field.Store.YES));
7 f$ ?9 @- ]: h% a0 T doc.add(new TextField("desc", descs, Field.Store.NO));
3 B+ J* X) c; u5 `+ q6 [ writer.addDocument(doc); // 添加文档: q/ ]" @ I' n* t5 M1 h( P) C
}( A/ ^; @5 h& p/ O0 Q
writer.close();) A# }! p k' s& s/ _
}
K+ e; G3 z6 O0 g5 D( i4 ]0 ^4 F3 o- K* z2 X/ u8 L* a
/**
( \, r+ V& t( d$ A% F( }! Q * 获取IndexWriter实例
1 @9 |& M3 x: p6 y! u {8 ^ * @return6 W; U Z- s* Y, r" o4 m
* @throws Exception
5 ]. H. i1 g! `! R) c7 w$ m. t& k! Q9 ^) L */
+ ]1 `) A- G+ e ^$ l8 @* R private IndexWriter getWriter()throws Exception{, N5 S q% {9 ?0 P9 m' n! C2 t
Analyzer analyzer=new StandardAnalyzer(); // 标准分词器
4 a9 ^8 T8 ^$ r; I$ [. P3 V6 z1 y IndexWriterConfig iwc=new IndexWriterConfig(analyzer);! F: y, h7 E* i# T4 ^0 q* S
IndexWriter writer=new IndexWriter(dir, iwc);
8 q" f3 G, w6 P. a/ H% r! S8 m7 R' ? return writer;5 X: k% s( {! S7 G" J5 |+ D
}4 F% x5 z) t! r& N% W, x
5 ^8 ^7 u: \- \0 I6 D /**) a( z; s$ Y( A& g9 z
* 测试写了几个文档
9 F! a: \% a/ l/ l# Z2 `7 V$ @ * @throws Exception+ f( d$ P% M# ^+ a: L* D
*/* U9 z* ?- ~, m- N h! M
@Test" T5 R0 ^. s* M$ L- C- t; t( d
public void testIndexWriter()throws Exception{
1 S/ l: A* g! f w; i: C$ s IndexWriter writer=getWriter();
3 H9 B4 t$ j8 ]8 Y System.out.println("写入了"+writer.numDocs()+"个文档");1 Y# m7 P, P: s, [0 G0 S3 `
writer.close(); ]* {1 E" O% C& z7 ^# [
}5 }. m8 |0 }) V3 c9 U" L S4 O; s
7 W6 O9 K/ e4 {5 p. x /**8 \1 M9 R) }; }3 }: V
* 测试读取文档
' I# G' L. F) m- y * @throws Exception
. G$ e/ ~6 u% L7 `9 I; a */
& J" n7 O+ c4 w$ } @Test! D) l! W7 {* t; A- V
public void testIndexReader()throws Exception{
6 e) p2 l' F/ b IndexReader reader=DirectoryReader.open(dir);1 K7 _0 J; S% w: F% s5 ^4 {4 D
System.out.println("最大文档数:"+reader.maxDoc());
) ^/ F; J) v S, M System.out.println("实际文档数:"+reader.numDocs());
8 z8 {4 P# E( W! ?6 Z, I4 T reader.close();* k) ~9 U U8 @ N
}1 D- B# A3 S6 [4 m3 H$ j- H
) s* K* a8 p& t+ ^7 n& H /**
* i" ~! q+ E7 u' C: X, u3 _ * 测试删除 在合并前7 c- \$ ~4 A7 C2 K4 k
* @throws Exception
/ `5 E) j2 m: F */4 W/ R' a) p; k1 l
@Test
$ _. N! @0 t3 N1 s public void testDeleteBeforeMerge()throws Exception{# C k8 ?! c$ P! I5 c1 S, J: O# [
IndexWriter writer=getWriter();( x; e) j+ Y3 P
System.out.println("删除前:"+writer.numDocs());
1 r# o5 @0 w+ g( m writer.deleteDocuments(new Term("id","1"));
. g7 t) {: P8 x7 d! T P L4 f writer.commit();
& M0 ?" l8 q. {2 U6 n System.out.println("writer.maxDoc():"+writer.maxDoc());0 ^/ z H$ v: q1 ?
System.out.println("writer.numDocs():"+writer.numDocs());6 K8 {/ N* R8 O
writer.close();; ]% C/ S2 C3 Q2 W
}
0 O4 R6 V: [$ V
7 H( d* \& R9 J( ]+ l2 a- R1 i /**, M9 |3 h) C+ k7 a0 W& U
* 测试删除 在合并后# T* {$ f7 q7 O E) r
* @throws Exception
+ ?# y4 ]3 N# f4 f */+ A" g0 C9 o4 U8 ?2 X& }6 f
@Test
0 f* ^8 f% E- _" ?6 G public void testDeleteAfterMerge()throws Exception{
2 B* O% W& ~. j+ k; l9 R IndexWriter writer=getWriter();7 A2 S6 ]4 `, F I" y/ C$ N
System.out.println("删除前:"+writer.numDocs());# `: N5 ]3 Z; i, W4 \# d: l
writer.deleteDocuments(new Term("id","1"));
1 [0 D1 Y/ D) y, T writer.forceMergeDeletes(); // 强制删除9 r+ u# Q% \. a+ F. ` }% u
writer.commit();1 @5 L2 j4 y# R, B. {/ P
System.out.println("writer.maxDoc():"+writer.maxDoc());0 e2 |5 l% f' K# a j3 z! d9 ?
System.out.println("writer.numDocs():"+writer.numDocs());
2 H/ d! W( L- @+ T# ^ writer.close();
. B: }( C9 R5 k; t }
; T# ]' c3 F7 m- ~ z
! c$ b& u& Q8 a6 O /**5 x+ z4 {* D9 s
* 测试更新/ k! L! I1 L- O* }9 ~
* @throws Exception* x! h8 ?8 ]7 o" C" }3 j' G: H3 u
*/
: U0 E9 q7 }$ Q! C j1 I( M* w @Test' {# ], E! X) u
public void testUpdate()throws Exception{1 ]: ?& ]3 L; S- C+ Z
IndexWriter writer=getWriter();, z& [* n5 F* m) X& G8 ^
Document doc=new Document();
/ g5 s: q9 ~9 N7 k$ I doc.add(new StringField("id", "1", Field.Store.YES));. f, G. z9 ^! _7 n
doc.add(new StringField("city","qingdao",Field.Store.YES));
4 R3 A8 P& M+ t# o8 ~7 f doc.add(new TextField("desc", "dsss is a city.", Field.Store.NO));7 u, [& ]+ O9 S0 a) s; O8 V
writer.updateDocument(new Term("id","1"), doc);
, r0 n( X* U; p$ N writer.close();) o8 `3 m: v+ Q# h
}9 T' o0 a9 X9 [4 h( l" s; J
}
5 l6 N% d1 q/ p4 v: V0 k
2 Q" O/ j+ |$ s5 h% c+ k2 f
: B6 f B! d! G( h1 D6 {1 l% [/ \+ F: r) F) w# x
|
|