Search code examples
marklogic

MarkLogic node.js - is it possible to support a derived value in parseBindings?


We have a search application using MarkLogic node.js. We use parsedQuery like this:

            qb.parsedFrom(prop.search, 
            qb.parseBindings(
                qb.word('name', qb.bind('name')),
                qb.word('birthdate', qb.bind('birthdate')),
                qb.range('count', qb.datatype('float'), qb.bind('count'))
            )
        )

The above currently supports search syntax like "count GT 50", etc. We would like to support searching using a derived value such as age. That is, we want to support a search syntax like "age GT 10", where the age value is not stored in the documents in the database but rather needs to be computed from the birthdate on the fly. We can't store the age in the documents since the age changes depending on the current date.

Is this possible and if so, how? If it matters, we are using ML8


Solution

  • The way to do this is with a custom constraint. The parse function constructs a cts query based on the parameters. To support something like "age GT 10", you'd want to build something like

    cts:element-range-query(
      xs:QName("date-of-birth"), "<=", 
      fn:current-date() - xs:yearMonthDuration("P10Y")
    )
    

    You'll need a date range index on date-of-birth.

    Once you've build that custom constraint, you can call it from the MarkLogic Node.js Client API. See Using a Custom Constraint Parser. There's an example in the Node Developer's Guide, too.