我试图在REST Uri中使用StructuredQuery,如记录here。
当我使用Chrome中的Postman并请求查询URI时:
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"]}} ]}}]}}}
它给了我回复。
现在当我尝试在Java中实现它时,使用HttpClient它给了我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);
错误:
HTTP/1.1 400 Bad Request [Content-type: application/xml, Server: MarkLogic, Content-Length: 364, Connection: Keep-Alive, Keep-Alive: timeout=5]
我错过了什么?
提前致谢,
大地。
答案 0 :(得分:1)
如果您打印出错误响应的正文,那么应该会提供更多信息。
也就是说,您可能需要考虑使用基于Apache HttpClient和Jersey Client构建的Java API,并为您处理HTTP交互。
答案 1 :(得分:0)
您可以尝试在查询控制台中使用search:resolve
测试结构化查询。要进行验证,您可以使用search:parse
从查询文本构造结构化查询。您可能没有在选项节点中正确定义约束,这将使您有机会对其进行测试,并使用search:check-options
确认您的选项节点已正确构建。您应该在REST服务上使用/v1/config/query/(default|{name})
来安装与默认相同的选项节点,或使用options
URL参数作为查询选项。通过这种方式,您可以确保所有已建立的约束,范围索引等,否则您将从REST界面获得“错误请求”响应。