marklogicmarklogic-9

Marklogic Java Client API criteria not working


I am looking to get all documents in a collection having a particular term present in the document. So, I am making use of the following code construct:

StringQueryDefinition sqd = queryManager.newStringDefinition();
sqd.setCollections(collectionName);
sqd.setCriteria(searchTerm);

but I am always getting the result count as 0, though if I remove the criteria, then I am getting all the documents in the collection.

What is wrong with this approach, please let me know.


Solution

  • I finally was able to find the answer with tags related to namespace constraint:

    //Create options for search:
    String options= "<search:options xmlns:search='http://marklogic.com/appservices/search'>"+
    "<search:constraint name='constraintname'>"+
    "<search:value>"+
    "<search:element name='elementname' ns='your-namespace'/>"+
    "</search:value>"+
    "</search:constraint>"+
    "</search:options>";
    
    //Write options to database
    QueryOptionsManager optionsManager = client.newServerConfigManager().newQueryOptionsManager();
    StringHandle writeHandle = new StringHandle(options);
    optionsManager.writeOptions("OPTION_NAME", writeHandle);
    
    //Create StructuredQueryBuilder with the options
    StructuredQueryBuilder queryBuilder = queryManager.newStructuredQueryBuilder("OPTIONS_NAME");
    
    //Create StructuredQueryDefinition using the query builder and specify constraint name and value.
    StructuredQueryDefinition queryDefinition = queryBuilder.and(queryBuilder.valueConstraint("constraintname", value));
    queryDefinition.setCollections(collectionName);
    
    SearchHandle searchHandle = queryManager.search(queryDefinition, new SearchHandle());