nocoder
nocoder

Reputation: 99

Marklogic- Retrieve distinct values of an XML element across documents

We are building a enterprise application with ML as the back end DB. One of the requirements is to find distinct values of a particular element in XML across various documents.

Can I get the distinct entity_type using a structured query/combined query

All the below documents are part of entity collection:

Document 1

<xml>
    <entities>
        <entity_name>Imprezza</entity_name>
        <entity_type>Car</entity_type>
        <entity_color>Red</entity_color>
    <entities>
<xml>

Document 2

<xml>
    <entities>
        <entity_name>Ducati</entity_name>
        <entity_type>Bike</entity_type>
        <entity_color>White</entity_color>
    <entities>
<xml>

Document 3

<xml>
    <entities>
        <entity_name>Lancia</entity_name>
        <entity_type>Car</entity_type>
        <entity_color>Red</entity_color>
    <entities>
<xml>

Document 4

<xml>
    <entities>
        <entity_name>Scania</entity_name>
        <entity_type>Truck</entity_type>
        <entity_color>Black</entity_color>
    <entities>
<xml>

Requirement: Give me all distinct entity_type in the collection

Result: Car,Bike,Truck

P.S: Need to get the result in Java layer. We currently have an API which generates MarkLogic combined queries (Structured query + options query) based on input. Can I get the distinct entities using a structured query/combined query ??

Upvotes: 1

Views: 378

Answers (1)

You can do this with a lexicon (based on no spaces above) https://docs.marklogic.com/guide/search-dev/lexicon

OR

You can do this with a range index on each of the values. https://docs.marklogic.com/guide/admin/range_index

In either case, you can retrieve the values via the values REST endpoint. https://docs.marklogic.com/REST/GET/v1/values/[name]

I believe that there are java API solutions once you have your lexicons or range index in place as well.

Upvotes: 5

Related Questions