nicolas.helleringer wrote:
Could you please show your code / usage of the Coordinates interface in your entity ?
Hi Nicolas,
Thank you for your response.
Here is my entity class:
Code:
@Entity
@Table(name = "myisam_product_article", catalog = "hibernatedb", schema = "")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "MyisamProductArticle.findAll", query = "SELECT m FROM MyisamProductArticle m"),
@NamedQuery(name = "MyisamProductArticle.findByArticleId", query = "SELECT m FROM MyisamProductArticle m WHERE m.articleId = :articleId"),
@NamedQuery(name = "MyisamProductArticle.findByLat", query = "SELECT m FROM MyisamProductArticle m WHERE m.lat = :lat"),
@NamedQuery(name = "MyisamProductArticle.findByLon", query = "SELECT m FROM MyisamProductArticle m WHERE m.lon = :lon"),
@NamedQuery(name = "MyisamProductArticle.findByCreationDate", query = "SELECT m FROM MyisamProductArticle m WHERE m.creationDate = :creationDate")})
@Spatial(spatialMode = SpatialMode.GRID)
//This annotation tells hibernate search that this class has to be indexed
@Indexed(index = "MyisamProductArticle")
@Analyzer(impl = org.apache.lucene.analysis.standard.StandardAnalyzer.class)
@AnalyzerDef(name = "customanalyzer", tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
filters = {@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
@Parameter(name = "language", value = "English"),
}),
@TokenFilterDef(factory = SynonymFilterFactory.class, params = {
@Parameter(name = "ignoreCase", value = "true"),
@Parameter(name = "expand", value = "true"),
@Parameter(name = "synonyms", value="syntest.txt")})
})
public class MyisamProductArticle implements Serializable, Coordinates, Comparable<MyisamProductArticle> {
private static final long serialVersionUID = 1L;
private Integer articleId;
@Size(max = 65535)
@Analyzer(definition = "customanalyzer")
@Field(index = Index.YES, store = Store.YES)
private String aDesc;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Latitude(of="location")
private Double lat;
@Longitude(of="location")
private Double lon;
private Date creationDate;
public MyisamProductArticle() {
}
public MyisamProductArticle(Integer articleId) {
this.articleId = articleId;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "article_id")
public Integer getArticleId() {
return articleId;
}
public void setArticleId(Integer articleId) {
this.articleId = articleId;
}
@Lob
@Column(name = "a_desc")
public String getADesc() {
return aDesc;
}
public void setADesc(String aDesc) {
this.aDesc = aDesc;
}
@Column(name = "lat")
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
@Override
public Double getLatitude() {
return lat;
}
@Override
public Double getLongitude() {
return lon;
}
@Column(name = "lon")
public Double getLon() {
return lon;
}
public void setLon(Double lon) {
this.lon = lon;
}
@Column(name = "creation_date")
@Temporal(TemporalType.DATE)
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
@Override
public int hashCode() {
int hash = 0;
hash += (articleId != null ? articleId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof MyisamProductArticle)) {
return false;
}
MyisamProductArticle other = (MyisamProductArticle) object;
if ((this.articleId == null && other.articleId != null) || (this.articleId != null && !this.articleId.equals(other.articleId))) {
return false;
}
return true;
}
@Field(store = Store.YES, index = Index.YES, analyze = Analyze.NO)
@FieldBridge(impl = SpatialFieldBridgeByQuadTree.class)
@Embedded
public Coordinates getLocation() {
return new Coordinates() {
@Override
public Double getLatitude() {
return lat;
}
@Override
public Double getLongitude() {
return lon;
}
};
}
// default comparator on Date
@Override
public int compareTo(MyisamProductArticle compareArticle) {
//ascending order
return this.creationDate.compareTo(compareArticle.creationDate);
//descending order
//return compareAritcle.compareTo(this.creationDate.creationDate);
}
@Override
public String toString() {
return "HibernateSearch.entity.MyisamProductArticle[ articleId=" + articleId + " ]";
}
}
Here is my facade class shown that how I use the entity:
Code:
final Integer SLOP_CONST = 2;
EntityManager em = emf.createEntityManager();
FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
try {
fullTextEntityManager.createIndexer().startAndWait();
} catch (InterruptedException ex) {
java.util.logging.Logger.getLogger(MyisamProductArticleFacade.class.getName()).log(Level.SEVERE, null, ex);
}
String termsStr = terms.toLowerCase();
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity( MyisamProductArticle.class ).get();
org.apache.lucene.search.Query query = null;
query = qb
.bool()
.must(qb.keyword().onFields("header", "aDesc").matching(termsStr).createQuery())
.must(qb.keyword().onField("state").matching(state).createQuery())
.must(qb.keyword().onField("postcode").matching(postcode).createQuery())
.must(qb.spatial()
.onDefaultCoordinates()
.within( radius, Unit.KM )
.ofLatitude( lat )
.andLongitude( lon ).createQuery())
.createQuery();
}
Thanks
Sam