-->
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.  [ 25 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: how to set ascending and descending to hibernate search sort
PostPosted: Thu Mar 21, 2013 8:13 am 
Regular
Regular

Joined: Fri Feb 04, 2011 8:34 pm
Posts: 66
Hi,

I want to apply descending / ascending to hibernate search sort.
Here is my current code:

Code:
Sort dateSort = new Sort(new SortField("creationDate", SortField.STRING));
persistenceQuery = fullTextEntityManager.createFullTextQuery(query, ProductArticle.class).setSort(dateSort);


Can anyone please tell me how to add asceding/descending to my sorting?
Thanks
Sam


Top
 Profile  
 
 Post subject: Re: how to set ascending and descending to hibernate search sort
PostPosted: Thu Mar 21, 2013 8:55 am 
Regular
Regular

Joined: Fri Feb 04, 2011 8:34 pm
Posts: 66
I have tested the following code, but it doesn't sort and the result is not on descending order.

bean class:
Code:
@Field(index = Index.YES, store = Store.YES)
    @DateBridge(resolution=Resolution.DAY)
    private Date creationDate;


search code:
Code:
Sort dateSort = new Sort(new SortField("creationDate", SortField.STRING, true));  //DESC on creationDate; false = ASC
persistenceQuery = fullTextEntityManager.createFullTextQuery(query, ProductArticle.class).setSort(dateSort);


BTW, this is with hibernate search 4.2.0
Any suggestion is very much appreciated.
Thanks
sam


Top
 Profile  
 
 Post subject: Re: how to set ascending and descending to hibernate search sort
PostPosted: Thu Mar 21, 2013 9:31 am 
Regular
Regular

Joined: Fri Feb 04, 2011 8:34 pm
Posts: 66
I found that the value for the date field is in the form of 2013-02-15. Its data type is Date in mysql.
I don't know why hibernate search sorting has no effect on the date field.

Sam.


Top
 Profile  
 
 Post subject: Re: how to set ascending and descending to hibernate search sort
PostPosted: Thu Mar 21, 2013 1:33 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
use @Field(analyze=Analyze.NO) on a field you want to use for sorting, or it will be split in multiple terms, making it unsuitable for sorting.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: how to set ascending and descending to hibernate search sort
PostPosted: Thu Mar 21, 2013 8:32 pm 
Regular
Regular

Joined: Fri Feb 04, 2011 8:34 pm
Posts: 66
sanne.grinovero wrote:
Hi,
use @Field(analyze=Analyze.NO) on a field you want to use for sorting, or it will be split in multiple terms, making it unsuitable for sorting.

Thank you for your suggestion.
The syntax is not right when I applied your change to the date field.
Here is the error message:
Quote:
cannot find symbol
symbol: variable Analyze
location: class ProductArticle

an enum annotation value must be an enum constant


Thanks
Sam


Top
 Profile  
 
 Post subject: Re: how to set ascending and descending to hibernate search sort
PostPosted: Thu Mar 21, 2013 9:45 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
This is valid code I pasted from out testsuite:
Code:
@Field(analyze = Analyze.NO)


I guess you are importing a different Analyze class?

Code:
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Field;

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: how to set ascending and descending to hibernate search sort
PostPosted: Thu Mar 21, 2013 10:11 pm 
Regular
Regular

Joined: Fri Feb 04, 2011 8:34 pm
Posts: 66
sanne.grinovero wrote:
This is valid code I pasted from out testsuite:
Code:
@Field(analyze = Analyze.NO)


I guess you are importing a different Analyze class?

Code:
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Field;


Now I imported the Analyze file.
It compiled but the result still no sorting.

Here is the annotated field in the bean class:
Code:
@Field(analyze = Analyze.NO)
    @DateBridge(resolution=Resolution.DAY)
    private Date creationDate;


Search code:
Code:
Sort dateSort = new Sort(new SortField("creationDate", SortField.STRING, true));  //DESC on creationDate; false = ASC
persistenceQuery = fullTextEntityManager.createFullTextQuery(query, ProductArticle.class).setSort(dateSort);
persistenceQuery.setFirstResult(start); //eg. start from the 15th element
        persistenceQuery.setMaxResults(size); //eg. return 10 elements
        // execute search
        List result = persistenceQuery.getResultList();
     
        return result;


database table definition:
Quote:
creation_date | date | YES | | NULL
mysql> select creation_date from product_article;
+---------------+
| creation_date |
+---------------+
| 2013-02-15 |
| 2013-03-20 |
| 2013-02-23 |
+---------------+
3 rows in set (0.00 sec)

mysql>


Thanks
Sam


Top
 Profile  
 
 Post subject: Re: how to set ascending and descending to hibernate search sort
PostPosted: Thu Mar 21, 2013 10:43 pm 
Regular
Regular

Joined: Fri Feb 04, 2011 8:34 pm
Posts: 66
Here is more detail about the bean class:

Code:
@Spatial(spatialMode = SpatialMode.GRID, name="location")
@Indexed(index = "ProductArticle")
@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="synonyms.txt")}),
                      },charFilters = { @CharFilterDef(factory = HTMLStripCharFilterFactory.class)})
