用hibernate search增量索引

用hibernate search增量索引
1、创建POJO

package cn.search.pojo;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
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 = "search_foo")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Indexed(index = "search_foo")
public class Foo implements Serializable {

 /**
  *
  */
 private static final long serialVersionUID = 1L;
 @Id
 @DocumentId
 @Field(name = "id", index = Index.TOKENIZED, store = Store.YES)
 private Integer id;

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

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

 public Integer getId() {
  return id;
 }

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

 public String getName() {
  return name;
 }

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

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }
}

2、配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
 <session-factory>
  <property name="hibernate.dialect">
   org.hibernate.dialect.Oracle9Dialect
  </property>
  <property name="hibernate.connection.url">
   jdbc:oracle:thin:@192.168.0.21:1521:oradb
  </property>
  <property name="hibernate.connection.username">
   goodsres
  </property>
  <property name="hibernate.connection.password">
   goodsres
  </property>
  <property name="hibernate.connection.driver_class">
   oracle.jdbc.driver.OracleDriver
  </property>

  <property name="hibernate.search.default.directory_provider">
   org.hibernate.search.store.FSDirectoryProvider
  </property>
  <property name="hibernate.search.default.indexBase">
   e:/index
  </property>
  <property name="hibernate.cache.provider_class">
   org.hibernate.cache.HashtableCacheProvider
  </property>

  <mapping class="cn.search.pojo.Foo" />

 </session-factory>

</hibernate-configuration>
3、测试代码

package cn.search.manager;

import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;

import java.util.List;

import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.queryParser.QueryParser;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;


import cn.search.pojo.Foo;

public class SearchResultsHibernate {
 private static SessionFactory sf = null;

 private static Session session = null;

 private static Transaction tx = null;

 @BeforeClass
 public static void setupBeforeClass() throws Exception {
  sf = new AnnotationConfiguration().configure("hibernate.cfg.xml")
    .buildSessionFactory();
  assertNotNull(sf);
 }

 @Before
 public void setUp() throws Exception {
  session = sf.openSession();
  tx = session.beginTransaction();
  tx.begin();
 }

 @After
 public void tearDown() throws Exception {
  tx.commit();
  session.close();
 }

 public static void tearDownAfterClass() throws Exception {
  if (sf != null)
   sf.close();
 }

 @Test
 public void testAddDept() throws Exception {
  Foo foo = new Foo();

  foo.setId(1);

  foo.setName("第一个hibernate search");

  foo.setTitle("好好学习,天天向上");

  session.delete(foo);
 }

 @Test
 public void testIndex() throws Exception {
  FullTextSession fullTextSession = Search.createFullTextSession(session);
  assertNotNull(session);

  QueryParser parser = new QueryParser("title", new StopAnalyzer());
  org.apache.lucene.search.Query luceneQuery = parser.parse("好好学习");
  Query hibQuery = fullTextSession.createFullTextQuery(luceneQuery,
    Foo.class);

  List list = hibQuery.list();

  assertTrue(list.size() > 0);
 }

 public static void main(String[] args) {

  try {
   setupBeforeClass();
   SearchResultsHibernate searchResults = new SearchResultsHibernate();
   searchResults.setUp();
   searchResults.testAddDept();
   searchResults.tearDown();
   SearchResultsHibernate.tearDownAfterClass();

  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

你可能感兴趣的:(用hibernate search增量索引)