Hi Sam,
I have a little problem which I am unable to fix. The above code works for
Standalone java application.
Now , I am trying to put this code inside a J2EE spring based application
inside a method below
public ValuesHandle getTopTen(){
// construct the query options
String optionXml = "<search:options "+
"xmlns:search='http://marklogic.com/appservices/search'>"+
"<search:values name='country'>"+
"<search:values-option>frequency-order</search:values-option>"+
"<search:values-option>limit=5</search:values-option>"+
"<search:range collation='http://marklogic.com/collation/'
type='xs:string'>"+
"<search:element ns='' name='country'/>"+
"</search:range>"+
"</search:values>"+
"</search:options>";
// create a handle to send the query options
StringHandle writeHandle = new StringHandle(optionXml);
DatabaseClient client = conn.getClient(); //conn is the
connection object already created
QueryOptionsManager optionsMgr =
client.newServerConfigManager().newQueryOptionsManager();
// write the query options to the database
optionsMgr.writeOptions("myOptions", writeHandle);
QueryManager queryMgr = client.newQueryManager();
ValuesDefinition query =
queryMgr.newValuesDefinition("country", "myOptions");
ValuesHandle values = queryMgr.values(query, new
ValuesHandle());
return values;
}
But while trying to call this method from my controller , I am getting this
error
com.marklogic.client.FailedRequestException: Local message: /config/query
write failed: Precondition Failed. Server Message: RESTAPI-EMPTYBODY
(err:FOER0000): Empty POST or PUT body
at
com.marklogic.client.impl.JerseyServices.putPostValueImpl(JerseyServices.java:1655)
at
com.marklogic.client.impl.JerseyServices.putValue(JerseyServices.java:1558)
at
com.marklogic.client.impl.QueryOptionsManagerImpl.writeOptions(QueryOptionsManagerImpl.java:97)
at com.marklogic.training.Search.getTopTen(Search.java:388)
This line of code is failing while debugging
optionsMgr.writeOptions("myOptions", writeHandle);
How to write the options in a web application , looks like it is calling a
REST api and it is not in a proper format.
Can you please look into this issue, the same code works for standalone
java application
Regards and Thanks
NS
On Tue, Feb 24, 2015 at 12:18 AM, Sam Mefford <[email protected]>
wrote:
> Ah well, my last reply was redundant as you seem to have found the
> answer yourself. Good on you!
>
> Here's an update with a working sample. Notice I changed
> search:constraint to search:values. And notice I pass the options name
> to the newValuesDefintion method.
>
> import com.marklogic.client.DatabaseClientFactory;
> import static
> com.marklogic.client.DatabaseClientFactory.Authentication.DIGEST;
> import com.marklogic.client.DatabaseClient;
> import com.marklogic.client.admin.QueryOptionsManager;
> import com.marklogic.client.query.CountedDistinctValue;
> import com.marklogic.client.query.QueryManager;
> import com.marklogic.client.query.ValuesDefinition;
> import com.marklogic.client.io.StringHandle;
> import com.marklogic.client.io.ValuesHandle;
>
> public class Test {
> public static void main(String[] args) {
> DatabaseClient client = DatabaseClientFactory.newClient("localhost",
> 8000, "admin", "admin", DIGEST);
>
> QueryOptionsManager optionsMgr =
> client.newServerConfigManager().newQueryOptionsManager();
>
> // construct the query options
> String optionXml =
> "<search:options "+
>
> "xmlns:search='http://marklogic.com/appservices/search'>"+
> "<search:values name='country'>"+
> "<search:range
> collation='http://marklogic.com/collation/' type='xs:string' facet='true'>"+
>
> "<search:facet-option>frequency-order</search:facet-option>"+
>
> "<search:facet-option>descending</search:facet-option>"+
> "<search:facet-option>limit=10</search:facet-option>"+
> "<search:element ns='' name='country'/>"+
> "</search:range>"+
> " </search:values>"+
> "</search:options>";
>
> // create a handle to send the query options
> StringHandle writeHandle = new StringHandle(optionXml);
>
> // write the query options to the database
> optionsMgr.writeOptions("myOptions", writeHandle);
> QueryManager queryMgr = client.newQueryManager();
> ValuesDefinition query = queryMgr.newValuesDefinition("country",
> "myOptions");
> ValuesHandle values = queryMgr.values(query, new ValuesHandle());
> for (CountedDistinctValue value : values.getValues() ) {
> String textValue = value.get("xs:string", String.class);
> System.out.println(textValue + " " + value.getCount());
> }
> }
> }
>
>
> Sam Mefford
> Senior Engineer
> MarkLogic [email protected]
> Cell: +1 801 706 9731www.marklogic.com
>
> This e-mail and any accompanying attachments are confidential. The
> information is intended
> solely for the use of the individual to whom it is addressed. Any review,
> disclosure, copying,
> distribution, or use of this e-mail communication by others is strictly
> prohibited. If you
> are not the intended recipient, please notify us immediately by returning
> this message to
> the sender and delete all copies. Thank you for your cooperation.
>
> On 2/22/2015 10:58 PM, Maisnam Ns wrote:
>
> Hi,
>
> Can someone help me on creating the query options , the first one using
>
> 1. QueryOptionsBuilder in Java API works but with xml it is not working,
> the result I am getting is
>
> 1. US 100
> 2. JP 49
> 3. ES 23
>
> Basically , I am getting the country and the counts .
>
> QueryOptionsBuilder qob = new QueryOptionsBuilder();
>
> // expose the "SPEAKER" element range index as "speaker" values
> QueryOptionsHandle options = new QueryOptionsHandle().withValues(
> qob.values("country",
> qob.range(
> qob.elementRangeIndex(new QName("country"),
>
> qob.stringRangeType(QueryOptions.DEFAULT_COLLATION))),
> "frequency-order"));
>
>
> 2. QueryOptionsManager optionsMgr =
> client.newServerConfigManager().newQueryOptionsManager();
>
> // construct the query options
> String optionXml =
> "<search:options "+
> "xmlns:search='
> http://marklogic.com/appservices/search'>"+
> "<search:constraint name='country'>"+
> "<search:range collation='
> http://marklogic.com/collation/' type='xs:string' facet='true'>"+
> "<search:facet-option>frequency-order</
> search:facet-option>"+
> "<search:facet-option>descending</
> search:facet-option>"+
> "<search:facet-option>limit=10</
> search:facet-option>"+
> "<search:element ns='' name='country'/>"+
> "</search:range>"+
> " </search:constraint>"+
> "</search:options>";
>
> // create a handle to send the query options
> StringHandle writeHandle = new StringHandle(optionXml);
>
> // write the query options to the database
> optionsMgr.writeOptions(OPTIONS_NAME, writeHandle);
> QueryManager queryMgr = client.newQueryManager();
>
>
> _______________________________________________
> General mailing
> [email protected]http://developer.marklogic.com/mailman/listinfo/general
>
>
>
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general
>
>
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general