4

I am using Compass to make queries on data inside in memory data structure. It works fine for searching String and enum values, now I want to search dates.

Search criteria are annotated by @SearchRestriction annotation. Example about someDate:

    @SearchRestriction(path="fooBar.someDate" type = SearchRestrictionType.EQUAL)
    String someDate;

At searchable data SomeDate is annotated like the following:

    @SearchableProperty
    Date someDate;

SomeDate inside the searchable data is generated with new Date();) and query String is given as 20120802.

Situation on debugger: This code generates queries like this:

    someDate:20120802

Here someDate is the name of the field I am looking for and 20120802 is a date in order yyyyMMdd.

Problem: No results is returned, when this query is run. I get an empty list. The Date in query is the same as in the Date object.

What is wrong?? Is this wrong way to search Dates with Compass? I can find only range queries about Date, but a search with exact Date or part of exact Date I cannot find.

mico
  • 12,730
  • 12
  • 59
  • 99
  • [http://lucene.apache.org/core/old_versioned_docs/versions/3_1_0/api/core/org/apache/lucene/document/DateTools.html](http://lucene.apache.org/core/old_versioned_docs/versions/3_1_0/api/core/org/apache/lucene/document/DateTools.html) tells how Date can be converted to String for indexing. Should this mean my @SearchableProperty should go on a String formed with DateTools? And then it works? – mico Aug 08 '12 at 09:50
  • I believe there is a way to go with Date objects but I have to be missing something crusial? Anybody has made queries with Dates? – mico Aug 13 '12 at 23:48
  • I have never used compass API but what I do know is all annotation based framework use reflection so value printed in the logs may not be used while comparison is being done. The annotation should specify way to convert date object to string. I will advice you to look by this perspective. – Amit Deshpande Aug 14 '12 at 11:27
  • Thanks Amit. Actually `someDate:20120802` is the real query String that is lured from that String variable by debugging the program. It really uses that String to pass to the engine for search. It is not just a log entry. – mico Aug 14 '12 at 13:24
  • Well, you pointed to the right direction. This is what I found based on your comment: http://www.compass-project.org/docs/2.0.0M2/reference/html/core-osem.html . I should tell the format of Date with `@SearchableProperty(format = "yyyy-MM-dd")` – mico Aug 14 '12 at 13:27
  • Great :) Happy to see solution is what I thought. – Amit Deshpande Aug 14 '12 at 16:49

1 Answers1

2

You need to specify the format for Searchable property [Date]

@SearchableProperty(format = "yyyyMMdd")

To some extent, it relates to Grails: Lucene, Compass Query Builder and date ranges

Community
  • 1
  • 1
Puspendu Banerjee
  • 2,631
  • 16
  • 19