java自学网VIP

Java自学网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3011|回复: 2

【第三节】Lucene5文档域加权

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

    [LV.Master]出神入化

    2096

    主题

    3754

    帖子

    6万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    66788

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

    发表于 2016-6-20 14:49:36 | 显示全部楼层 |阅读模式
    1、源码- r* t6 C9 }. ~( M2 z+ S
    import java.nio.file.Paths;5 M% Q. m8 d) D6 I. ~
    5 J: I" ^8 X1 |# E
    import org.apache.lucene.analysis.Analyzer;
    7 A& s+ s6 ~1 O+ v" y# eimport org.apache.lucene.analysis.standard.StandardAnalyzer;
    % k- w4 s/ u# Y' s0 _1 t4 Qimport org.apache.lucene.document.Document;4 D- G8 v# I+ K
    import org.apache.lucene.document.Field;
    & T$ x# h2 u, D$ l6 rimport org.apache.lucene.document.StringField;/ w+ e! `3 K2 Y- n
    import org.apache.lucene.document.TextField;
    2 V1 n$ k/ ]6 ~6 O0 Ximport org.apache.lucene.index.DirectoryReader;
    : l! T( e% W( A5 `3 m! I& K; H+ j; l9 g! Rimport org.apache.lucene.index.IndexReader;
    7 t8 f( f$ J0 mimport org.apache.lucene.index.IndexWriter;
    $ Z9 \3 @# ]" L! }2 _5 Timport org.apache.lucene.index.IndexWriterConfig;
    0 \& x$ V9 h# c8 N; iimport org.apache.lucene.index.Term;
    $ u" a% c  y( n8 v0 Fimport org.apache.lucene.search.IndexSearcher;" N+ g) X3 a$ h0 p( a* A- f
    import org.apache.lucene.search.Query;, p9 i' V+ _" z% H5 x& o6 F- Z
    import org.apache.lucene.search.ScoreDoc;
    ! C" t+ A& \  e: l. f- ~# zimport org.apache.lucene.search.TermQuery;- x8 _, t6 W8 z4 g# A1 R9 u
    import org.apache.lucene.search.TopDocs;( F( n  K+ P6 X8 H0 H* G
    import org.apache.lucene.store.Directory;: I( E5 T7 N! s( ]. O; V
    import org.apache.lucene.store.FSDirectory;
    ( m. D1 C. {  m& \7 Eimport org.junit.Test;
    + ?; `( d0 h, Y/ E2 Z) @" ^# s1 |# s
      s! z! z5 f9 g& s! ?public class IndexingTest2 {8 A0 O. o- ~5 P6 E3 w" ~; [

    8 l) h& p4 W1 ]1 B# O: U    private String ids[]={"1","2","3","4"};. J  p, d1 ~) u: {2 U  \( P
        private String authors[]={"Jack","Marry","John","Json"};! `9 j7 m& C2 u/ V$ p$ }: i
        private String positions[]={"accounting","technician","salesperson","boss"};
    ! D: P$ D* o* y* E( F7 s    private String titles[]={"Java is a good language.","Java is a cross platform language","Java powerful","You should learn java"};+ E3 f' Y, u! @7 s) N! i, a
        private String contents[]={! K* u/ _! P; p* i8 i
                "If possible, use the same JRE major version at both index and search time.",
    / E0 k6 @$ M% F& j            "When upgrading to a different JRE major version, consider re-indexing. ",
    , T, S& f- K6 p+ g5 c9 H" y2 V7 \            "Different JRE major versions may implement different versions of Unicode,",/ g8 X% `% n7 J  M4 \; `# C) ?4 E3 a
                "For example: with Java 1.4, `LetterTokenizer` will split around the character U+02C6,", U6 S6 B' a  H  u5 P
        };
    5 v: [6 {2 A' R. g& l4 S
    # Y% s( H8 D- q& I9 \0 w: B    private Directory dir;
    " Y6 l" F( w$ ]. Q7 r* N) m/ O3 F1 ~' A" a7 b+ F$ R0 _
        /**; ?( c  }# ^( ]$ ?
         * 获取IndexWriter实例" b9 B9 ~$ ]9 D% O
         * @return
    . X7 a( R; L9 ^1 ]     * @throws Exception; J4 A. B) D3 v& d
         */
    + }$ c! @7 K' a    private IndexWriter getWriter()throws Exception{: m" C5 P* A# p- \! n  e
            Analyzer analyzer=new StandardAnalyzer(); // 标准分词器
    * r9 z7 T) O0 M( A        IndexWriterConfig iwc=new IndexWriterConfig(analyzer);
    8 Z5 e0 P1 V* \; i5 [5 o" a: d- H' ~        IndexWriter writer=new IndexWriter(dir, iwc);5 o& k  O. ?9 Z; o  }7 @
            return writer;, p* P9 |. i; y4 ?# B- j6 G# V
        }
    ! D4 `3 d* {, g# k
    0 ^' e0 f. B4 q* j% X3 E    /**0 o" J$ f% H0 p; g$ x
         * 生成索引7 ]2 S- C' J' F  n+ q, E: k8 y" [$ H
         * @throws Exception. Y0 A' }$ C6 E/ t
         */
    6 A! F1 J" F. t) s  U. x$ f    @Test! j* I- j1 D- `  x1 ?
        public void index()throws Exception{( w- W) w& w: t5 J
            dir=FSDirectory.open(Paths.get("D:\\lucene3"));
    / V; n2 q, E& J0 ]        IndexWriter writer=getWriter();, X' F1 k4 Y: G" N& b! B
            for(int i=0;i<ids.length;i++){  ]& z& s9 t& e- X9 O
                Document doc=new Document();
    0 g7 ]( T5 N5 I  p9 V  [. s            doc.add(new StringField("id", ids, Field.Store.YES));
    ! U) t6 W* B: X" Z( X. E6 ~4 i7 o! t            doc.add(new StringField("author",authors,Field.Store.YES));
    6 B. g" H# p" p, O            doc.add(new StringField("position",positions,Field.Store.YES));
    7 h, |) r; ^" t- g- T5 ?$ c4 @6 S            // 加权操作
    2 P8 n3 b) }# Z6 G# H            TextField field=new TextField("title", titles, Field.Store.YES);
    , }: z  t  ]. ^# K            if("boss".equals(positions)){
    ' g6 ^9 |& K4 H) c3 ?/ e/ Y                field.setBoost(1.5f);
    # e  G) a" I9 B6 F            }% L% T% W1 F! D6 i
                doc.add(field);/ B& B# B9 h4 V, b: n( i% W+ X
                doc.add(new TextField("content", contents, Field.Store.NO));
    1 H. c0 F/ ]) {: o  U            writer.addDocument(doc); // 添加文档
    ) P% H1 r) \6 E- m        }
    3 u7 [! B% _4 S# B- o( O1 q" a        writer.close();' d! ~4 [6 e" g  V1 X) X
        }
    - D( [& ^! S! [! `! {3 s
    7 c5 }1 D' f! o) _    /**
    & K# c+ Z( v8 z/ {, C$ ~     * 查询
      M) d6 F3 q3 |- o9 A0 j     * @throws Exception% a( k" E5 L% F
         */
    0 |9 D0 f( v; j* @; @# \    @Test5 S6 V4 L/ \4 }( r! i
        public void search()throws Exception{4 @9 ?# B# W7 `1 P$ a
            dir=FSDirectory.open(Paths.get("D:\\lucene3"));
    $ J$ ?. s' i& S8 ?        IndexReader reader=DirectoryReader.open(dir);: G" X$ k) N( t, r
            IndexSearcher is=new IndexSearcher(reader);
    7 X2 |0 ?( |0 Q$ o. ~0 g# b        String searchField="title";
    ( b1 Q$ \# T% m" O- g        String q="java";
    - J' A" @/ K2 \! B        Term t=new Term(searchField,q);1 G  m2 m; z2 z1 e
            Query query=new TermQuery(t);. j% Q0 N8 ^9 j
            TopDocs hits=is.search(query, 10);+ O4 l* F) c9 |  ?# `3 ^+ Z" k
            System.out.println("匹配 '"+q+"',总共查询到"+hits.totalHits+"个文档");# a2 C7 c. U* f) p
            for(ScoreDoc scoreDoc:hits.scoreDocs){/ J+ ^3 S4 M- ~6 b
                Document doc=is.doc(scoreDoc.doc);
    9 @7 U% H5 K! c( ~8 v& J0 r) S            System.out.println(doc.get("author"));
    # \! R0 B8 _% x1 E. I        }
    4 c1 Q8 Z8 z. e* D; u! j1 q7 K; U        reader.close();
    1 }9 ]& T8 d2 D  \/ w# k# l' j6 p    }* {6 s8 g' J: f/ I. \) f7 D' _

    2 G( G- }# n$ B3 m% I$ E}
    1 b$ z! L( c$ s9 l% f, C/ a) ^  \2 p9 Q( M- Q

    3 @5 J" m! u& {% n; }5 w& X
    # l* @9 {- F0 b! M: s: E2 z
    回复

    使用道具 举报

  • 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, 2025-4-1 14:14 , Processed in 0.279178 second(s), 30 queries .

    Powered by Javazx

    Copyright © 2012-2022, Javazx Cloud.

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