问题
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:
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:string
s. You can of course have them as xs:integer
s, and but either way they will be parsed as strings when passed in the parameter.
来源:https://stackoverflow.com/questions/30328309/marklogic-xquery-ctselement-range-query-using-variable-length-sequence-or-m