|
1、源码, w$ Z# e3 E. W5 Z1 F* j
import java.nio.file.Paths;
; ?7 p& E( f+ f3 V; R( L
% s$ u/ k/ O5 J. g$ z1 }# e( Oimport org.apache.lucene.analysis.Analyzer;( B& @" H. t0 a; N* I
import org.apache.lucene.analysis.standard.StandardAnalyzer;
1 C, M, j% k; i) S2 T7 x7 g8 bimport org.apache.lucene.document.Document;
( I! I% N W- y6 @import org.apache.lucene.document.Field;
3 z& \. Z7 j3 y% Yimport org.apache.lucene.document.StringField;1 ^8 f: r. o/ c3 Y$ @; a& x
import org.apache.lucene.document.TextField;
. y) u8 b0 t& ]- bimport org.apache.lucene.index.DirectoryReader;/ j2 j: x* ~7 V0 U1 H' n. `% u
import org.apache.lucene.index.IndexReader;
( {, i! p) y/ Z3 K) R' Zimport org.apache.lucene.index.IndexWriter;
; w& w1 \; {4 ?1 [$ v4 uimport org.apache.lucene.index.IndexWriterConfig;
* L# k J' l- ?. t. w. a9 Bimport org.apache.lucene.index.Term;, A% q! Y, b: P6 ?+ |9 `
import org.apache.lucene.search.IndexSearcher;0 M3 l3 v% S4 Q6 A' M6 E+ s
import org.apache.lucene.search.Query;
( e, U7 u- c; G2 b1 o) c' Iimport org.apache.lucene.search.ScoreDoc;
" M8 _$ H% i$ p) Q! @" }4 {import org.apache.lucene.search.TermQuery;. F& _* E9 z% Y0 s
import org.apache.lucene.search.TopDocs;0 p2 J% x7 A# r \6 m- M
import org.apache.lucene.store.Directory;: a( i5 Q7 r4 F
import org.apache.lucene.store.FSDirectory;
. [7 S K: U7 z0 @1 _8 B- limport org.junit.Test;2 h! U4 Y, y& Q0 m8 s8 v# ~, \
- m" s- p0 K+ xpublic class IndexingTest2 {% M. Y2 f$ M- r# |
: _6 k" \, C2 K private String ids[]={"1","2","3","4"};
( W( K2 V7 |$ J z: E2 `0 U! O private String authors[]={"Jack","Marry","John","Json"};$ U1 Y' J0 z, u- L& n
private String positions[]={"accounting","technician","salesperson","boss"};
5 ^- X0 I* p# [% L9 V private String titles[]={"Java is a good language.","Java is a cross platform language","Java powerful","You should learn java"};+ V d( T! O9 v
private String contents[]={
l1 c5 H4 a% _: ~0 e "If possible, use the same JRE major version at both index and search time.",
; l+ W( L; H0 I4 e; p# B "When upgrading to a different JRE major version, consider re-indexing. ",& T* T! ~7 F9 S+ k+ _
"Different JRE major versions may implement different versions of Unicode,",. V' f4 M0 }3 D' s
"For example: with Java 1.4, `LetterTokenizer` will split around the character U+02C6,"' X) E! F& H! p+ \( _
};) Y9 Z w6 E2 u- t: C" S9 q$ n) J# R
; @1 `* |& ]6 C
private Directory dir;
) D" p5 A8 A2 T0 I. R# m- l
! Y! W/ }1 ^6 c; Z4 k1 Y /**
5 r8 D% @) k7 R2 _* G7 l: T * 获取IndexWriter实例7 X" U2 W+ |7 w3 W0 s
* @return
& M+ J; m/ {' O& ^" c * @throws Exception
# P1 b6 x9 \1 k8 s, s' o9 m( U* X */2 a2 l. r- s$ U2 a' t
private IndexWriter getWriter()throws Exception{
0 W) y! d3 V/ E4 O A9 d$ g Analyzer analyzer=new StandardAnalyzer(); // 标准分词器
G) z! K3 E w! W IndexWriterConfig iwc=new IndexWriterConfig(analyzer);, z' u) y8 P& x% }2 y6 P
IndexWriter writer=new IndexWriter(dir, iwc);" w) u- D' ^& \: E2 i
return writer;% b( }; P% E9 h/ z) T
}0 v) f% Y0 A: a/ d A! x X) m
$ z. F$ Q2 M- B0 z9 V /**6 K9 ^5 A: d3 z, j1 \
* 生成索引
4 K! W9 g2 v# d) H* F/ _2 `* H * @throws Exception
+ p/ [9 |: ]& e+ ~8 e5 F */- x; v, |2 ~( Z4 ^' F" q5 t
@Test
9 h5 `( }) z2 Z( j, \ public void index()throws Exception{/ A6 H6 T# {% O! A# S* u" G
dir=FSDirectory.open(Paths.get("D:\\lucene3"));6 B' t. t, T3 }) i
IndexWriter writer=getWriter();0 ]( X# Y8 U+ i6 p4 `$ d
for(int i=0;i<ids.length;i++){* x0 B! b& i5 ^3 Z: p) j: e
Document doc=new Document();
9 H% n3 o& b5 n/ U3 ]: m doc.add(new StringField("id", ids, Field.Store.YES));4 n# [* p5 a3 K! w
doc.add(new StringField("author",authors,Field.Store.YES));
( z9 c4 i b- m9 I j! j: _% k doc.add(new StringField("position",positions,Field.Store.YES));
* b1 a1 |* j5 e4 J' X // 加权操作. a2 y& p0 u9 j; c
TextField field=new TextField("title", titles, Field.Store.YES);1 I: v" h$ a- _0 c- M% }7 }- v
if("boss".equals(positions)){4 Z0 x& o6 \9 B+ C; t, ^
field.setBoost(1.5f);( q" d7 ]" H$ X" D4 ?. R6 O
}
\7 m8 F( ^, E. v$ m4 R. m% g/ b doc.add(field);
% B1 F# I; C: s, D) z4 q' i doc.add(new TextField("content", contents, Field.Store.NO));
6 ], T7 n2 v* Q5 @# L: X writer.addDocument(doc); // 添加文档8 p" x Z d. q, M+ v
}* @+ B# K: f3 j: R8 e
writer.close();
* m" m6 c3 Q% W6 a }
' k0 b1 v4 H. ^7 l" U" u1 I0 }% n7 c3 w0 R
/**
, V! t6 N/ W7 D# a * 查询5 Z! w) `4 Z* [6 n
* @throws Exception
" u% q0 w0 a% ^- f0 u) \4 |; M */- S0 D. a) ?4 {9 |( z
@Test
4 `; j T$ ]7 M4 D public void search()throws Exception{
+ x; [4 s* u* }$ l dir=FSDirectory.open(Paths.get("D:\\lucene3"));
3 L: n7 N& y$ k* r# s! i# ^ IndexReader reader=DirectoryReader.open(dir);; q8 f2 ], c7 v R# R* b
IndexSearcher is=new IndexSearcher(reader);( s2 ~3 n; d- L( M8 K
String searchField="title";
+ a' k5 \& P9 H String q="java";, R5 W% x7 L. d. f& ^% L
Term t=new Term(searchField,q);
: K+ b* d5 B6 H8 ]" A' Q! h Query query=new TermQuery(t);: H* T k5 g" g0 Q4 i) a* Z, G+ X
TopDocs hits=is.search(query, 10);; C( A; N4 \; Q ~
System.out.println("匹配 '"+q+"',总共查询到"+hits.totalHits+"个文档");
6 D0 {9 L; S1 G3 T for(ScoreDoc scoreDoc:hits.scoreDocs){
4 r9 z; x1 K5 m2 A; ?7 L Document doc=is.doc(scoreDoc.doc);
) T1 c1 I6 k; e" f0 d, Y$ V* o System.out.println(doc.get("author"));. O; J$ Y& B' J- [* q' Y
}
8 ^% u9 t9 @8 O) S4 w& x! C; O0 P reader.close();
( J7 G; A1 h2 @0 ~' o }
" ~8 A5 H4 \. t) l7 k& M1 d9 J* w$ ]
}$ k. O I* w, ], h7 z" Q7 V* B& s
1 b h% c& b5 a4 B" o( I9 s: }! D
8 i1 y. H0 A# J
! W4 a. L# B" X: j0 i
|
|