-1

I am new to marklogic and would like to create the marklogic query in json.

I was using StructuredQueryDefinition to create query and then creating RawCombinedQueryDefinition.

StructuredQueryDefinition queryCriteria = sqb.or(query, sqb.properties(sqb.term(parameters.getQuery))));
String combinedQuery = "<search xmlns=\"http://marklogic.com/appservices/search\">"             + queryCriteria.serialize() + options + "</search>";
RawCombinedQueryDefinition rawCombinedQuery = queryMgr.newRawCombinedQueryDefinition( new StringHandle(combinedQuery));

Can I use StructuredQueryDefinition to create query in json and if yes, how do I add options to it for json?

Thanks

stackuser1
  • 55
  • 1
  • 6

1 Answers1

0

You cannot use StructuredQueryDefinition to create a query in json. However, you could use a popular XML builder to build your XML options so you don't have to manage raw XML. Or, if you'd really like your options in JSON, you could use Jackson to generate the structured query in JSON, then combine your json options.

Something like this:

ObjectMapper mapper = new ObjectMapper();
ObjectNode combinedQuery = mapper.createObjectNode();
combinedQuery.putObject("search")
  .putObject("query")
    .putArray("queries")
      .addObject()
        .putObject("or-query")
          .putArray("queries")
            .add(mapper.readTree(query))
            .addObject()
              .putObject("properties-fragment-query")
                .putObject("term-query")
                  .putArray("text")
                    .add(parameters.getQuery());
combinedQuery.with("search")
  .setAll((ObjectNode) mapper.readTree(options));
queryMgr.newRawCombinedQueryDefinition( new JacksonHandle(combinedQuery));

For posterity, I believe this question is related to this one from the same user. Perhaps something from my answer to that one will also help with this one.

Community
  • 1
  • 1
Sam Mefford
  • 2,465
  • 10
  • 15
  • thanks for the confirmation on StructuredQueryDefinition. Marklogic should provide this as enhancement in future. – stackuser1 Feb 09 '16 at 17:03