fatherazrael fatherazrael - 4 months ago 22
SQL Question

How to display hibernate search results using QueryBuilder, Lucene Query & FullTextQuery?

How to display results using FullTextEntity Manager with order clause only.

EntityManager em = factory.createEntityManager();
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
QueryBuilder qb = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity( Desh.class ).get();

org.apache.lucene.search.Query query = new org.apache.lucene.search.MatchAllDocsQuery();
//Also used qb.all().getQuery(); but no results

FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, Desh.class);
System.out.println(""+ query);
System.out.println(""+ query.toString());

fullTextQuery.setSort(new Sort(new SortField("DeshName_for_sort", SortField.STRING, true)));
//Also used DESH but no results

fullTextQuery.setFirstResult(0).setMaxResults(60);
System.out.println("" + fullTextQuery);
System.out.println("" + fullTextQuery.getParameters());

int size = fullTextQuery.getResultSize();
List<Desh> result = fullTextQuery.getResultList();
System.out.println("Size e -> "+ size);
System.out.println("Size e -> "+ result.size());
for (Deshuser : result) {
System.out.println("Id: " + user.getId());
System.out.println("Person Id:" + user.getName());
}


Desh - Entity Class:

@Entity
@Table(name = "DESH")
@XmlRootElement
@Indexed

public class Desh implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID")
private Long id;

@Fields({ @Field(index = Index.YES, store = Store.NO), @Field(name = "DeshName_for_sort", index = Index.YES, analyze = Analyze.NO) })
@Column(name = "NAME", length = 100)
private String name;

public Desh () {
}

public Desh (Long id) {
this.id = id;
}

public Desh (Long id) {
this.id = id;

}

public Long getId() {
return id;
}

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



public String getName() {
return name;
}

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


@Override
public String toString() {
return "in.hibprac.hibernatepractice.Desh[ id=" + id + " ]";
}

}


When i run above program, it returns no size. But when i return using Namedquery and Native Query it executes fine. Even when i remove sorting statements in code, it returns nothing. Could anyone guide me where the problem is?

Answer

try this :

FullTextEntityManager ftem=Search.getFullTextEntityManager(entityManager);
ftem.createIndexer().startAndWait();

to launch an indexation targetting all entities annotated with @Indexed