Search code examples
marklogicmarklogic-9marklogic-dhf

MarkLogic - Query for multiple input values using search API


MarkLogic version 9.0-6.2

My need is to accept input (lets say PolicyId) in an array and return all the documents in the collection matching the list of PolicyIds. An element range index is created on PolicyId and I am able to do this with below query. The code is working as expected.

const PolList = ["pol1","pol2","pol3"]
cts.search(
   cts.jsonPropertyRangeQuery("RegistrationId", "=",PolList)
)

Now, I am trying to do the same with search API (search.search). I created an options file and deployed to the modules database

{
"options": 
  {
  "search-option": "unfiltered",
  "additional-query":[
              "<collection-query xmlns='http://marklogic.com/cts'>
              <uri>registration</uri>
              </collection-query>"
              ],
  "constraint": [
              { 
                "name": "policyId",
                "range": {
                "type": "xs:string",
                "collation" : "http://marklogic.com/collation/codepoint",
                "element": {"name": "PolicyId" }
                 }
              }
         ],
"extract-document-data": 
              {
              "selected": "all"
              }
  }
}

Then I am using below code to fetch the documents.

const SearchOptions = fn.head(xdmp.invokeFunction(
     function() {
           return fn.doc("/Default/data-hub-FINAL/rest- 
 api/options/PolicyId.xml");
   },
   {
    'database': xdmp.database('data-hub-MODULES')
    }));

const result = fn.head(search.search('PolicyId:'+PolicyId, 
SearchOptions.firstChild)).xpath('search:result/search:extracted/data()', 
{'search': 'http://marklogic.com/appservices/search'});

This code working fine for a single PolicyId. How could I pass an array of PolicyIds and make it work? I want to get all the documents with a single database call.


Solution

  • I am able to solve this by using an OR operator as shown below.

    const result = fn.head(search.search('PolicyId:'+PolicyId1 +' OR ' +PolicyId2, 
    SearchOptions.firstChild)).xpath('search:result/search:extracted/data()', 
    {'search': 'http://marklogic.com/appservices/search'});
    

    I am not exactly passing a list but concatenating the list of input PolicyIds with the OR operator and able to get all matching documents back.