cancel
Showing results for 
Search instead for 
Did you mean: 

SearchParameters and sorting by date

morgan_david
Champ in-the-making
Champ in-the-making
I'm using the Java foundation APIs for searching the repository and I'm trying to apply a sort on a date field.
Code example of what I'm trying to do:

From model:

<property name="ident:identDate">
    <type>d:date</type>
    <mandatory>true</mandatory>
</property>


String sortField = "@ident\\:identDate"
boolean sortAsc = true;
searchParams.addSort(sortField, sortAsc);

Now I'm able to sort by other properties in the model that are d:text.  Is there something special that needs to be done to sort by dates?

Thank you.
6 REPLIES 6

morgan_david
Champ in-the-making
Champ in-the-making
So I've looked a little more into this.  Lucene won't sort on a tokenized field.  So I've added another field that mimics the date field except it's just an int and it's stored untokenized.


<property name="ident:identDateSort">
   <type>d:int</type>
   <mandatory>true</mandatory>
   <index enabled="true">
      <tokenised>false</tokenised>
   </index>
</property>

Unfortunately things still aren't sorting.  What am I doing wrong?

morgan_david
Champ in-the-making
Champ in-the-making
I figured out what I was doing wrong.  I was passing the wrong field namespace.  Once I passed in the right namespace and stopped escaping some characters sorting started working.

chrisb
Champ in-the-making
Champ in-the-making
I figured out what I was doing wrong.  I was passing the wrong field namespace.  Once I passed in the right namespace and stopped escaping some characters sorting started working.

I am trying to do the same thing RE: sorting. Would you mind posting the correct syntax for the example you gave above - would really help me out  Smiley Very Happy

Thanks,

Chris

morgan_david
Champ in-the-making
Champ in-the-making
No problem.  Assuming your namespace is "http://www.example.com/name/space" and you want to sort of field "sort" then what you want to pass to addSort would be like so:

searchParams.addSort("@{http://www.example.com/name/space}sort", true);

When building a query you want to escape any special characters for Lucene like '{', ':', and '}', but for sorting you do not want to escape these characters.

For performance sake it's best if your sort field is not a string if you can help it.  String sorting is apparently the most expensive sort for Lucene.

chrisb
Champ in-the-making
Champ in-the-making
Thanks for the quick reply and the explanation - much appreciated, and exactly what I was after  😎 .

andy
Champ on-the-rise
Champ on-the-rise
Hi

You can sort on date properties even when tokenised. Date does not include time in the index by default. (It can be configured to use time and sort is still supported - we build the additional column required for sort internally in this configuration)

Stricly, lucene only fails to order if there is more than one token per doc (even if there are no docs for some and nonefor others)
This may have changed in later versions.

Andy
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.