It looks like the QueryResponse from Solrj has no mean to give you a quoted Json string with wt=on or not. All I received is something like this
{responseHeader={status=0,QTime=2,params= {fl=id,productName,imageFront,priceEng,priceEngExp...
Question:
1) Am I missing something here ? Or there is no way to get the json response properly from the Solr server by Solrj.
2) Now on my client, if I convert the non-quoted json string from Solrj, does it mean it was done two times, once in server time and one in the Solrj client time ?
You can get JSON response by setting wt=json to the Solr query. Example URL is shown below :
localhost:8983/solr/select/?q=:&rows=10&indent=on&wt=json
You can't get JSON response using Solrj. You don't need to use Solrj for this purpose.By sending HTTP requests to the URL above, you can get json response.
With newer versions of Solr (starting with 4.7.0) it is possible to return the query response directly in json-format. This can be done with the NoOpResponseParser.
SolrQuery query = new SolrQuery();
QueryRequest req = new QueryRequest(query);
NoOpResponseParser rawJsonResponseParser = new NoOpResponseParser();
rawJsonResponseParser.setWriterType("json");
req.setResponseParser(rawJsonResponseParser);
NamedList<Object> resp = mySolrClient.request(req);
String jsonResponse = (String) resp.get("response");
System.out.println(jsonResponse );
Related
Is it possible to include cursorMark value in POST request's body instead of sending it as query string parameter?
The following query:
{"query":"val:abc","limit":10,"cursorMark":"*","sort":"id asc"}
returns an error with the message: "Unknown top-level key in JSON request : cursorMark"
According to Solr Json Request API documentation, every query string parameter has a corresponding POST request parameter in JSON API, e.g. q -> query, start -> offset, etc.
However, there is no equivalent parameter for cursorMark query string parameter.
The best solution I am aware of is changing request type from application/json to application/x-www-form-urlencoded which allows using query string parameters in POST request's body. The reason why I was using application/json was to get json response, but it turns that it is controlled by wt=json parameter.
Changed query uri to: http://localhost:8983/solr/myCore/select?wt=json
Changed POST request parameters back to query string counterparts, i.e. q, start, rows, etc.
UrlEncoded the query string.
Put the encoded query string in POST body.
Changed request content type to application/x-www-form-urlencoded.
https://solr.apache.org/guide/7_7/json-request-api.html#passing-parameters-via-json says that you can augment a JSON-based POST with non-JSON params. I got this to work in 2022 with a JSON query that includes "params": {"cursorMark": "*"}, without needing to resort to changing the request type (as suggested in the accepted answer).
My fellow co-worker backend programmer said that he has configure an API that expect to receive something like this from my mobile app:
[{"id":50},{"id":60}]
I'm using Alamofire which receive param dictionary to be sent. But I believe this is also the same mechanism using NSURLSession or any other third party plugins.
The question is: how should I construct the dictionary to send an array of ids, like how a HTTP form can have several text field with the same id, and then it will be received on the server end as an array of id? What I've tried so far and all fails:
param.setValue(50, forKey:"id");
param.setValue(60, forKey:"id");
// this only send the last single value
param.setValue([50, 60], forKey:"id");
// this results in error (415 unsupported media type)
param.setValue(50, forKey:"id[0]");
param.setValue(60, forKey:"id[1]");
// this also results in error (415 unsupported media type)
How can I send this correctly just like how web form send? Thanks.
I think keyword for your question is "Alamofire send json"
If your server accepts json data, you can do like this:
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let values = [["id":50],["id":60]]
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(values, options: [])
Alamofire.request(request)
.responseJSON { response in
// do whatever you want here
}
Good luck!
The problem with the first method is you're overwriting the value for the key id, that's the reason why it only sends the last value. Try sending the params as an array.
let dict = NSMutableArray()
param.setValue(50, forKey:"id")
dict.addObject(param)
param.setValue(60, forKey:"id")
dict.addObject(param)
Pass the dict as the parameter for the request method.
I know that the response from the pubnub history() is:
[["message","Message","message"],"Start Time Token", "End Time Token"]
im creating an string to receive the response:
String msg = response.toString();
And this should give me the full array, but now to retrieve the first message im doing this:
String[] msgOne = msg[0];
And this is not working.
for pubnub history method , the response is a org.json.JSONArray so to get the messages array you can use something like this.
JSONArray messages = (JSONArray)( ((JSONArray)response).get(0));
JSONArray class here http://www.json.org/javadoc/ provides more info about the methods that you can use on messages variable.
How can I execute a RealTime get request from the SolrJ client?
I specifically need to retrieve un-commited documents in order to check the _version_ field for optimistic concurrency.
Since the RealTime Get is implemented with an alternate requestHandler, you would just need to use the setRequestHandler() method on SolrQuery passing "/get" as the handler name.
Please see the testRealTimeGet() method in this SolrExampleTests.java file from the Solr source for a full example.
Here is the snippet from that file:
SolrQuery q = new SolrQuery();
q.setRequestHandler("/get");
q.set("id", "DOCID");
q.set("fl", "id,name,aaa:[value v=aaa]");
Given a QueryResponse object (SolrJ 3.6.2), is there any way to retrieve the query that was made to get that response other than parsing the query string?
QueryResponse exposes the Header information from which the q can be retrieved.
rsp.getHeader().get("q")
QueryResponse exposes the Header information from which the q can be retrieved. But it cant be directly retrived directly as mentioned by Jayendra.
You need to use:
response.getHeader().get("params");
This will give you a result like:
{start=0,q=apple,qf=name^10.0 description^5.0,version=2,rows=10,defType=edismax}
There you can see your result.