Search code examples
marklogicmarklogic-9mlcp

How to fetch a document based on a value of the particular column using QueryBatcher?


I want to fetch all the documents where the value of the status is "unprocessed".

My documents look like:

{
    "EMPID": "102", 
    "Fname": "FName2", 
    "Lname": "LastName2", 
    "DOB": "02012001", 
    "age": "19", 
    "Gender": "F", 
    "Pay": "21", 
    "status": "unprocessed"
}

URI's of these documents looks like "/test/102/FName2/"

I can able to fetch all the documents from the db using below code:

QueryManager queryManager=client.newQueryManager();
StructuredQueryBuilder sqb = queryManager.newStructuredQueryBuilder();
StructuredQueryDefinition query = sqb.directory(true, "/test/");
DataMovementManager dmm = client.newDataMovementManager();
QueryBatcher batcher = dmm.newQueryBatcher(query);
batcher.onUrisReady(
    new ExportListener()
        .onDocumentReady(doc-> {
            String uriParts[] = doc.getUri().split("/");
            try {
                Files.write(
                    Paths.get(DATA_DIR, "output", 
                              uriParts[uriParts.length - 1]), 
                    doc.getContent(new StringHandle()).toBuffer());

            } catch (Exception e) {
                e.printStackTrace();
            }
        }))
   .onQueryFailure( exception -> exception.printStackTrace() );

dmm.startJob(batcher);

Now, how can I fetch the documents whose status is unprocessed? How can I modify the listeners?


Solution

  • Use an AndQuery to combine the DirectoryQuery with a ValueQuery for the status JSON property.

    final StructuredQueryDefinition query = sqb.and(
      sqb.directory(true, "/test/"),
      sqb.value(sqb.jsonProperty("status"), "unprocessed")
    );