Rakesh .p
Rakesh .p

Reputation: 79

Facets in marklogic using java

How to get Facets Information from MarkLogic using java API? I tried to figure our by referring to some docs but was unable to get the solution. Please help in finding the solution.

I made an element range Index on json Property named "integerQuery" Element range index on "integerQuery"

Now trying to get faceted values and names from the code.

  DatabaseClient client=DatabaseClientFactory.newClient("10.53.195.198",6010,"nosql",new 
     DigestAuthContext("admin","admin"));   

    QueryManager queryManager=client.newQueryManager();

    StructuredQueryBuilder queryBuilder=queryManager.newStructuredQueryBuilder();

 queryBuilder.jsonProperty("integerQuery");
 StructuredQueryDefinition def=queryBuilder.or();
   SearchHandle handle= queryManager.search(def,new SearchHandle());
 System.out.println(handle.getFacetResult("integerQuery"));  //Prints NULL

Tried using query options with QueryOptionsBuilder but the class was removed in the updated MarkLogic java version.

Can anyone suggest an answer detailed description of the faceted values from element range index ?

I tried to learn from the intro course provided by them but it still uses QueryOptionsBuilder class.

my json document in database.

{
  "Name": "Flipkart", 
  "integerQuery": 7
}

Upvotes: 0

Views: 164

Answers (1)

Vivek Siddharthan
Vivek Siddharthan

Reputation: 204

You need to define the Options yourself for facets. Then, use the StructuredQueryDefinition built from the StructuredQueryBuilder together with the facet options and pass it on to RawCombinedQueryDefinition. A sample example would be:

// Build the StructuredQueryDefinition 
// from StructuredQueryBuilder
StructuredQueryDefinition builtSQ = queryBuilder.term("one");
String XML_OPTIONS = 
     "<options xmlns=\"http://marklogic.com/appservices/search\">" +
        " <constraint name=\"artist\"> " +
        " <range type = \"xs:string\" " +
               " collation = \"http://marklogic.com/collation/en/S1/AS/T00BB\"> " + 
        " <element ns = \"http://marklogic.com/MLU/top-songs\" name=\"artist\"/>" + 
        " <facet-option>descending</facet=option> " +
        " <facet-option>limit=30</facet=option> " +
     "</options>";
// Create a QueryManager
QueryManager qm = client.newQueryManager();
StructuredWriteHandle queryHandle = new StringHandle().with(
        "<search xmlns=\"http://marklogic.com/appservices/search\">" +
            builtSQ.serialize() + 
            XML_OPTIONS +
        "</search>").withFormat(Format.XML)
RawCombinedQueryDefinition query = qm.newRawCombinedQueryDefinition(queryHandle);
// Perform the search
SearchHandle results = qm.search(query, new SearchHandle());

The above XML_OPTIONS are for building a facet based on range index on element "artist". For more information on how to build the facet options, please go through the following links.

Generating Search Facets

Constrained Searches and Faceted Navigation

Constraint Options

Facet Options

For information about the Combined Query please see this link

Hope this helps!

Upvotes: 3

Related Questions