在BasicCar.java:
@Column(name = "name")
@Field(name="BaiscCarName",index=Index.TOKENIZED,store=Store.YES)
@Boost(0.1f)
private String name;
@Column(name = "factory" , length=50)
@Field(name="factory",index=Index.TOKENIZED,store=Store.YES)
@Boost(2.0f)
private String factory;
插入数据如下:
查询方法:
void queryEntity2() throws Exception{ //处理boost的Analyzer方法
session = sf.openSession();
FullTextSession fullTextSession=Search.getFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
QueryParser parser = new QueryParser(Version.LUCENE_31,"id",analyzer);
org.apache.lucene.search.Query luceneQuery =
parser.parse( "BaiscCarName:hello and factory:good" );
org.hibernate.Query fullTextQuery =
fullTextSession.createFullTextQuery( luceneQuery, BasicCar.class );
List result = fullTextQuery.list();
for(int i=0;i<result.size();i++)
{
BasicCar bc3 = (BasicCar) result.get(i);
System.out.println("id for the selected basiccar is:"
+bc3.getName()+" "+bc3.getFactory());
}
tx.commit();
session.close();
}
结果:
id for the selected basiccar is:very hello good
id for the selected basiccar is:hello very good
end