|
1、源码
; a1 {, f6 G$ q; F: L+ K+ r' [import java.nio.file.Paths;
* b; @3 r+ a" w$ n, W$ O" _
" W* J" g4 K! G% b3 S" Vimport org.apache.lucene.analysis.Analyzer;
" M- o! a- l/ Z' }! h. v7 x6 A- G" ~import org.apache.lucene.analysis.standard.StandardAnalyzer;4 |& U; s! W$ H, p# J
import org.apache.lucene.document.Document;# d9 d1 Y' n8 O* T: W! i' J4 L) R
import org.apache.lucene.document.Field;
" g( ]) G2 {5 S; Y1 Gimport org.apache.lucene.document.StringField;* w3 \7 {, q- [6 T/ O0 ?
import org.apache.lucene.document.TextField;
& @+ u! U _ B1 Himport org.apache.lucene.index.DirectoryReader;
9 z1 n9 s3 e; y! J# T6 aimport org.apache.lucene.index.IndexReader;
0 a/ [# h$ L0 e) V9 Y8 Iimport org.apache.lucene.index.IndexWriter;
' i) k3 e* j9 D/ G) w( mimport org.apache.lucene.index.IndexWriterConfig;5 Y- ]- J+ s) l/ m- e. a3 ^
import org.apache.lucene.index.Term;" \* a, _& g- [- |. H6 B
import org.apache.lucene.search.IndexSearcher;
8 f6 X4 B2 {/ v: d4 i! a! L% Jimport org.apache.lucene.search.Query;5 T- k, V9 G3 K% @, V" o
import org.apache.lucene.search.ScoreDoc;- s' j3 s; K' P
import org.apache.lucene.search.TermQuery;0 G; A& \1 ?3 b2 _4 _
import org.apache.lucene.search.TopDocs;
) ^- x& T: f1 u4 Oimport org.apache.lucene.store.Directory;8 [/ l( _1 i3 Y1 `/ s3 @4 `4 \; V4 x
import org.apache.lucene.store.FSDirectory;# f! { j$ ?" o5 R
import org.junit.Test;! G$ E3 j1 O5 C7 }6 u: r
0 O6 t e: @, O1 zpublic class IndexingTest2 {
d! [5 D0 O9 M$ Z' z7 F }- P3 ?+ B+ r0 Y3 _
private String ids[]={"1","2","3","4"};6 r) o, a/ J- |
private String authors[]={"Jack","Marry","John","Json"};
3 `: n$ ~5 C9 m" H; C0 v private String positions[]={"accounting","technician","salesperson","boss"};
+ }; v1 B1 H$ P6 ^4 E- @ private String titles[]={"Java is a good language.","Java is a cross platform language","Java powerful","You should learn java"};) k) U! X' n2 v; X* s- G
private String contents[]={) P2 g4 F2 t7 d. V) G
"If possible, use the same JRE major version at both index and search time.",, @0 w- `8 H# S; S0 r* [6 @0 L& U
"When upgrading to a different JRE major version, consider re-indexing. ",1 `: u+ ?1 l% R, [
"Different JRE major versions may implement different versions of Unicode,",! ~* _& L; O7 m* }4 |: V. ?& v5 m
"For example: with Java 1.4, `LetterTokenizer` will split around the character U+02C6,"
/ l! \. N0 p% ]& R };/ x! m5 p3 h( T# c
' r8 b1 _# Q/ D n: {+ k
private Directory dir;
$ i; f s" ]3 F h! H- c, G5 J9 J, u5 q/ v& p c
/**& I, {9 w$ A; m$ N! c- u+ {
* 获取IndexWriter实例6 ?( g, Z0 a. ]' w: Y: x
* @return. f0 e4 r! Y+ I- v+ ^
* @throws Exception
- X# X) W3 s# X+ c% @4 p$ w* ]" A */
2 H# h# r0 z( T! x& S private IndexWriter getWriter()throws Exception{
& ^' l& f1 M3 s% [- t' D Analyzer analyzer=new StandardAnalyzer(); // 标准分词器# u( `! e( D6 F& D! x
IndexWriterConfig iwc=new IndexWriterConfig(analyzer);6 c: h0 Q" |' m- Z9 n2 C
IndexWriter writer=new IndexWriter(dir, iwc);+ x' E7 ~/ |3 H
return writer;4 T! Z6 d9 z7 C0 r2 K$ ]2 U9 V( U
}
9 M' [. h9 ]! n- l v( h. w2 Q4 ^ {1 L B: J
/**
: y3 z( w' X$ I @1 I * 生成索引7 w' D- s2 j) q( ~
* @throws Exception
% ~& h/ P: c' Z$ E */. t. _9 {& |) z4 q: C9 ?! F
@Test
3 x8 U% T! k1 p public void index()throws Exception{5 f! u5 r4 c. D3 G8 P0 D: T
dir=FSDirectory.open(Paths.get("D:\\lucene3"));. c' [/ I9 D* K, ^) T7 n8 S: x* j) a
IndexWriter writer=getWriter();
( p' M. ]% L' Q2 L% V* J6 d for(int i=0;i<ids.length;i++){
$ L8 W |. L; v: }% _& O' t$ o Document doc=new Document();
9 L/ \$ v2 B! D/ M' \+ F doc.add(new StringField("id", ids, Field.Store.YES));) [2 m! w5 r0 w2 z/ g
doc.add(new StringField("author",authors,Field.Store.YES));( x- y [# ^" B* D) V) {: W
doc.add(new StringField("position",positions,Field.Store.YES));6 J" T- c1 U4 F* C- I8 P$ Z# R
// 加权操作; p% o+ ~% H7 B# Z
TextField field=new TextField("title", titles, Field.Store.YES);% y; u5 L( w+ s3 d1 u& Z l9 m7 w2 @
if("boss".equals(positions)){7 W: b- B5 b( _8 X3 B
field.setBoost(1.5f);
1 L) j! u2 z7 Z. s' X& f }3 I/ ?9 e" y/ b# O& w0 |1 N
doc.add(field);
$ Z6 b4 t, h$ A doc.add(new TextField("content", contents, Field.Store.NO));
8 x- z( B2 q+ }# z6 S- V writer.addDocument(doc); // 添加文档7 o) Y0 J* Y+ F& L% M; i( d
}
# I# g9 P5 }3 C' }% C writer.close();2 a2 l9 A3 w# }8 a8 _/ Y5 I6 h3 d
}
7 ?4 ?! E# a+ t% u! F# m3 j6 b" C @0 O! i, k3 w
/**. H) q6 |& _" ~* _( w
* 查询
, } c: Z6 G) T% t7 X4 o3 l * @throws Exception: E3 s' I0 L# K
*/6 K- S. I% ^% F5 f# M
@Test8 k& B1 e; n k/ l0 j* R
public void search()throws Exception{
! R+ C) w0 b `" i2 o4 g" x dir=FSDirectory.open(Paths.get("D:\\lucene3"));
2 |4 k) q1 l$ j5 B IndexReader reader=DirectoryReader.open(dir);
3 ~. w$ F! R( l IndexSearcher is=new IndexSearcher(reader);4 n& u7 l; G' o1 X1 w( e+ k
String searchField="title"; G! M- o) A. o* n% V+ r
String q="java";
P2 c& N/ v2 [6 V" G; u% Z Term t=new Term(searchField,q);
" g4 ^/ v( U0 Z; V9 y. q$ {' V6 Q Query query=new TermQuery(t);. \" _0 f7 R/ J. R7 ^- o1 j* u
TopDocs hits=is.search(query, 10);
; R9 y' v, H; v) } System.out.println("匹配 '"+q+"',总共查询到"+hits.totalHits+"个文档");1 U) q; q& y0 X# t" M% l9 p+ n8 _
for(ScoreDoc scoreDoc:hits.scoreDocs){9 ~2 J- a" b/ B, N
Document doc=is.doc(scoreDoc.doc);
) p! K9 x3 L0 ~" V System.out.println(doc.get("author"));
0 H" d9 u C, ~$ k9 ] }0 t2 I7 s, Y9 q5 [
reader.close();
. X! |7 ^. I5 P5 T& B4 x }6 M0 ^5 F7 M' t" e
/ }* V4 ~' w' L& U) A}( `1 ]" z$ z. G" ?9 _/ z
# V7 L$ r3 h' C( m* k, t# a
; d$ U3 C2 I2 b* o
. d% a- s/ l8 L) U g |
|