public class ProductArticle implements Serializable, Coordinates, Comparable<ProductArticle>{
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "article_id")
    private Integer articleId;
    @Lob
    @Size(max = 65535)
    @Column(name = "a_desc")
    @Analyzer(definition = "customanalyzer")
    @Field(index = Index.YES, store = Store.YES)
    private String aDesc;
   
    @Size(max = 255)
    @Column(name = "header")
    @Field(index = Index.YES, store = Store.YES)
    @Analyzer(definition = "customanalyzer")
    private String header;
   
    @Size(max = 64)
    @Column(name = "parent_category_name")
    @Analyzer(definition = "customanalyzer")
    @Field(index = Index.YES, store = Store.YES)
    private String parentCategoryName;
   
    @Size(max = 64)
    @Column(name = "sub_category_name")
    @Analyzer(definition = "customanalyzer")
    @Field(index = Index.YES, store = Store.YES)
    private String subCategoryName;
   
    @Size(max = 50)
    @Column(name = "state")
    @Analyzer(definition = "customanalyzer")
    @Field(index = Index.YES, store = Store.YES)
    private String state;
   
    @Size(max = 50)
    @Column(name = "suburb")
    @Analyzer(definition = "customanalyzer")
    @Field(index = Index.YES, store = Store.YES)
    private String suburb;
   
    @Size(max = 5)
    @Column(name = "postcode")
    @Analyzer(definition = "customanalyzer")
    @Field(index = Index.YES, store = Store.YES)
    private String postcode;

    @Column(name = "latitude")
    private Double latitude;

    @Column(name = "longitude")
    private Double longitude;
    @Column(name = "creation_date")
    @Field(analyze = Analyze.NO)
    @DateBridge(resolution=Resolution.DAY)
    private Date creationDate;


Top
 Profile  
 
 Post subject: Re: how to set ascending and descending to hibernate search sort
PostPosted: Fri Mar 22, 2013 10:58 am 
Regular
Regular

Joined: Fri Feb 04, 2011 8:34 pm
Posts: 66
Is there any example that have sorting on Date field, that I can follow?

Thanks
Sam


Top
 Profile  
 
 Post subject: Re: how to set ascending and descending to hibernate search sort
PostPosted: Sat Mar 23, 2013 5:00 am 
Regular
Regular

Joined: Fri Feb 04, 2011 8:34 pm
Posts: 66
oakley wrote:
(spam deleted)


I am writing a junit test case to fix it by myself.
I just realised really aren't many people out there have the time to help ya.
cheers
Sam


Top
 Profile  
 
 Post subject: Re: how to set ascending and descending to hibernate search sort
PostPosted: Sun Mar 24, 2013 2:34 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
since I'm not understanding what's wrong in your example, we created a test too:
https://github.com/Sanne/hibernate-search/commit/13aacca2efe5bc9cfa64b7a5a4988b0186d6f5a3

it's passing. Please use it as an example, or point out what you're doing differently?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: how to set ascending and descending to hibernate search sort
PostPosted: Mon Mar 25, 2013 8:28 am 
Regular
Regular

Joined: Fri Feb 04, 2011 8:34 pm
Posts: 66
sanne.grinovero wrote:
Hi,
since I'm not understanding what's wrong in your example, we created a test too:
https://github.com/Sanne/hibernate-search/commit/13aacca2efe5bc9cfa64b7a5a4988b0186d6f5a3

it's passing. Please use it as an example, or point out what you're doing differently?


Thank you for your code.
Do you know what package to include so that I can use the following import?

package org.hibernate.search.test does not exist

