3

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 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

hey Rob..i tried this.. but if i am searching for name=B and value=2, it should not return any result but still it returns the document..
The namespace you use should be identical to the namespace used in your XML data. If there is no namespace then you should leave it blank.
Regarding name=b and value=2, I missed in the original question that you were just looking to evaluate <yy> .. </yy> and not the entire document. Right now you return the document because name=b and value=2 both exist elsewhere in the document. The search options do not account for this. Let me think about this and get back to you.
Hi Ankit, I've updated my search options and request to retrieve the data you are looking for by adding an additional constraint and leveraging it in the GET request. Please give it a try and let me know how it goes. I tested this and it is working on my end.
2

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.

6 Comments

right ehennum.. but if i want to search for name=B and value=2 under one <yy>, then how to do it. If give query as: <yy><name>B</name><value>2</value></yy>, then also result count is 1 but thats not the case for me.
The preferred approach would be to model the document differently. The document above is the equivalent of an entire table in a relational database. Instead, the preferred document should be the equivalent of a single row in a relational table -- one yy node in the example above. That way, the indexes can return the right yy or list of yy items (which can be aggregated into a dynamic document as needed).
will try that way but due to business requirements if <yy> is a property of <xx> which can have multiple set of values..what should be the preferred approach ..considering we would be searching and doing CRUD operations on the file..
Sorry, I misunderstood. I thought the abstraction above represented a list of objects and not a list of properties for an object. The easiest way to get past this issue is to turn on filtering by adding a q:filtered property of true withing a top q:query. See docs.marklogic.com/guide/search-dev/qbe#id_59987
i tried : <q:qbe xmlns:q="marklogic.com/appservices/querybyexample"> <q:query> <q:and> <name>B</name> <value>2</value> </q:and> <q:filtered>true</q:filtered> </q:query> </q:qbe> but still i get the result..does q:filtered makes the search parameters to be looked up under same root
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.