Marklogic 8 xml search

152 views Asked by At

Suppose I have a xml as below:

<xx>
    <yy>
        <name>A</name>
        <value>1</value>
    </yy>

    <yy>
        <name>A</name>
        <value>2</value>
    </yy>

    <yy>
        <name>B</name>
        <value>1</value>
    </yy>
</xx>

Now I want to find if any 'yy' is present with name as A and value as 1. So here the matching content would be:

<yy>
    <name>A</name>
    <value>1</value>
</yy>

I am trying to do this by REST call,qbe GET request but not able to do it. Can some one help me out using:

/v1/qbe
or
/v1/search
2

There are 2 answers

4
Rob S. On

With the /v1/search API you'll need to use custom search options to achieve this.

To upload custom search options to MarkLogic: http://developer.marklogic.com/learn/rest/custom-search#search-using-an-element-value-constraint

Your search options will define constraints for your search based on the indexes you created. Your search options should look like this:

<options xmlns="http://marklogic.com/appservices/search">
  <constraint name="yy">
    <element-query name="yy" ns="" />
  </constraint>
  <constraint name="name">
    <value>
      <element ns="" name="name"/>
    </value>
  </constraint>
  <constraint name="value">
    <value>
      <element ns="" name="value"/>
    </value>
  </constraint>
</options>

Let's say you upload these options as "mySearchOptions".

Finally, you can make this GET request to get the search results you want:

http://localhost:REST_SERVER_PORT/v1/search?q=yy%3A(name%3Aa%20AND%20value%3A1)&options=mySearchOptions

6
ehennum On

Can you show the QBE you're sending?

You should be able to set the query parameter to

<yy><name>A</name><value>1</value></yy> 

with appropriate escaping, as in:

.../v1/qbe?query=%3Cyy%3E%3Cname%3EA%3C/name%3E%3Cvalue%3E1%3C/value%3E%3C/yy%3E

Because of the URL escaping issue, it's often easier to POST a QBE from an edited file.

By the way, if the goal is to bring back matching documents, there shouldn't be any reason to set up range indexes. The universal index matches documents.

Range indexes are useful for doing relational comparisons (such as <), sorting, or extracting value lists or tuples from large numbers of documents.