Search documents with parent-child context : Marklogic

192 views Asked by At

I have the following XML. When I search for city as Bangalore & state as Telegana it should not return any result since the city and state belong to different address elements...

XML Document  :
<person>
    <name>Sundeep</name>
    <address>
        <city>Bangalore</city>
        <state>Karnataka</state>
    </address>
    <address>
        <city>Hyderabad</city>
        <state>Telangana</state>
    </address>
</person>

Search for: city=Bangalore & state=Telangana

Current Output : It returns the person document when using below query

<search:and-query>
    <search:value-query>
        <search:element name="city"/>
        <search:text>Bangalore</search:text>
    </search:value-query>
    <search:value-query>
        <search:element name="state"/>
        <search:text>Telangana</search:text>            
    </search:value-query>
</search:and-query>

Excepted Output: Since both city and state belong to two different address element it should not return the above document in the result. Any pointers in solving this issue using REST-API or XQuery ?

1

There are 1 answers

6
Rob S. On

I actually just solved this problem earlier today using the /v1/search REST API here: Marklogic 8 xml search

To use xQuery for a solution this will work just fine for you:

xquery version "1.0-ml";
import module namespace search =
     "http://marklogic.com/appservices/search"
     at "/MarkLogic/appservices/search/search.xqy";

let $options :=
  <options xmlns="http://marklogic.com/appservices/search">
    <constraint name="address">
      <element-query name="address" ns="" />
    </constraint>
    <constraint name="city">
      <value>
        <element ns="" name="city"/>
      </value>
    </constraint>
    <constraint name="state">
      <value>
        <element ns="" name="state"/>
      </value>
    </constraint>
  </options>

return
  search:search('address:(city:Bangalore AND state:Karnataka)', $options)