Search code examples
marklogicmarklogic-9marklogic-dhf

MarkLogic : Apply 'AND' query on collections using options file


We have bitemporal enabled on customer and billing collections and have a customer '123456' in the latest collection of both customer and billing collections. We are trying to use an option file to pull the LATEST document of the customer from the 'customer' collection.

Below is the options file we coded.

{
"options": 
  {
  "search-option": "unfiltered",
  "additional-query":[
          "<collection-query xmlns='http://marklogic.com/cts'>
          <uri>customer</uri>
          </collection-query>"
          ],
  "additional-query":[
          "<collection-query xmlns='http://marklogic.com/cts'>
          <uri>latest</uri>
          </collection-query>"
          ],
  "constraint": [
          { 
          "name": "CustomerId",
          "range": 
                         {
          "type": "xs:string",
          "collation" : "http://marklogic.com/collation/codepoint",
          "element": {"name": "CustomerId" }
                         }
          }
          ],
"extract-document-data": 
          {
          "selected": "exclude",
          "extract-path": [ "/envelope/instance/Customer" ]
          }
  }
}

When we try below code, the documents are still coming from both customer and billing collections.

fn.head(search.search('CustomerId:123456', 
SearchOptions.firstChild)).xpath('search:result/search:extracted/data()', 
{'search': 'http://marklogic.com/appservices/search'});

SearchOptions in above query is the options file.

Our understanding is that 'additional-query' would apply AND condition, but looks like there is some gap in our understanding.

How can I apply AND condition in options file to pull the LATEST document from 'customer' collection only.

Thanks in advance!


Solution

  • You can use:

    "additional-query":[
          "<collection-query xmlns='http://marklogic.com/cts'>
          <uri>customer</uri>
          </collection-query>",
    
          "<collection-query xmlns='http://marklogic.com/cts'>
          <uri>latest</uri>
          </collection-query>"
          ],
    

    They are wrapped in an and-query by default (at top-level).

    HTH!