hibernate search分目录存储测试

TEAM : I.S.T.O
AUTHOR : Jerry

实体类
package search.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;

@Entity
@Table(name="song")
@Indexed(index="song")
@NamedQueries({
    @NamedQuery(name="Song.list[find]",query="from Song s order by s.time desc"),
    @NamedQuery(name="Song.list[count]",query="select count(*) from Song"),
    @NamedQuery(name="Song.byCityId[load]",query="from Song s where s.artistCityId =?")
})
public class Song implements Serializable{

    private static final long serialVersionUID = -1475639504643543299L;

    @Id
    @DocumentId
    @GeneratedValue(generator="system_uuid")
    @GenericGenerator(name="system_uuid",strategy="uuid")
    private String id;

    @Column(name="artist",length=36)
    private String artist;

    @Column(name="name",length=36,nullable=false)
    @Field(index=Index.TOKENIZED,store=Store.NO)
    private String name;

    @Lob
    @Column(name="lyric",length=2000)
    @Field(index=Index.TOKENIZED,store=Store.NO)
    private String lyric;

    @Column(name="time",nullable=false)
    @Field(index=Index.UN_TOKENIZED,store=Store.NO)
    private Date time;

    @Column(name="artistCityId",nullable=false)
    @Field(index=Index.UN_TOKENIZED,store=Store.YES)
    private Integer artistCityId;

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLyric() {
        return lyric;
    }

    public void setLyric(String lyric) {
        this.lyric = lyric;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id=id;
    }

    public Integer getArtistCityId() {
        return artistCityId;
    }

    public void setArtistCityId(Integer artistCityId) {
        this.artistCityId = artistCityId;
    }

}

测试类
package search.test;

import java.util.Date;
import java.util.List;

import net.paoding.analysis.analyzer.PaodingAnalyzer;

import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import search.entity.Song;
import search.util.HibernateSessionFactory;

public class IndexTest {

    private Session session;

    @Before
    public void before(){
        session=HibernateSessionFactory.getSession();
    }

    @Test
    public void insert(){
        Transaction tx=session.beginTransaction();
        try {
            for(int i=0;i<1000;i++){
                Song song=new Song();
                song.setArtist("群星");
                song.setName("北京欢迎你");
                song.setLyric("北京欢迎你,welcome to beijing,第29届奥林匹克运动会");
                song.setTime(new Date());
                song.setArtistCityId(i%8);
                session.save(song);
                if(i%1000==0)session.flush();
            }
//            throw new RuntimeException("error");
        } catch (Exception e) {
            tx.rollback();
        }
        tx.commit();
    }

    @Test
    public void delete(){
        FullTextSession fullTextSession = Search.createFullTextSession(session);
        Transaction tx = fullTextSession.beginTransaction();
        List<Song> songs = session.getNamedQuery("Song.byCityId[load]").setInteger(0, 1).list();
        for (Song song : songs) {
            session.delete(song);
        }
        tx.commit();
     
    }

    @Test
    public void index(){
        FullTextSession fullTextSession = Search.createFullTextSession(session);
        Transaction tx = fullTextSession.beginTransaction();
        List<Song> songs = session.getNamedQuery("Song.list[find]").list();
        for (Song song : songs) {
            fullTextSession.index(song);
        }
        tx.commit();
     
    }

    @Test
    public void search() throws ParseException{
        FullTextSession fullTextSession = Search.createFullTextSession(session);
        Transaction tx = fullTextSession.beginTransaction();
        BooleanQuery booleanQuery=new BooleanQuery();
        MultiFieldQueryParser parser = new MultiFieldQueryParser( new String[]{"name", "lyric"},
          new PaodingAnalyzer());
        Query query = parser.parse( "北京欢迎你" );
        booleanQuery.add(query,BooleanClause.Occur.MUST);
        TermQuery tq=new TermQuery(new Term("artistCityId","0"));
        booleanQuery.add(tq,BooleanClause.Occur.MUST);
        org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery( booleanQuery, Song.class );
        List<Song> songs = hibQuery.list();
//        for(Song song:songs){
//            System.out.println(song.getArtist()+":"+song.getName()+":"+song.getLyric());
//        }
        System.out.println("count:"+songs.size());
        tx.commit();
    }

    @Test
    public void purgeAll(){
        FullTextSession fullTextSession = Search.createFullTextSession(session);
        Transaction tx = fullTextSession.beginTransaction();
        fullTextSession.purgeAll( Song.class );
        tx.commit();
        fullTextSession.getSearchFactory().optimize(Song.class);
        fullTextSession.getSearchFactory().optimize();
    }

    @After
    public void close(){
        HibernateSessionFactory.closeSession();
    }
}
此上代码全部拷贝即可正常使用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值