Search code examples
xmlxqueryindexingmarklogic

How to create element range index in Marklogic?


I have the following xml:-

<?xml version="1.0" encoding="UTF-8"?>
<patent-assignment>
  <assignment-record>
    <correspondent>
      <name>NORTH AMERICA INTERNATIONAL PATENT OFFIC</name>
      <address-1>P.O. BOX 506</address-1>
      <address-2>MERRIFIELD, VA 22116</address-2>
    </correspondent>
   </assignment-record>
  <patent-assignors>
    <patent-assignor>
      <name>TSAI, YU-WEN</name>
      <execution-date>
    <date>20050331</date>
      </execution-date>
    </patent-assignor>
    <patent-assignor>
      <name>HUANG, CHENG-I</name>
      <execution-date>
    <date>20050331</date>
      </execution-date>
    </patent-assignor>
  </patent-assignors>
  <patent-assignees>
    <patent-assignee>
      <name>FARADAY TECHNOLOGY CORP.</name>
      <address-1>NO.10-2, LI-HSIN ROAD 1, SCIENCE-BASED INDUSTRIAL PARK</address-1>
      <city>HSIN-CHU CITY</city>
      <country-name>TAIWAN</country-name>
    </patent-assignee>
  </patent-assignees>
 </patent-assignment>

Now I want to create range element indexes on names of patent-Assignor and patent-Assignee. But in Marklogic there is no option to specify XPath for range indexes. It will just take the index name as "name". So what will be the proper way to create element range indexes on names of patent-Assignor and patent-Assignee ?


Solution

  • Puneet, in order to get just one set of names, MarkLogic needs to be able to distinguish between the sets somehow. Your best bet is to change the name element's localname (currently "name") or namespace (currently none) during ingest. After doing so, you can build an element-range-index and use cts:element-values(). For instance:

    <?xml version="1.0" encoding="UTF-8"?>
    <patent-assignment>
      <assignment-record>
        <correspondent>
          <name>NORTH AMERICA INTERNATIONAL PATENT OFFIC</name>
          <address-1>P.O. BOX 506</address-1>
          <address-2>MERRIFIELD, VA 22116</address-2>
        </correspondent>
      </assignment-record>
      <patent-assignors xmlns="http://puneet/assignors">
        <patent-assignor>
          <name>TSAI, YU-WEN</name>
          <execution-date>
            <date>20050331</date>
          </execution-date>
        </patent-assignor>
        <patent-assignor>
          <name>HUANG, CHENG-I</name>
          <execution-date>
            <date>20050331</date>
          </execution-date>
        </patent-assignor>
      </patent-assignors>
      <patent-assignees xmlns="http://puneet/assignees">
        <patent-assignee>
          <name>FARADAY TECHNOLOGY CORP.</name>
          <address-1>NO.10-2, LI-HSIN ROAD 1, SCIENCE-BASED INDUSTRIAL PARK</address-1>
          <city>HSIN-CHU CITY</city>
          <country-name>TAIWAN</country-name>
        </patent-assignee>
      </patent-assignees>
    </patent-assignment>
    

    From this XML, you can build a range index on each of the "name" elements, then call

    cts:element-values(fn:QName("http://puneet/assignees", "name"))
    

    to get the assignee names.