marklogicmarklogic-8marklogic-9

How to use predicate in path range index


My requirement is to create Path Range Index in MarkLogic with predicate.

Take this XML -

<class>
       <student rollno = "393">
          <firstname>shivling</firstname>
          <lastname>Bhandare</lastname>
          <marks>85</marks>
       </student>
       <student rollno = "493">
          <firstname>abc</firstname>
          <lastname>pqr</lastname>
          <marks>95</marks>
       </student>
  </class>

here I want to create path range index for xpath with predicate /class/student[marks gt 80]/firstname.

can we achieve this using Path Range Index?


Solution

  • The short answer is yes, you can create a path index like that. However, step back a bit. What are you hoping to gain from creating such an index? You can create a path field with that path, which means you can then search for first names of students with high scores. You can put a range index on that, which means you can get the firstnames of students with high scores. Then if you wanted the same for students with low scores you'd have to make another field. And having the firstname in hand doesn't make accessing the complete student any more performant: you'd still need to pull the whole class every time.

    Most likely you really want to create a cleaner 1-object/1-document data model if you need to ask questions about individual students. Then you don't need a special path to get at high scoring students, you just need a range index on scores and you can do it as a simple query, and pluck what you want out of the results, e.g.

    cts:search(/student, cts:element-range-query(xs:QName("marks"), ">", 80) )/firstname