|
1、源码
' R9 d4 l" Z4 T% H" S) L2 Nimport java.nio.file.Paths;" Q1 O& H, J$ H& @
* l; u X: P% H* r* W8 e7 G/ i
import org.apache.lucene.analysis.Analyzer; j- M S2 @0 K- Z! l$ G8 g! ^) [0 c
import org.apache.lucene.analysis.standard.StandardAnalyzer;
8 f2 h" C: B9 K2 _) e- n M4 s5 Bimport org.apache.lucene.document.Document;' c( F3 T, t9 s9 X
import org.apache.lucene.document.Field;# M( {1 }! u" b" K& w
import org.apache.lucene.document.StringField;. z% x" b, p8 R2 b( }/ i
import org.apache.lucene.document.TextField;3 F X T8 d& Y7 F' n
import org.apache.lucene.index.DirectoryReader;
. p' g1 d) W% w% j" ^import org.apache.lucene.index.IndexReader;
/ Q0 @; x3 N2 C/ rimport org.apache.lucene.index.IndexWriter;( A& e- A. x7 N7 u# {
import org.apache.lucene.index.IndexWriterConfig;
* |& a0 |% k3 t/ p' _ s6 }import org.apache.lucene.index.Term;
: q, P, z3 p4 m* C6 Kimport org.apache.lucene.search.IndexSearcher;
- f, D: |: ?' |- U* Q7 O9 H4 timport org.apache.lucene.search.Query;/ N: Y( k7 G5 I$ [3 a
import org.apache.lucene.search.ScoreDoc;' {, \3 W" h) G' e! _
import org.apache.lucene.search.TermQuery;
5 y0 ^1 _& O! A* ~; U$ Vimport org.apache.lucene.search.TopDocs;1 [' B$ \7 `2 L/ ~- Z
import org.apache.lucene.store.Directory;
" L& L! P8 }2 Gimport org.apache.lucene.store.FSDirectory;# E/ O! n! U* B% Y7 M) k
import org.junit.Test;
& D6 n% e. F: y! C# ~
! t4 l6 a0 G' `9 B L3 Xpublic class IndexingTest2 {
6 w) Q, B7 e. V; K# n1 G
. O0 X6 u, [/ f( t+ q% v4 K private String ids[]={"1","2","3","4"};
# @& `6 |, p' x+ k* E private String authors[]={"Jack","Marry","John","Json"};: I) {5 m, {+ {! }3 ?. O
private String positions[]={"accounting","technician","salesperson","boss"};
/ K7 R& |9 n* x* B$ O private String titles[]={"Java is a good language.","Java is a cross platform language","Java powerful","You should learn java"};% R$ v" O1 N+ D7 `
private String contents[]={; p" y8 }6 W m3 [
"If possible, use the same JRE major version at both index and search time.",
, G! U! W! P8 P2 v2 F, F "When upgrading to a different JRE major version, consider re-indexing. ",$ U7 Y: z/ X' W
"Different JRE major versions may implement different versions of Unicode,",) \+ g1 a2 g. o. B; ~4 u5 @9 |
"For example: with Java 1.4, `LetterTokenizer` will split around the character U+02C6,"2 n+ S- a) e6 o' M
};
+ i4 ^$ d/ S6 m D3 Z2 D) o, `+ n+ N! Q& c2 S
private Directory dir;( P* ^# l* u$ V% i8 _6 \
, q- n. d6 |% M* s
/**
# I3 E( n# V* M' G- ~ * 获取IndexWriter实例
; o% Q0 H5 f; u5 h * @return
* p( O" T, p q, i- A * @throws Exception
2 s; u4 S' M6 e O- Q */
4 z, Y" G. j5 X' q private IndexWriter getWriter()throws Exception{
" F3 P( p$ Q% s* U& }/ V Analyzer analyzer=new StandardAnalyzer(); // 标准分词器7 U+ N7 j' x1 g8 c) J5 g
IndexWriterConfig iwc=new IndexWriterConfig(analyzer);) `5 J# c( ^- `
IndexWriter writer=new IndexWriter(dir, iwc);
0 h! G/ E$ t9 Q: `3 B$ t; o2 c return writer;
' e) Q+ l$ E# T }
; T: f9 G8 c. U( ~2 P6 H& _, J1 N L4 d6 |
/**) v/ V2 E' [ v. x4 Y4 d8 i x
* 生成索引
, `8 {) R2 |/ R+ a! l% [. f2 y * @throws Exception
* a, u$ \2 V3 G8 `- i# F" Z1 | */# w$ I' p1 z' _' S2 d% L+ G
@Test" z' m! p$ D% s
public void index()throws Exception{% k1 c' U2 I0 t. L( F% ? p: L
dir=FSDirectory.open(Paths.get("D:\\lucene3"));
& b, ]6 z4 t! m% A# A( u( v IndexWriter writer=getWriter();' Y8 ^; [) A" H( Z5 W' w
for(int i=0;i<ids.length;i++){
) {# }: z2 ~7 p* ?: C4 x# e Document doc=new Document();
2 ^; C4 \' c3 W" E0 g5 k8 y$ h doc.add(new StringField("id", ids, Field.Store.YES));) r- E1 j+ n/ q/ {5 n2 E8 J
doc.add(new StringField("author",authors,Field.Store.YES));
- x: i& \& u6 y- Z- I$ b5 D doc.add(new StringField("position",positions,Field.Store.YES));
8 Q: z5 ~" @5 K7 b& q4 i, L; R // 加权操作
; s. q, P+ k, j* U" l: x, a TextField field=new TextField("title", titles, Field.Store.YES);4 ^- w9 L& L( { O
if("boss".equals(positions)){% H( n- Z, z9 k# R$ I1 t
field.setBoost(1.5f);
6 _: j5 q' s; k% g" K) o }
9 ]& h- t7 v/ T! o) `0 U5 o doc.add(field);
' x* h( [& |& K doc.add(new TextField("content", contents, Field.Store.NO));
* m0 i4 H" A% _+ c' A writer.addDocument(doc); // 添加文档
3 ?! [: M+ }) u) g3 p d }
8 u x+ x! n+ P* A( ^, M; a writer.close();, c% f8 p" k# ?# N$ F
}
: @0 A+ k5 r" |3 s6 s2 E# J" n& W6 N
/**' I* s4 q$ \7 F' ~; {# e1 i
* 查询
( T& w z; t1 ^/ l+ G+ L/ w * @throws Exception& ]* O# t( m% f( v7 I
*/8 {+ D" N2 m; e; I- P- F9 h
@Test' A3 x5 p6 P" l- H: j; k* V
public void search()throws Exception{( a2 Y3 Y. f$ k c2 J4 d. t
dir=FSDirectory.open(Paths.get("D:\\lucene3"));5 o& Y' \6 S/ G; z& U9 d" C
IndexReader reader=DirectoryReader.open(dir);
, m% n( D" U9 y IndexSearcher is=new IndexSearcher(reader);
5 ~% @! `2 i: O5 X" i- J String searchField="title";* ?$ H+ j. U2 Z7 a7 q& u
String q="java";
$ j; n |, W; v/ o2 f% W& h Term t=new Term(searchField,q);
% W% D3 v/ s. s: I2 {, ^) y5 _' D Query query=new TermQuery(t);
: {4 x5 b, T' ]8 Z, |8 }% m, ^ TopDocs hits=is.search(query, 10);0 ?9 V2 V( G, G9 I7 \9 m' y+ o& z
System.out.println("匹配 '"+q+"',总共查询到"+hits.totalHits+"个文档");+ w! v6 u+ O" \" {1 P u0 Q
for(ScoreDoc scoreDoc:hits.scoreDocs){1 l2 J7 Z6 C0 {4 ?! E1 v7 D
Document doc=is.doc(scoreDoc.doc);
! z5 _& i' x. }' S) s* o+ b2 t System.out.println(doc.get("author"));
( C v1 O* y* K! s; R0 n; U% H }4 s# l) i! J6 z: F+ @
reader.close();
$ ^" c! Q$ x) H3 H# Z }- x8 K9 g2 q% Y. M" W
5 C% Y1 R# V3 e5 }8 C}
; I" S+ F+ z+ G6 K1 i
# }" l7 t$ I. R+ u8 d1 q7 g' [; a4 ~3 @! M8 C+ j7 ^) v
% T5 C4 o; p3 [9 E |
|