0

I have a collection of 100,000 records structured like this:

<record>
    <pk>1</pk>
    <id>1234</id>
</record>
<record>
    <pk>2</pk>
    <id>1234</id>
</record>
<record>
    <pk>3</pk>
    <id>5678</id>
</record>
<record>
    <pk>4</pk>
    <id>5678</id>
</record>

I have setup a range index on id.

I want to write a query in XQuery that will allow me to pass in a variable length sequence or map of id's and get back out all the records with those id's.

It needs to be such that I can pass in any number of id's. Also, it needs to take advantage of the range index (as in fast).

1
  • Are those records all in one document, or is it one record per document? Simple problem if the latter. If the former, is your goal to find the pk, or do you need the whole record? Commented May 19, 2015 at 16:18

1 Answer 1

1

CTS searches are your friend. They will use your indexes.
You have a few different options with your CTS search. My first thought is to try something like this:

let $ids as xs:string* := (1, 2, 4, 5, 6, 33, 35.....89)
let $results as element(record)* := cts:search(/record,
    cts:element-value-query(xs:QName("id"), $ids, ("exact"))
    )
return $results
(: This will return all the record elements with their children :)

The documentation shows that you can specify a sequence as the second parameter in your cts:element-value-query, and your sequence by very nature is variable length. When your sequence is passed into that cts:element-value-query, it is as if to say "ID==3 OR ID==9 OR ID==13", etc.

http://docs.marklogic.com/7.0/cts:element-value-query

You can also use an element range query: http://docs.marklogic.com/7.0/cts:element-range-query, though I'm not as good with those so I can't give you very much help on that.

Note that in my code above, I have put the ids as xs:strings. You can of course have them as xs:integers, and but either way they will be parsed as strings when passed in the parameter.

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

Comments

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.