nocoder
nocoder

Reputation: 99

MarkLogic: additional-query not working while searching documents

We are trying to retrieve distinct values for a particular element across documents using element range indexes. We want the results to be restricted to documents belonging to a specific collection.

Documents are segregated into different collections logically - like "basedata" collection which holds all the application base data documents and "transactiondata" collection which holds all incoming/outgoing transactions to our application.

Now the requirement is to restrict the range index to only the basedata collection documents and not the transactiondata collection documents.

  1. Created element ranged index for : entityName

2. Wrote the below code to use element range index with search:range

String valueOptionString = 
    " <search:options xmlns:search="http://marklogic.com/appservices/search">
    <search:values name="entityName">
        <search:range type="xs:string">
            <search:element name="entityName"/>
        </search:range>
    </search:values>
    <search:additional-query>
        <cts:collection-query xmlns:cts="http://marklogic.com/cts">
            <cts:uri>basedata</cts:uri>
        </cts:collection-query>
    </search:additional-query>
</search:options> ";    

QueryManager queryMgr = client.newQueryManager();
    QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager();
    optionsMgr.writeOptions("DistinctValues", new StringHandle(valueOptionString));

    ValuesDefinition vdef = queryMgr.newValuesDefinition("entityName", "DistinctValues");
    ValuesHandle vh = queryMgr.values(vdef, new ValuesHandle());

    for (CountedDistinctValue value : vh.getValues()) {
        System.out.println("Distinct value is :: " +
            value.get("xs:string", String.class));
    }

The above code is supposed to restrict the results to only documents in basedata collection but is not working as expected

Document in basedata collection:

<?xml  version="1.0" encoding="UTF-8"?>
<entity>
    <entityName>Company</entityName>
    <createdBy>CompanyOwner</createdBy>
    <createdDate>2017-01-01T05:56:35.360Z</createdDate>
    <status>Active</status>
    <entityattributes>
        <entityattribute>
        </entityattribute>
    </entityattributes>
</entity>

Document in transactiondata collection:

<?xml  version="1.0" encoding="UTF-8"?>
<entity>
    <entityName>DistinctValueTestEntity</entityName>
    <createdBy>DistinctValuteSystemNew</createdBy>
    <createdDate>2017-01-03T05:56:35.360Z</createdDate>
    <status>Active</status>
    <entityattributes>
        <entityattribute>
        </entityattribute>
    </entityattributes>
</entity>

Output from Java code:

Company,DistinctValueteSystemNew

The above result is wrong as DistinctValueteSystemNew should not be considered as I have not included transactiondata collection in cts:uri

Upvotes: 0

Views: 115

Answers (1)

ehennum
ehennum

Reputation: 7335

The query defined in the query options applies only to a search request and not to a values request.

You can constrain a values request with a query by setting the query on the values definition using the setQueryDefinition() method:

http://docs.marklogic.com/javadoc/client/com/marklogic/client/query/ValuesDefinition.html#setQueryDefinition-com.marklogic.client.query.ValueQueryDefinition-

The query can contain a collection query. In the example:

<search:query>
  <search:collection-query>
    <search:uri>basedata</search:uri>
  </search:collection-query>
</search:query>

For more information, see:

http://docs.marklogic.com/guide/search-dev/structured-query#id_76890

Hoping that helps,

Upvotes: 1

Related Questions