java自学网VIP

Java自学网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2632|回复: 2

【第三节】Lucene5文档域加权

[复制链接]
  • TA的每日心情
    开心
    2021-5-25 00:00
  • 签到天数: 1917 天

    [LV.Master]出神入化

    2025

    主题

    3683

    帖子

    6万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    66101

    宣传达人突出贡献优秀版主荣誉管理论坛元老

    发表于 2016-6-20 14:49:36 | 显示全部楼层 |阅读模式
    1、源码. N% X9 A5 I2 }/ b" X2 l
    import java.nio.file.Paths;& B! }: w# A; ?  D6 `% o
    2 K  |& h% J5 S4 I. v; ?  C: I" b
    import org.apache.lucene.analysis.Analyzer;! {& u8 E* j6 a7 C0 _: ]# v
    import org.apache.lucene.analysis.standard.StandardAnalyzer;1 C. M2 G! W/ W; s) @4 @
    import org.apache.lucene.document.Document;; y6 Y0 S  H/ o  S0 I4 l8 O# R  I
    import org.apache.lucene.document.Field;6 @# A& n& ?( e% v# T6 {
    import org.apache.lucene.document.StringField;
    . N, k6 ]4 Y" Q( ]1 y' u+ c0 yimport org.apache.lucene.document.TextField;
    1 i+ ^3 ~6 U" k+ |# f! E6 kimport org.apache.lucene.index.DirectoryReader;
    8 [; m% l+ m# q! i1 mimport org.apache.lucene.index.IndexReader;  F6 j1 j" C0 z
    import org.apache.lucene.index.IndexWriter;% h1 n; S, ^: ]5 N. Y# b8 e
    import org.apache.lucene.index.IndexWriterConfig;) f4 O  X* `& B
    import org.apache.lucene.index.Term;% a6 b# v" @! [) S% k2 g
    import org.apache.lucene.search.IndexSearcher;
    0 ^! e7 N* ]( y' }; qimport org.apache.lucene.search.Query;6 ]8 J5 [( L2 f7 L9 F' j
    import org.apache.lucene.search.ScoreDoc;
    3 B" v9 |* d9 T! U2 w! j) Yimport org.apache.lucene.search.TermQuery;4 [% ?7 F" g7 q
    import org.apache.lucene.search.TopDocs;
    ' t, R( w# \* W9 E* i1 d% K: Aimport org.apache.lucene.store.Directory;; F1 p  E5 F( i& z5 E3 w
    import org.apache.lucene.store.FSDirectory;5 Z+ |9 D0 a' R
    import org.junit.Test;
    9 ~0 x+ z, c% b! x
    " C4 R9 h$ d8 N- a7 B  V/ spublic class IndexingTest2 {
    7 U4 ^# C( T  |7 U$ V% [8 S1 o( Z4 A4 _
        private String ids[]={"1","2","3","4"};" m' |* S; y: Q" \, A" [
        private String authors[]={"Jack","Marry","John","Json"};
    - ~" |8 o- V4 ]+ ~    private String positions[]={"accounting","technician","salesperson","boss"};
    5 S& {/ H' V% p5 s9 T2 R( v    private String titles[]={"Java is a good language.","Java is a cross platform language","Java powerful","You should learn java"};% i) Q& Q$ N$ r3 J5 j; b
        private String contents[]={. `9 n; `4 ?9 v, Y: v% \" ^( W
                "If possible, use the same JRE major version at both index and search time.",5 ]* V* U& h1 g+ ]
                "When upgrading to a different JRE major version, consider re-indexing. ",' u' J, g1 E5 T, L6 z
                "Different JRE major versions may implement different versions of Unicode,",: L- X8 e7 ?/ {
                "For example: with Java 1.4, `LetterTokenizer` will split around the character U+02C6,"
    : {9 ~: {( \% f4 `* V* V- y. V3 q    };
    % D( T) ?3 `+ S6 T! _1 u; n* s* P# ?# G6 N
        private Directory dir;
    0 l( C, Q4 I' e0 k, K% ~! x4 G# U; b, h8 j
        /**8 c& B' ?$ b7 p' m; P. Z( ^' O7 y
         * 获取IndexWriter实例
    : E% m+ d( W: g. x& A- A     * @return7 a. o$ x+ X* H" C( c  N
         * @throws Exception3 J6 g, ]% k( m8 a; @
         */7 S5 R1 I% o" k% E" N! e7 ?& D
        private IndexWriter getWriter()throws Exception{; ?+ V# e$ T% |
            Analyzer analyzer=new StandardAnalyzer(); // 标准分词器
    ) z; b; J0 Z; R& X) u        IndexWriterConfig iwc=new IndexWriterConfig(analyzer);
    1 i- H. S* u& B' q) z        IndexWriter writer=new IndexWriter(dir, iwc);
    0 E2 f* {! X2 Y- i# w8 \6 w! _        return writer;
    7 J7 \- U: ]0 ^3 V6 J    }( K3 c1 O9 N: N3 [& I
    # V6 e9 v8 X7 q
        /**
    % P3 M9 Y- _% e     * 生成索引, d/ a9 k- Q- N: r: P2 F7 g2 U
         * @throws Exception
    ! w! s6 j) t4 t3 k  ?     */
    ( j" y( D$ v0 [9 I    @Test9 ^* A$ o5 q9 A' Q8 K
        public void index()throws Exception{
    : [  e" ^: o& k1 e" L) ^5 J        dir=FSDirectory.open(Paths.get("D:\\lucene3"));
    5 e# ^' b0 P  @6 J        IndexWriter writer=getWriter();; F/ [0 Q6 X3 T2 M& p
            for(int i=0;i<ids.length;i++){) z% O, {+ Z) p
                Document doc=new Document();
    0 ^1 Q' i& ?3 |5 d. v2 V# L  q' l1 c            doc.add(new StringField("id", ids, Field.Store.YES));+ j- V" |; q. T# K: a$ k9 @0 u
                doc.add(new StringField("author",authors,Field.Store.YES));  y* i. L4 `, y
                doc.add(new StringField("position",positions,Field.Store.YES));: A& K5 N, E5 C, l/ p
                // 加权操作
    % [1 f( G6 d4 o8 F2 J( |            TextField field=new TextField("title", titles, Field.Store.YES);; Y$ \- e# }  e, O4 f1 ]3 w5 f' \4 [* S
                if("boss".equals(positions)){7 r' k/ v6 D1 U$ }. }0 G# [
                    field.setBoost(1.5f);
    0 e0 X1 U' v5 d0 n1 Y3 q* |) {            }
    4 ~+ r- g0 f4 R$ K( u            doc.add(field);
    # }& M: C; G" G, T            doc.add(new TextField("content", contents, Field.Store.NO));6 Z. Z6 j7 j' d" D
                writer.addDocument(doc); // 添加文档
    , D, d  J. G7 {  K) i& R+ x/ k- R        }
    3 S: C& d/ R" ^        writer.close();
    / S% R2 U' @; N5 g1 s4 Y    }1 a5 F; l$ ^! c; C
    2 z- P4 ?2 F# S( U1 C' {/ d
        /**( |) ^" ]6 {: s2 q6 A2 i5 h
         * 查询5 {/ z4 [" O) o" z
         * @throws Exception
      Q) R- Z5 a+ J; @6 V     */" k( l3 J% E% j" l. {  k
        @Test( u! |. y+ _; C  O% E
        public void search()throws Exception{
    0 S3 ~* t) K& ~, M" l- s        dir=FSDirectory.open(Paths.get("D:\\lucene3"));* A% j, }" f& b% I4 I+ O9 K' m
            IndexReader reader=DirectoryReader.open(dir);. Y9 I, G  M; Y
            IndexSearcher is=new IndexSearcher(reader);
    ( l$ [7 X- G/ k, W: X/ U        String searchField="title";
    , u0 X, X5 i2 D+ j  p8 f! Y0 k  h/ o9 E        String q="java";
    1 F4 L8 {$ d' A+ P( i% g" ?  \+ a( W. j8 O        Term t=new Term(searchField,q);
    8 ~% i- @' O3 ^0 P+ H7 B        Query query=new TermQuery(t);& ^, H- X  C8 X7 y) o. H7 ~
            TopDocs hits=is.search(query, 10);& y, m4 P% C" W- J
            System.out.println("匹配 '"+q+"',总共查询到"+hits.totalHits+"个文档");
    5 Z/ h3 U8 R' o5 j0 s# w, t        for(ScoreDoc scoreDoc:hits.scoreDocs){; P# j% _: s* h0 s
                Document doc=is.doc(scoreDoc.doc);# c# s" r. T" r2 K
                System.out.println(doc.get("author"));; X0 U& v8 v. `
            }
    & e3 w3 V: x0 A) h9 V" L- E        reader.close();( l: P' M0 |+ O, H8 z" ~, V
        }' e( U( _! A6 n6 A3 l& F
    ) u" H1 F: F( w/ ?9 E' u4 c
    }. S/ v+ S- h& {/ C+ P, r, p

    & a* y- r: z; h) F
    + \/ @! D/ M( m2 J6 ^& l
    ; X) m: C" o. t; |
    回复

    使用道具 举报

  • TA的每日心情

    2016-10-29 14:08
  • 签到天数: 1 天

    [LV.1]初学乍练

    0

    主题

    15

    帖子

    41

    积分

    普通会员

    Rank: 2

    积分
    41
    发表于 2016-10-29 14:14:51 | 显示全部楼层
    看了那么多,还是觉得我参加的北京尚学堂的教的好。包教包会,而且毕业就有1W的工资在手。一起来学习吧
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    慵懒
    2016-10-30 10:51
  • 签到天数: 1 天

    [LV.1]初学乍练

    0

    主题

    34

    帖子

    78

    积分

    普通会员

    Rank: 2

    积分
    78
    发表于 2016-10-30 12:24:00 | 显示全部楼层
    java自学网给力 亲测资源可以
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|Java自学网

    GMT+8, 2024-4-30 03:44 , Processed in 0.103767 second(s), 29 queries .

    Powered by Javazx

    Copyright © 2012-2022, Javazx Cloud.

    快速回复 返回顶部 返回列表