-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: How to use Coordinates with Hibernate-search-4.2.0
PostPosted: Wed Jan 30, 2013 8:32 am 
Regular
Regular

Joined: Fri Feb 04, 2011 8:34 pm
Posts: 66
Hi, with Hibernate search 4.2.0 Final, can any body please advise how to use its Coordinates interface in my entity bean class?

In the Test suit of hibernate search, its POI.java class uses the Coordinate in this way:

Code:
@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;
                        }
                };
        }


But when I applied the same codes in my sample project, it complained the getLocation() method, which is shown below:

Quote:
Basic attributes can only be of the following types: Java primitive types, wrapper of primitive types, String, java.math.BigInteger, java.math.BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.TimeStamp, byte[], Byte[], char[], Character[], enums, or any Serializable type.


I am wondering how to use it.

Thanks a lot
Samuel


Top
 Profile  
 
 Post subject: Re: How to use Coordinates with Hibernate-search-4.2.0
PostPosted: Thu Jan 31, 2013 9:28 am 
Contributor
Contributor

Joined: Tue Jan 29, 2013 11:56 am
Posts: 3
Could you please show your code / usage of the Coordinates interface in your entity ?


Top
 Profile  
 
 Post subject: Re: How to use Coordinates with Hibernate-search-4.2.0
PostPosted: Thu Jan 31, 2013 9:52 am 
Regular
Regular

Joined: Fri Feb 04, 2011 8:34 pm
Posts: 66
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


Top
 Profile  
 
 Post subject: Re: How to use Coordinates with Hibernate-search-4.2.0
PostPosted: Thu Mar 21, 2013 12:44 am 
Regular
Regular

Joined: Fri Feb 04, 2011 8:34 pm
Posts: 66
By the way, how to set sort on date along with distance? eg. primary sort on date, and secondary sort on distance?
Thanks
Sam


Top
 Profile  
 
 Post subject: Re: How to use Coordinates with Hibernate-search-4.2.0
PostPosted: Thu Mar 21, 2013 12:58 am 
Regular
Regular

Joined: Fri Feb 04, 2011 8:34 pm
Posts: 66
samsam9988 wrote:
By the way, how to set sort on date along with distance? eg. primary sort on date, and secondary sort on distance?
Thanks
Sam


Here is my code for searching:

Code:
Sort distanceSort = new Sort(new DistanceSortField(lat, lon, "location"));
persistenceQuery = fullTextEntityManager.createFullTextQuery(query, ProductArticle.class).setSort(distanceSort);



bean class:

Code:
@Column(name = "creation_date")
    @Temporal(TemporalType.DATE)
    @Boost(2.0f)
    @Field(index = Index.YES, store = Store.YES)
    @DateBridge(resolution=Resolution.DAY)
    private Date creationDate;


I wonder how to add creationDate field to the Sorting.

Any suggestion is very appreciated.
Thanks
Sam


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.