|
1、源码
$ j8 ?2 j( S) |) P/ G2 x' oimport java.nio.file.Paths;
. I1 N2 j, y, {! |# q& x
( `! {8 \9 j( K- fimport org.apache.lucene.analysis.Analyzer;% A1 H' ?! D" P! ~* y9 k
import org.apache.lucene.analysis.standard.StandardAnalyzer;! `9 \( Z( C. v$ B
import org.apache.lucene.document.Document;
: R7 W* y% ^; Z+ n! F& w" p' S3 Gimport org.apache.lucene.document.Field;
5 u0 J& ~5 S, R7 y& S) B2 R) aimport org.apache.lucene.document.StringField;
" y$ U. q' X4 @3 l# Himport org.apache.lucene.document.TextField;2 g+ Y+ I- `! a" Y% e* v
import org.apache.lucene.index.DirectoryReader;
3 u6 C3 |* n$ c% rimport org.apache.lucene.index.IndexReader;
- z% L6 o! U" c& Cimport org.apache.lucene.index.IndexWriter;3 \. t& [$ q1 f. R' O# V
import org.apache.lucene.index.IndexWriterConfig;) E' w1 _2 Q! P) {
import org.apache.lucene.index.Term;8 C* a6 v& C8 i% p
import org.apache.lucene.search.IndexSearcher;
4 I" D L1 f" rimport org.apache.lucene.search.Query;
. c0 q) j! _: o7 r* x1 _import org.apache.lucene.search.ScoreDoc;
' Q- r' \# d1 |% w# g8 Simport org.apache.lucene.search.TermQuery;
4 q7 F7 u1 b+ G( |import org.apache.lucene.search.TopDocs;
# L, {# ` Q e pimport org.apache.lucene.store.Directory;; [, |7 f- L& S: J4 a* }
import org.apache.lucene.store.FSDirectory;
" [3 L7 x# n. W9 P, Himport org.junit.Test;
/ V- e9 _9 F; t' B l* n- X2 F! Y4 t
public class IndexingTest2 {
4 s5 K& m# @. L# @6 L1 ?" ]4 f K* C& j- Z6 X0 L/ L
private String ids[]={"1","2","3","4"};
% \% f% T, L0 K! Z3 r: P' I9 h private String authors[]={"Jack","Marry","John","Json"};
' Y& a1 K7 J: ^4 @7 N) O( z* R private String positions[]={"accounting","technician","salesperson","boss"};, ?- j7 O8 w! S4 D( U& P. {
private String titles[]={"Java is a good language.","Java is a cross platform language","Java powerful","You should learn java"};
0 ]! J0 ?5 u2 W9 F" c8 F0 } private String contents[]={4 V- L/ ]0 |1 R, D3 [8 n: |/ Y$ i# p
"If possible, use the same JRE major version at both index and search time.",2 C _' s0 u! p2 y6 [* Y: N. b
"When upgrading to a different JRE major version, consider re-indexing. ",
& @: F2 B+ S1 ~( t' q, }/ g$ z "Different JRE major versions may implement different versions of Unicode,",
) u ^# n" u h- o "For example: with Java 1.4, `LetterTokenizer` will split around the character U+02C6,"
; K& @/ c& F; Q* p+ _ };- ?/ }8 H/ [9 G1 D# e4 J
$ q/ [2 ]( v3 S: e, c+ s private Directory dir;! T6 ?5 V8 A+ H8 P, O& y! G
( E, d/ d# o0 M5 a, Q. ^ /**
7 x2 w/ D" S; S/ u/ @ * 获取IndexWriter实例! [& d6 ~8 p, W6 E6 I+ y: A
* @return+ ?' H s3 s3 V! @8 D4 p/ i6 s! {
* @throws Exception! V! v- G3 Y+ }- ^! j
*/
& u1 t; f2 k$ o' X private IndexWriter getWriter()throws Exception{
. `9 `) r8 M% K8 @# Y- ? Analyzer analyzer=new StandardAnalyzer(); // 标准分词器& T0 d1 m# g3 F- @: @5 C
IndexWriterConfig iwc=new IndexWriterConfig(analyzer); b1 s/ \+ e/ A/ f: H
IndexWriter writer=new IndexWriter(dir, iwc);
( ^5 M3 G9 c; y' A' w& @0 x return writer;
; j1 } d$ w/ W( L+ x1 g( h }( l8 g2 E3 j8 ]7 W/ `6 d
" }1 V: ]) O, I7 ^% W- z
/**8 O8 E/ i% q! X# l( J
* 生成索引
8 U4 x- z1 U9 C. {5 z( d4 i, n * @throws Exception
+ f+ E$ Z4 L9 T! F5 o */0 V' q' J& I' S9 B
@Test7 Y+ T9 L8 w& }1 ~" I, U
public void index()throws Exception{! r$ s: z$ |9 \9 x5 Q0 h
dir=FSDirectory.open(Paths.get("D:\\lucene3"));
7 L) F1 g8 @" t8 ^( h. D IndexWriter writer=getWriter();, x) A7 b) J3 K3 U0 C8 d/ P
for(int i=0;i<ids.length;i++){
/ _3 |3 g/ r5 u5 \+ h/ r Document doc=new Document();% b4 Q: ~+ `) ]' l
doc.add(new StringField("id", ids, Field.Store.YES));) ]8 h6 _$ P3 _ s
doc.add(new StringField("author",authors,Field.Store.YES));. @3 Z. g/ I. C* A
doc.add(new StringField("position",positions,Field.Store.YES));
5 B" k, B6 X. j# {; T! l // 加权操作
7 ?# B! U# a- L/ G( H3 h$ [ TextField field=new TextField("title", titles, Field.Store.YES); { x6 Z, a6 R4 p
if("boss".equals(positions)){
0 j L( H; y2 [, V* f field.setBoost(1.5f);
( z" J; M A9 G1 B/ |2 D }) ?: o) D ~6 Q5 l, v$ m( Y
doc.add(field);* V; V5 [) f5 C" X4 `6 @& Q
doc.add(new TextField("content", contents, Field.Store.NO)); j6 a) I. y/ N9 e6 a. U# b* h
writer.addDocument(doc); // 添加文档
6 ~; O! |" C2 Z6 Z, z' d0 l }
% i) G; w' C6 p writer.close();
7 H5 ?4 _& [- N4 h+ ?6 ` }
+ D) A2 U7 E7 b2 g
: q, O! z5 ~2 S& [; ?- B+ @ /**( z8 } d- `. N# |4 B
* 查询3 F0 e5 |0 W/ S
* @throws Exception8 j8 o! A- v2 B8 H
*/: i2 z7 M% K9 E, Y1 i
@Test, ]5 b! ? y4 f
public void search()throws Exception{
, o$ R" A* D! `( k. Z1 ~" p dir=FSDirectory.open(Paths.get("D:\\lucene3"));
: _5 }0 A7 R6 T8 Y& y IndexReader reader=DirectoryReader.open(dir);. l' w+ k/ J! O* I2 E! `) }+ Q
IndexSearcher is=new IndexSearcher(reader);
, }* @5 P; ?% ]- z String searchField="title";) F, f( l+ j2 g/ j
String q="java";
" D; e7 Y0 p" M Term t=new Term(searchField,q);
2 C' H: J) F2 ^6 n( [ Query query=new TermQuery(t);
* J9 C& A1 H; J; d9 g TopDocs hits=is.search(query, 10);
' R6 a# V4 U* b$ w- D: ]; y System.out.println("匹配 '"+q+"',总共查询到"+hits.totalHits+"个文档"); O( s4 P4 e& ~6 [* d
for(ScoreDoc scoreDoc:hits.scoreDocs){
, M0 S0 t) u. X/ R. ~ Document doc=is.doc(scoreDoc.doc);
% ?) T5 J8 e& m/ C; S* f, }1 J System.out.println(doc.get("author"));* `9 ~4 ]3 `3 G P
}
: c( C) D7 ^- G4 ~1 \0 i0 L' j reader.close();
/ M7 \ G" A% p& N9 W, { }
0 U& _: {6 Y; [; g' o+ J1 q: R+ R) w0 o [( b L" ~
}
: |( m. v0 |8 O$ X1 t1 o) z; a% t: { f6 x7 e! i2 i- o7 I
. X+ b* H- l0 V, ?4 @
$ |9 H7 c" j: D/ J+ H0 ]
|
|