Hlib Babii
Hlib Babii

Reputation: 680

Range Structured Query using Client Java API in MarkLogic

Having created the following Element Range Index: scalar type: unsignedLong, localname: number,

I am running the following code:

    String options =
            "<search:options " +
                    "xmlns:search='http://marklogic.com/appservices/search'>" +
                "<search:constraint name='number'>" +
                    "<search:range type='xs:unsignedLong'>" +
                        "<search:element name='number' ns=''/>" +
                    "</search:range>" +
                "</search:constraint>" +
            "</search:options>";

    databaseClient.newServerConfigManager()
        .newQueryOptionsManager().writeOptions("myopt", new StringHandle(options));

    JSONDocumentManager jsonDocumentManager = 
                                     databaseClient.newJSONDocumentManager();

    for (int i = 0; i < 10; i++) {
        jsonDocumentManager.write("/somepath/"+ i +".json", 
                                 new StringHandle("{\"number\": \""+i+"\"}"));
    }


    QueryDefinition queryDefinition = new StructuredQueryBuilder()
        .rangeConstraint("number", StructuredQueryBuilder.Operator.GT, "5");
    queryDefinition.setOptionsName("myopt");

    StringHandle searchHandle = databaseClient.newQueryManager()
        .search(queryDefinition, new StringHandle());
    System.out.println(searchHandle.get());

I hoped to get the documents with numbers greater than 5, but I am getting empty search result. Maybe I am missing something?

I'm using MarkLogic server 7.0, client-api-java 2.0.5.

Thanks, Hlib

UPDATE

I'm Running the similar code on another machine, it works fine. Trying to find out the difference now

Upvotes: 0

Views: 249

Answers (1)

ehennum
ehennum

Reputation: 7335

Is it possible that the environment where that code works is running MarkLogic 8?

On MarkLogic 7, JSON is persisted as XML in a special namespace. You define the range index with that namespace:

http://docs.marklogic.com/7.0/guide/rest-dev/search#id_95526

Then, specify the range constraint with json-key instead of element:

<search:json-key>number</search:json-key>

In MarkLogic 8, JSON is persisted as JSON, so you don't define the index with the namespace and you use json-property instead of json-key.

Hoping that helps,

Upvotes: 1

Related Questions