CrazyNerd
CrazyNerd

Reputation: 63

Want to search all marklogic documents with a particular value for a tag

I want to search all the MarkLogic documents having "Sample" as the value for the element "tagname" with the attribute "attr" and the value "attr" using the Java API

<tagname attr="attr">Sample</tagname>

Upvotes: 1

Views: 234

Answers (2)

Mads Hansen
Mads Hansen

Reputation: 66781

With the Java API, you can use a containerQuery() to match substructures contained within the element "tagname", and then use and() with a value() constraint for ElementAttribute for the tagname/@attr="attr" and another value() constraint for the Element tagname with the value "Sample".

// create the client
DatabaseClient client = 
    DatabaseClientFactory.newClient(host, port, user, password, authType);
// create a manager for searching
QueryManager queryMgr = client.newQueryManager();    
// create a query builder
StructuredQueryBuilder qb = new StructuredQueryBuilder();

// build a search definition
StructuredQueryDefinition query = 
  qb.containerQuery(
    qb.element("tagname"),
    qb.and(
      qb.value(
        qb.elementAttribute(
          qb.element("tagname"),
          qb.attribute("attr")
        ),
        "attr"),
      qb.value(
        qb.element("tagname"),
        "Sample"
      )
    )
  );

// run the search
queryMgr.search(query, resultsHandle);

Upvotes: 3

Tyler Replogle
Tyler Replogle

Reputation: 1339

What you are looking for is cts:element-attribute-value-query.

You will want to wrap this in a cts:search and limit it. Depending on what you want you might have to "and" it with a element value query if you care about the value of the element and the attribute.

If you are going to be doing a lot of querying in MarkLogic you might want to check out the search developer guide at https://docs.marklogic.com/guide/search-dev

Upvotes: 1

Related Questions