|
1、源码) c, Y+ W; h. d/ _& C9 f' G' _
import java.nio.file.Paths; I' x2 c" `' s' \6 x9 T
+ O# h2 D! i* |7 |7 }' }7 nimport org.apache.lucene.analysis.Analyzer;
5 m! ]0 D& o+ x0 `7 E! _0 mimport org.apache.lucene.analysis.standard.StandardAnalyzer;
2 O# a( Z( [) l" B2 C% ximport org.apache.lucene.document.Document;
$ h5 T; |, q' ~# o1 Y j1 Bimport org.apache.lucene.document.Field;
& b0 h; C6 r% Fimport org.apache.lucene.document.StringField;! M% ?+ G" X* {& A) M0 k, d9 m
import org.apache.lucene.document.TextField;
9 i5 \0 U# g1 m- ]! }8 r2 Mimport org.apache.lucene.index.DirectoryReader;( Z7 \& c( k% Q/ `
import org.apache.lucene.index.IndexReader;
8 B9 n7 m) N: w$ Pimport org.apache.lucene.index.IndexWriter;
% A. O# N9 A$ kimport org.apache.lucene.index.IndexWriterConfig;
7 U. a2 L0 |6 B4 i5 w2 W cimport org.apache.lucene.index.Term;
# @1 s9 S0 K# W( s* zimport org.apache.lucene.search.IndexSearcher;
9 b+ }: S) W o/ u5 ]import org.apache.lucene.search.Query;
& d w: K& P4 @8 Q, k( {3 E" eimport org.apache.lucene.search.ScoreDoc;
9 k, ]0 l0 W X7 J1 l7 Q3 }# s4 Dimport org.apache.lucene.search.TermQuery;+ n4 L8 w' K9 |0 @' c+ \
import org.apache.lucene.search.TopDocs;
4 C+ f Z3 l7 h* Fimport org.apache.lucene.store.Directory;1 X: F7 u5 v8 q5 ]
import org.apache.lucene.store.FSDirectory;* K, f- G$ B: K1 w
import org.junit.Test;
4 H$ f9 `4 L/ |0 \ J2 y2 @+ g" f4 T" l% v
public class IndexingTest2 {; @; k0 h# b$ w* ^$ _
* T" U- h$ v0 G P
private String ids[]={"1","2","3","4"};2 ]5 v4 Q: s0 m# u4 ?
private String authors[]={"Jack","Marry","John","Json"};
" p2 s7 H# e1 e' Z; A4 z private String positions[]={"accounting","technician","salesperson","boss"};7 o, u3 t; ?* Z3 E% M: J! e: t
private String titles[]={"Java is a good language.","Java is a cross platform language","Java powerful","You should learn java"};
7 d( [0 i6 ~" e$ @0 Z7 T3 I7 } v private String contents[]={, _1 M$ H$ _0 U, B# U4 R; q% n
"If possible, use the same JRE major version at both index and search time.",
+ H1 B6 I Y3 ~8 c4 { "When upgrading to a different JRE major version, consider re-indexing. ",
$ U. e/ h! t6 z% X3 C, D! y "Different JRE major versions may implement different versions of Unicode,",4 T& ]# y; Q# ~
"For example: with Java 1.4, `LetterTokenizer` will split around the character U+02C6,"3 z" x. C, H2 u7 r
};
6 z( [/ F) w! T+ r& W6 l5 H- ~" f- _7 O, p% i( Q! T" r
private Directory dir;
- _) Q9 z2 C% N: I4 Y$ S. x# M' W+ l
/**
4 v6 [. d1 A& t, r * 获取IndexWriter实例
9 c" L8 [* Q3 D" O \2 r) O * @return
/ A/ F( D7 y- U" c * @throws Exception( z/ T; J, s6 P* @ g* c/ K2 G
*/
5 x( v6 I# x9 P1 g private IndexWriter getWriter()throws Exception{
7 W' n, y# U% K7 {5 A: ` Analyzer analyzer=new StandardAnalyzer(); // 标准分词器
|! e) p6 ~( U& X7 s" V% @# s; | IndexWriterConfig iwc=new IndexWriterConfig(analyzer);
9 u9 t2 @- `9 L, t' O& u6 v6 e2 @ V IndexWriter writer=new IndexWriter(dir, iwc);
( I, [& G1 { |2 `( O6 b* J, S return writer;
3 C5 K7 @1 k* x! C }
. `/ p5 t& ]# L S8 r7 O8 V
7 ]" c3 A# P/ i+ s /**
7 f* r3 |& g: a+ e p4 B * 生成索引
, L8 V# V( i. f3 B) B * @throws Exception
& A. e D( m5 P" R */$ l$ w0 y% }5 M: I$ l, z& g1 @
@Test
3 ^4 T' i; S/ c* I public void index()throws Exception{
6 o! H1 _5 p) e dir=FSDirectory.open(Paths.get("D:\\lucene3"));& O/ \7 Q; m' D- ]
IndexWriter writer=getWriter();5 i- J) N' s* w- g
for(int i=0;i<ids.length;i++){
7 L G- g' f |& B: K" i Document doc=new Document();6 G/ Y7 u" I* \
doc.add(new StringField("id", ids, Field.Store.YES));& q+ p6 C$ G `! w( u9 Q8 A( i* x
doc.add(new StringField("author",authors,Field.Store.YES));
0 E: J6 M$ Y+ c" D doc.add(new StringField("position",positions,Field.Store.YES));
/ X" Y% S+ @* X9 c i+ T, \ // 加权操作
3 K: J5 U! Q G TextField field=new TextField("title", titles, Field.Store.YES);9 t8 s# A8 R+ f7 U0 x7 R+ f8 U
if("boss".equals(positions)){2 p4 T/ s. ]/ w, M
field.setBoost(1.5f);: @! K" j, r+ P
}
3 t `+ B7 X( p) W9 g* a) G8 L9 H doc.add(field);
+ U8 }2 q- f/ H l doc.add(new TextField("content", contents, Field.Store.NO));9 ]9 n Z8 `+ A3 H
writer.addDocument(doc); // 添加文档
L* ?0 J! ]; N+ D: Y5 a) r1 h+ ^3 A }. N) S- ^4 G0 Y. h3 s) }- D
writer.close();' K0 P* a( }+ p9 `4 T3 |. t: \
}4 J; s% o" }4 }$ B0 Q* \6 u
( z% L' C: Q1 x2 o" b' G
/**
5 x6 G& v- h4 I/ N7 ~ * 查询0 t9 ~2 ?% n+ E+ j1 N M+ l6 x
* @throws Exception
( i2 O: E1 x( h$ Y V; @ */0 A6 d" w8 i& d- Z% ~' }8 H
@Test
! F" P4 a4 k: q7 z) g public void search()throws Exception{ Z) f$ J/ Q d( D9 C9 J+ E
dir=FSDirectory.open(Paths.get("D:\\lucene3"));5 I& }" ~- F. Z* f0 p0 |6 C
IndexReader reader=DirectoryReader.open(dir);8 u q, {, n1 X6 y3 G# d- G
IndexSearcher is=new IndexSearcher(reader);
) E2 j- Z0 Q7 B& B String searchField="title";# d' S# }. r$ D3 _) T
String q="java";" E' }4 Y' z4 W+ J. l1 N' ~5 T
Term t=new Term(searchField,q);
4 f ^# }- }, X* o) }, U/ b* p Query query=new TermQuery(t);3 a9 Y3 _( D3 p
TopDocs hits=is.search(query, 10);
5 M+ u( a2 h$ Q$ o System.out.println("匹配 '"+q+"',总共查询到"+hits.totalHits+"个文档");
+ n4 u4 }' }! t. G: U. h for(ScoreDoc scoreDoc:hits.scoreDocs){5 h1 P0 v1 R! d7 i0 Q: g) g9 N+ _3 p3 @
Document doc=is.doc(scoreDoc.doc);& @) h, x4 z( g# C$ Z$ u) E
System.out.println(doc.get("author"));( J9 Q4 Z- T* k% y- a, x- y
}
: B. K7 L7 c0 C reader.close();% M7 E5 \" I( \/ B. ]7 l, k0 |1 \- I
}3 `. E$ Z+ k, h1 y5 l
' m! ?2 i1 ^6 Y* X( P}
( W5 l5 t7 M4 `+ K* f3 L7 |: ]8 t
~, I! X6 O& J$ N2 V
- w6 b% G2 l! u5 g+ D& w& ~
|
|