0

Hello MarkLogic experts,

Could you please advice how to achieve the below sample results using MarkLogic range query.

XML

<entry min="100" max="999">
<product>apple</product>
</entry>
<entry min="1" max="99">
<product>orange</product>
</entry>
</root>

Query:

Query 1 : 200   > 

Results should be: apple

Query 2 : 200   <

Results should be: apple, orange

Query 3 : 200 =>

Results should be: apple

Query 4 : 200 <=

Results should be: apple

Query 5 : 200 =

Results 4: apple

Query 6 : 150 to 250

Results should be: apple

please help some sample query for the above results, thanks

Regards,

Dinesh

1 Answers1

4

If I'm understanding your requirements correctly, for a single operator you could accept as input the value and operator (although => from your example needs to be >= in MarkLogic):

let $value := 200
let $operator := ">"
return
  cts:search(doc()/entry,
    cts:or-query((
      cts:element-attribute-range-query(
        xs:QName("entry"), xs:QName("min"), $operator, $value),
      cts:element-attribute-range-query(
        xs:QName("entry"), xs:QName("max"), $operator, $value)))

For a range:

let $begin := 150
let $end := 250
return 
  cts:search(doc()/entry,
    cts:and-query((
      cts:element-attribute-range-query(
        xs:QName("entry"), xs:QName("min"), ">=", $begin),
      cts:element-attribute-range-query(
        xs:QName("entry"), xs:QName("max"), "<=", $end)))

Of course, you'll need to build range indexes on those attribute values, or these queries will throw an exception.

wst
  • 11,681
  • 1
  • 24
  • 39
  • 3
    I addressed something like this in a couple blog posts: http://developer.marklogic.com/blog/ranged-buckets; http://developer.marklogic.com/blog/ranged-buckets-udf. The first post covers the approach wst shows here. – Dave Cassel Feb 02 '15 at 22:46
  • If you have multiple 'entry' nodes in the same document i.e you will need to wrap the above and/or querys in a near-query and enable the range-value-positions option for the indexes on min and max otherwise you may get false results e.g quering for min >=10 max <=20 will return both documents without the near query – legspeleo Feb 07 '15 at 17:36