简体   繁体   中英

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

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM