I want to search documents containing a particular word in whole MarkLogic database (across elements and attributes). cts:word-query works only on elements. How can I achieve that in MarkLogic?

Example:

Document 1. <abc>Data correction query</abc>

Document 2. <element2 state="correction"></element2>

Output- If search is for word "correction" then both the above docs/URIS should be returned.

Set of possible elements-attributes containing that word is not fixed as it's a data correction exercise. Is there any possible way of extending this cts:word-query like search beyond element without knowing the attributes names?

1

There are 1 best solutions below

1
On

Attributes are not included in the universal index, so you can't just use a standard cts:word-query.

You could create a field index with an XPath for the attributes you want to target and word search options enabled. Depending upon the size of your database, it might not be a great idea to use a super-generic XPath like //*/@*, but it is possible.

Assuming you created a field called attr then you could search with a cts:field-word-query like this:

cts:search(doc(), 
  cts:or-query((
    cts:word-query("correction"), 
    cts:field-word-query("attr", "correction")
  ))
)