Y.Prithvi
Y.Prithvi

Reputation: 1221

Marklogic 400 Bad Request


I trying to use StructuredQuery in REST Uri as documented here.
When I use Postman from Chrome and requesting query URI as:

http://IPADDRESS:PORT/v1/search?structuredQuery={"query":{"or-query":{"queries":[{"and-query":{"queries":[ {"word-constraint-query":{"constraint-name":"bio","text":["product"]}}, {"value-constraint-query":{"constraint-name":"company","text":["MarkLogic"]}} ]}}, {"and-query":{"queries":[ {"element-constraint-query":{"constraint-name":"spoken","and-query": {"queries":[{"term-query":{"text":["fie"]}}]} }}, {"word-constraint-query":{"constraint-name":"stagedir","text":["fall"]}}, {"value-constraint-query":{"constraint-name":"person","text":["GRUMIO"]}} ]}}, {"and-query":{"queries":[ {"properties-query":{"term-query":{"text":["fish"]}}}, {"directory-query":{"uri":["/images/2012/02/27/"]},"infinite":true} ]}}, {"and-query":{"queries":[ {"collection-query":{"uri":["mlw2012"]}}, {"term-query":{"text":["fun"]}} ]}}]}}}

It's give me response.

Now when I try to implement this in Java, using HttpClient it's giving me 400 Bad request error, Code:

CredentialsProvider credsProvider = new BasicCredentialsProvider();
query="http://192.168.192.110:8013/v1/search?structuredQuery=%7B%22query%22%3A%7B%22or-query%22%3A%7B%22queries%22%3A%5B%7B%22and-query%22%3A%7B%22queries%22%3A%5B%20%7B%22word-constraint-query%22%3A%7B%22constraint-name%22%3A%22bio%22%2C%22text%22%3A%5B%22product%22%5D%7D%7D%2C%20%7B%22value-constraint-query%22%3A%7B%22constraint-name%22%3A%22company%22%2C%22text%22%3A%5B%22MarkLogic%22%5D%7D%7D%20%5D%7D%7D%2C%20%7B%22and-query%22%3A%7B%22queries%22%3A%5B%20%7B%22element-constraint-query%22%3A%7B%22constraint-name%22%3A%22spoken%22%2C%22and-query%22%3A%20%7B%22queries%22%3A%5B%7B%22term-query%22%3A%7B%22text%22%3A%5B%22fie%22%5D%7D%7D%5D%7D%20%7D%7D%2C%20%7B%22word-constraint-query%22%3A%7B%22constraint-name%22%3A%22stagedir%22%2C%22text%22%3A%5B%22fall%22%5D%7D%7D%2C%20%7B%22value-constraint-query%22%3A%7B%22constraint-name%22%3A%22person%22%2C%22text%22%3A%5B%22GRUMIO%22%5D%7D%7D%20%5D%7D%7D%2C%20%7B%22and-query%22%3A%7B%22queries%22%3A%5B%20%7B%22properties-query%22%3A%7B%22term-query%22%3A%7B%22text%22%3A%5B%22fish%22%5D%7D%7D%7D%2C%20%7B%22directory-query%22%3A%7B%22uri%22%3A%5B%22%2Fimages%2F2012%2F02%2F27%2F%22%5D%7D%2C%22infinite%22%3Atrue%7D%20%5D%7D%7D%2C%20%7B%22and-query%22%3A%7B%22queries%22%3A%5B%20%7B%22collection-query%22%3A%7B%22uri%22%3A%5B%22mlw2012%22%5D%7D%7D%2C%20%7B%22term-query%22%3A%7B%22text%22%3A%5B%22fun%22%5D%7D%7D%20%5D%7D%7D%5D%7D%7D%7D"
    credsProvider.setCredentials(new AuthScope(IpAddress, port), new UsernamePasswordCredentials(user, password));

    DefaultHttpClient client = new DefaultHttpClient();
    client.setCredentialsProvider(credsProvider);

    HttpGet get = new HttpGet(query);
    ResponseHandler<String> handler = new BasicResponseHandler();
    HttpResponse resp = client.execute(get);

Error:

HTTP/1.1 400 Bad Request [Content-type: application/xml, Server: MarkLogic, Content-Length: 364, Connection: Keep-Alive, Keep-Alive: timeout=5]

What did I miss?

Thanks in advance,
Prithvi.

Upvotes: 1

Views: 354

Answers (2)

Shon
Shon

Reputation: 700

You might try testing your structured query using search:resolve in the query console. To verify, you could use search:parse to construct the structured query from your query text. You may not have correctly defined your constraints in your options node, and this would give you an opportunity to test it, as well as use search:check-options to confirm that your options node is properly structured. You should use /v1/config/query/(default|{name}) on your REST service to install the same options node as the default or as a query option using the options URL param. This way you can make sure you have your constraints, range indexes, etc. all established, or else you will get the "Bad Request" response from the REST interface.

Upvotes: 0

ehennum
ehennum

Reputation: 7335

If you print out the body of the error response, that should give you more information.

That said, you might want to consider using the Java API, which is built on top of Apache HttpClient and Jersey Client and handles the HTTP interactions for you.

Upvotes: 1

Related Questions