Search code examples
marklogicmarklogic-8

Faceting on XML tag values


I want to do faceting on xml tags, and sub faceting on the tag values, I have an xml documents like below

<TermiteJServiceResponse>
  <EntityTypeHit type="DRUG">
    <HitCollection>
      <Hit type="DRUG" id="CHEMBL1201288">
        <Name>Dantrolene</Name>
      </Hit>
      <Hit type="DRUG" id="CHEMBL286398">
        <Name>Propylene Glycol</Name>
      </Hit>
      <Hit type="DRUG" id="GXC376D7F8C0E7A0C3787E8A2384DC56E80">
        <Name>PEG400</Name>
      </Hit>
    </HitCollection>
  </EntityTypeHit>
  <EntityTypeHit type="COMPOUNDS">
    <HitCollection>
      <Hit type="COMPOUNDS" id="A-409912.5">
        <Name>A-409912.5</Name>
      </Hit>
      <Hit type="COMPOUNDS" id="A-409912">
        <Name>A-409912</Name>
      </Hit>
    </HitCollection>
  </EntityTypeHit>
  <EntityTypeHit type="GENE">
    <HitCollection>
      <Hit type="GENE" id="TRH">
        <Name>thyrotropin-releasing hormone</Name>
      </Hit>
    </HitCollection>
  </EntityTypeHit>
  <EntityTypeHit type="BIOPROC">
    <HitCollection>
      <Hit type="BIOPROC" id="BP70302">
        <Name>infusion</Name>
      </Hit>
      <Hit type="BIOPROC" id="D009200">
        <Name>Myocardial Contraction</Name>
      </Hit>
      <Hit type="BIOPROC" id="BP70198">
        <Name>cmax values</Name>
      </Hit>
      <Hit type="BIOPROC" id="D001835">
        <Name>Body Weight</Name>
      </Hit>
      <Hit type="BIOPROC" id="D062186">
        <Name>Arterial Pressure</Name>
      </Hit>
      <Hit type="BIOPROC" id="BP70209">
        <Name>contractility</Name>
      </Hit>
      <Hit type="BIOPROC" id="D006339">
        <Name>Heart Rate</Name>
      </Hit>
      <Hit type="BIOPROC" id="BP70316">
        <Name>intravenal</Name>
      </Hit>
    </HitCollection>
  </EntityTypeHit>
  <EntityTypeHit type="SPECIES">
    <HitCollection>
      <Hit type="SPECIES" id="D051381">
        <Name>Rats</Name>
      </Hit>
    </HitCollection>
  </EntityTypeHit>

</TermiteJServiceResponse>

I would like to facet based on in above document DRUGS and sub facet on Drug names and similarly Compounds and sub facet on Compound Name


Solution

  • Take a look a the MarkLogic Search Developer's Guide on "Constrained Searches and Faceted Navigation".

    Using Search API you can define constraints and facets (a type of constraint) using the <search:options> element. For each facet you will need to define a range index. Ideally you would use semantically named elements (<DRUG> instead of <Hit type="DRUG">) for simpler indexing; however, if this schema is not flexible, then you can define a path range index over //Hit[type="DRUG"], and reference it in your search options like:

    <constraint name="Drug">
      <range type="xs:string" facet="true">
        <path-index>
          //Hit[type="DRUG"]
        </path-index>
      </range>
    </constraint>
    

    When you make a call to Search API using search:search or search:resolve, it will return a search:response element containing the results (snippets) and constraints or facet values that you define in <search:options>.