Here is my pom.xml file:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <artifactId>TestDate</artifactId>
    <groupId>HibernateSearchDateTest</groupId>
    <version>1.0</version>
  </parent>

    <groupId>HibernateSearchDateTest</groupId>
    <artifactId>TestDate-ejb</artifactId>
    <version>1.0</version>
    <packaging>ejb</packaging>

    <name>TestDate-ejb</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <log4j.version>1.2.14</log4j.version>
        <hibernate.version>4.1.7.Final</hibernate.version>
        <org.springframework-version>3.0.7.RELEASE</org.springframework-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
        <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-infinispan</artifactId>
                <version>4.1.9.Final</version>
        </dependency>
        <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>4.1.9.Final</version>
        </dependency>

        <dependency>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.0-api</artifactId>
                <version>1.0.1.Final</version>
        </dependency>
               
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-search</artifactId>
            <version>4.2.0.Final</version>
        </dependency>
        <!-- Additional Analyzers: -->
        <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-search-analyzers</artifactId>
           <version>4.2.0.Final</version>
        </dependency>
       
        <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-search-orm</artifactId>
                <version>4.2.0.Final</version>
                <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-envers</artifactId>
            <version>4.1.9.Final</version>
           

            <exclusions>
                <exclusion>
                    <groupId>dom4j</groupId>
                    <artifactId>dom4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>

<!--        <dependency>
                <groupId>org.apache.lucene</groupId>
                <artifactId>lucene-snowball</artifactId>
                <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>4.0.0</version>
        </dependency>
       
        <dependency>
                <groupId>org.apache.lucene</groupId>
                <artifactId>lucene-analyzers-common</artifactId>
                <version>4.0.0</version>
        </dependency>-->
       
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.2</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <ejbVersion>3.1</ejbVersion>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>6.0</version>
                                    <type>jar</type>
                                </artifactItem>
<!--                                <artifactItem>
                                    <groupId>org.hibernate</groupId>
                                    <artifactId>hibernate-search</artifactId>
                                    <version>4.2.0.Final</version>
                                    <type>jar</type>
                                </artifactItem>-->
                            </artifactItems>
                        </configuration>
                    </execution>
                   
                </executions>
            </plugin>
        </plugins>
    </build>

</project>


Thanks a lot
Sam


Top
 Profile  
 
 Post subject: Re: how to set ascending and descending to hibernate search sort
PostPosted: Mon Mar 25, 2013 8:52 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
That package is part of our testsuite, it's not distributed.

You can take the full sources - including this test - by cloning and building the git repository:

Code:
git clone git@github.com:hibernate/hibernate-search.git
cd hibernate-search
mvn clean install -s settings-example.xml

You'll find more details in the readme in the root of the sources.

Being a Maven project, should be trivial to import in your favourite IDE as well:
just import it, and feel free to play with my tests. If you can update it to make it fail, feel free to send a pull request back to us: I'll let you know if we need to fix something or in case wat's wrong with the use case.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: how to set ascending and descending to hibernate search sort
PostPosted: Mon Mar 25, 2013 8:20 pm 
Regular
Regular

Joined: Fri Feb 04, 2011 8:34 pm
Posts: 66
sanne.grinovero wrote:
That package is part of our testsuite, it's not distributed.

You can take the full sources - including this test - by cloning and building the git repository:

Code:
git clone git@github.com:hibernate/hibernate-search.git
cd hibernate-search
mvn clean install -s settings-example.xml

You'll find more details in the readme in the root of the sources.

Being a Maven project, should be trivial to import in your favourite IDE as well:
just import it, and feel free to play with my tests. If you can update it to make it fail, feel free to send a pull request back to us: I'll let you know if we need to fix something or in case wat's wrong with the use case.


HI thank you for sending me the instruction.
I got the following error when using git to fetch the source:

Quote:
git clone git@github.com:hibernate/hibernate-search.git
Cloning into hibernate-search...
The authenticity of host 'github.com (207.97.227.239)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: The remote end hung up unexpectedly


Thanks
Sam


Top
 Profile  
 
 Post subject: Re: how to set ascending and descending to hibernate search sort
PostPosted: Tue Mar 26, 2013 5:03 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
I'm sorry my bad I gave you the read/write URL.
Please try this one:
git://github.com/hibernate/hibernate-search.git

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 25 posts ]  Go to page 1, 2  Next

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.