marklogic search by date field in java

171 Views Asked by At

I have a CustomPojo where I have a date field(testDate) in it. Now I want to search this CustomPojo's based on the date range of that date field. Am trying to achieve it in following way but unfortunately it is not filtering the data but returning all pojos. Any suggestions please.

*PojoRepository<CustomPojo, Long> customPojoRepo= getDbClient()
            .newPojoRepository(CustomPojo.class, Long.class);
PojoQueryBuilder<CustomPojo> qb = customPojoRepo.getQueryBuilder();
StructuredQueryDefinition sqdef=
             qb.and(qb.range("testDate", PojoQueryBuilder.Operator.GE, startCal.getTime()),
             qb.range("testDate", PojoQueryBuilder.Operator.LE, endCal.getTime()));
PojoPage<CustomPojo> matchingObjs =
customPojoRepo.search(sqdef,
                    start);*

Thanks, Jagan

2

There are 2 best solutions below

1
DALDEI On

Could you post the definition of CustomPojo, and a sample of the JSON doc which is stored in the db. The definition of PojoRepository (as documented) is intended for simple CRUD methods, in particular the exact serialiation (data mapping) from your Pojo to JSON is undefined. Using queries you need to know the serialized name of the field as ML does not know anything about java. Also a range index is typed, without the definition of the range index, the Pojo, the JSON and endCal.getTime() It’s not possible to detetermine which of the most likely issues you are having.

0
Sam Mefford On

I know this doesn't directly answer your question, but please note that there is a working example of doing range queries on dates in PojoFacadeTest.java which is using TimeTest as the Pojo. To support this bootstrap.xqy (which is run as a prerequisite to unit tests) creates three dateTime range indexes:

  • com.marklogic.client.test.TimeTest/calendarTest
  • com.marklogic.client.test.TimeTest/calendarTestCet
  • com.marklogic.client.test.TimeTest/dateTest