Tutorial.Java
Code:
package roseindia.net.dao.hibernate;
import java.io.Serializable;
import javax.persistence.Id;
import org.hibernate.annotations.Entity;
import org.hibernate.search.annotations.Analyzer;
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
@Indexed
@Analyzer (impl = roseindia.net.dao.hibernate.English.class)
public class Tutorial implements Serializable {
/** identifier field */
@Id
@DocumentId
private Integer id;
/** persistent field */
@Field(index=Index.TOKENIZED, store=Store.YES)
private String shortdesc;
/** persistent field */
@Field(index=Index.TOKENIZED, store=Store.YES)
private String longdesc;
/** persistent field */
//@Field(index=Index.TOKENIZED, store=Store.YES)
private String pageurl;
/** full constructor */
public Tutorial(Integer id, String shortdesc, String longdesc, String pageurl) {
this.id = id;
this.shortdesc = shortdesc;
this.longdesc = longdesc;
this.pageurl = pageurl;
}
/** default constructor */
public Tutorial() {
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getShortdesc() {
return this.shortdesc;
}
public void setShortdesc(String shortdesc) {
this.shortdesc = shortdesc;
}
public String getLongdesc() {
return this.longdesc;
}
public void setLongdesc(String longdesc) {
this.longdesc = longdesc;
}
public String getPageurl() {
return this.pageurl;
}
public void setPageurl(String pageurl) {
this.pageurl = pageurl;
}
}
English.javaCode:
public class English extends Analyzer
{
/**
This method useful in following situation<br>
Assume that one of our sentContent entities contains the display name "WWE rocks" <br>
and you want to get hits for all of the following queries: "rock", "rocks", "rocked" <br>
and "rocking".In this case this method applies word stemming during the indexing process.
*/
@Override
public TokenStream tokenStream(String fieldName, Reader reader) {
TokenStream result = new StandardTokenizer(reader);
result = new StandardFilter(result);
//result = new LowerCaseFilter(result);
String fi = null;
try{fi = result.next().toString();}catch(Exception e){}
System.out.println("result "+result+" "+fi );
result = new SnowballFilter(result, "English");
return result;
}
}
Main Logic for Index and SearchCode:
Session session = _factory.openSession();
FullTextSession fullTextSession = Search.createFullTextSession(session);
Transaction tx=fullTextSession.beginTransaction();
List<Tutorial> tutorials = session.createQuery("from Tutorial as tutorials").list();
for (Tutorial tutorial : tutorials) {
System.out.println("Indexing");
fullTextSession.index(tutorial);
}
tx.commit();
-------------------
session = _factory.openSession();
fullTextSession = Search.createFullTextSession(session);
tx=fullTextSession.beginTransaction();
MultiFieldQueryParser multiFieldParser = new MultiFieldQueryParser(
new String[] {"shortdesc","longdesc","pageurl"},
new StandardAnalyzer()
);
Query luceneQuery = null;
luceneQuery = multiFieldParser.parse("gun");
org.hibernate.Query hQuery = fullTextSession.createFullTextQuery(
luceneQuery,
Tutorial.class
);
//hQuery.setFetchSize(2);
List all = hQuery.list();
System.out.println("Test Size="+all.size());
tx.commit();
In the above case search is working only on
longdesc field.
Search is not working on
shordesc field,even though perfect match avilable on database.
Please guide me, If i missing any thing to get full search!!
Thanks
Ismail