I want to implement a log in portal using java, neo4j REST API and Spring Framework. I am using the RestCypherQueryEngine class to send a cypher query to the server.
The query looks like -->
String query = "MATCH n WHERE n.Email = {email} AND n.Password = {pass} RETURN n;"
final QueryResult<Map<String,Object>> result = engine.query(query, Map.Util("Email", email), Map.Util("Password", pass);
.
"email" and "pass" are both Strings with the respective values.
I wanted to know if this is a valid query and can two parameters be passed like this ?
and how to know if a node has been returned or not or if the the login is authenticated. ?
Thank you.
You need to put all parameters into one map:
Map<String,Object> params = new HashMap<>();
params.put("email", email");
params.put("password", pass);
QueryResult<Map<String,Object>> result = engine.query(query,params);
NB: Query parameters are case sensitive.
Related
I have recently switched to Spring for consuming REST API calls hosted by ServiceNow.
I am building my URI as below:
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl.toString());
logger.info("URI before Query Param: " + builder.build().encode().toUri());
builder.queryParam("sysparm_limit", "2000000");
builder.queryParam("sysparm_offset", "0");
builder.queryParam("sysparm_exclude_reference_link", "true");
//this line is the issue because there is a = sign here
builder.queryParam("sysparm_query=user_name", snUser.getUser_name());
logger.info("URI after Query Param: " + builder.build().encode().toUri());
The output of this code is:
INFO: URI before Query Param: https://sandbox.service-now.com/api/now/v1/table/sys_user
INFO: URI after Query Param: https://sandbox.service-now.com/api/now/v1/table/sys_user?sysparm_limit=2000000&sysparm_offset=0&sysparm_exclude_reference_link=true&sysparm_query%3Duser_name=AX0011
The problem is with the final builder.queryParam. I am getting output as this:
sysparm_query%3Duser_name=AX0011
but what I want is:
sysparm_query=user_name=AX0011
So that eventually the final URI looks like this:
INFO: URI after Query Param: https://sandbox.service-now.com/api/now/v1/table/sys_user?sysparm_limit=2000000&sysparm_offset=0&sysparm_exclude_reference_link=true&sysparm_query=user_name=Z001NR6
So I tried replacing,
builder.queryParam("sysparm_query=user_name", snUser.getUser_name());
by:
builder.query("sysparm_query=user_name=" + snUser.getUser_name());
which changed the original output from:
INFO: URI after Query Param: https://sandbox.service-now.com/api/now/v1/table/sys_user?sysparm_limit=2000000&sysparm_offset=0&sysparm_exclude_reference_link=true&sysparm_query%3Duser_name=Z001NR6
to:
INFO: URI after Query Param: https://sandbox.service-now.com/api/now/v1/table/sys_user?sysparm_limit=2000000&sysparm_offset=0&sysparm_exclude_reference_link=true&sysparm_query=user_name%3DZ001NR6
Notice how sysparm_query%3Duser_name=Z001NR6 changed to sysparm_query=user_name%3DZ001NR6
Is ther anyway to see a = instead of %3D in the output?
The param looks quite strange - however - you can add it manually using the UriComponentsBuilder#query method:
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl("https://example.com/api/")
.queryParam("param1", "12345")
.queryParam("param2", "abc")
.query("query=username=JOE");
System.out.println(builder.build().toString());
// produces https://example.com/api/?param1=12345¶m2=abc&query=username=JOE
System.out.println(builder.build().encode().toString());
// produces https://example.com/api/?param1=12345¶m2=abc&query=username%3DJOE
Manual concatenation:
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl("https://example.com/api/")
.queryParam("param1", "12345")
.queryParam("param2", "abc");
// the parameter has to be properly url-encoded manually (not shown here)
String uri = builder.build().encode().toString() + "&query=username=JOE";
System.out.println(uri);
// produces: https://example.com/api/?param1=12345¶m2=abc&query=username=JOE
The query component of a URL is frequently used to carry information in key=value pairs; you could think of this as a Map<String, String>. In this case, = and & are special characters that delimit these pairs, and they must be encoded when they form part of the key or the value to ensure that anything reading the query string this way is able to parse it properly.
In your case, how you use the builder depends on how you would want to retrieve your data later on. There are two options:
// Building the URL:
builder.queryParam("sysparm_query=user_name", snUser.getUser_name());
// URL contains ...&sysparm_query%3Duser_name=AX0011
// Reading the parsed query map:
Map<String, String> query = ...
String data = query.get("sysparm_query=user_name");
// value is AX0011
Or
// Building the URL:
builder.queryParam("sysparm_query", "user_name=" + snUser.getUser_name());
// URL contains ...&sysparm_query=user_name%3DAX0011
// Reading the parsed query map:
Map<String, String> query = ...
String value = query.get("sysparm_query");
// value is user_name=AX0011
In a correctly encoded URL, one of the = will always be encoded as %3D. Using a UriComponentsBuilder ensures that your URLs will be correctly encoded and that anything reading your URLs will be able to do so properly without data loss.
I have q
queryString = "select?wt=json&rows=0&indent=true&facet=true&q=*:*&facet=true&facet.field=outcome_type"
If queried like :
http://x.x.x.x:8983/solr/abc/queryString
it works. here abc is a core.
Now I would like to execute it programmatically, and using the following approach :
SolrQuery query = new SolrQuery();
query.setQuery(queryString);
QueryResponse resp = server.query(query);
here queryString as defined above, but it return the following error :
Exception in thread "main"
org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException:
undefined field text
What I am missing here ? Or I need to build the query by set functions ?
I see few problems in your tentative.
You should not pass entire query string with the setQuery method. For almost each parameter available in query string there is a corresponding method in SolrQuery class.
SolrQuery does not support json format, SolrJ only supports the javabin and xml formats, I suggest to not specify any wt parameter.
So, you should use setQuery method only for q parameter:
query.setQuery("*:*");
For remaining parameters, the easiest way is use add method:
query.add("rows", "0"); // instead of setRows(0)
query.add("indent", "true");
query.add("facet", "true"); // ... setFacet(true)
query.add("facet.field", "outcome_type"); // ... addFacetField("outcome_type")
Hope this helps
I have used following approach to execute the query and it worked:
SolrQuery query = new SolrQuery();
query.setQuery(queryString);
query.setFacet(true);
query.set("wt", "json");
query.set("indent",true);
query.setRows(0);
query.addFacetField("outcome_type");
QueryResponse resp = server.query(query);
I have a problem when I want to query from my java web service into solr server. My code looks like this:
CloudSolrServer solr = new CloudSolrServer("BigDataNew1:2181,BigDataNew2:2181,BigDataNew3:2181,BigDataNew4:2181,BigDataNew5:2181/solr");
Solr Queryquery = new SolrQuery();
ModifiableSolrParams param = new ModifiableSolrParams();
param.set("q",keyword).set("fl"," id, title, desc, pubDate, media, person, location").set("count","1").set("wt", "json").set("facet", true).set("start", "0").set("rows", "5");
QueryResponse response = solr.query(param);
SolrDocumentList list = response.getResults();
I got the following error:org.apache.solr.client.solrj.SolrServerException: No collection param specified on request and no default collection has been set.
Anybody know what the problem is?
Thanks
You'll need to tell CloudSolrServer which collection you want to query.
You can do this by setting it with setDefaultCollection:
solr.setDefaultCollection("foobar");
I am new couchdb user and I am using lightcouch API.
currently, I can get data from couchdb, but what I can get is not I need. You can see the following code: username value and password value are stored, and these two values are needed to store into jsonarray.
However, the problem is when I tried to print the data, it only showing me this:
{"id":"0a3f38cfc5ca45d6bcd76725faf5b917","key":"0a3f38cfc5ca45d6bcd76725faf5b917","value":{"rev":"1-32d4a8325e8995d0eddd5b2626b752df"}}.
The problem is that I can not get username and password. BTW, another question is, when the servlet send jsonarray for example named data, how does the jquery get the message and username from data.
CouchDbProperties properties = new CouchDbProperties(
"db_test",
true,
"http",
"127.0.0.1",
5984, null, null);
CouchDbClient dbClient2 = new CouchDbClient(properties);
Map<String, Object> map = new HashMap<>();
map.put("username", "justin");
map.put("message", "hello world");
dbClient2.save(map);
List<JsonObject> jsonList = dbClient2.view("_all_docs").query(JsonObject.class);
for (int i=0; i< jsonList.size(); i++){
System.out.println(jsonList.get(i));
}
You can have "views" specified in CouchDb, in that case instead of "_all_docs" you would give your view path "_design/something/_view/someview".
More about views https://wiki.apache.org/couchdb/Introduction_to_CouchDB_views
Or look into "_all_docs?include_docs=true", since "_all_docs" does not return data itself, just documents info like ids.
I am trying to apply certain business rules for all users, except Subscription Administrators in Java. When i set the filter as below,
user.setQueryFilter(new QueryFilter("SubscriptionAdmin", "=", "false"));
I get an error like this
Could not parse: Attribute "SubscriptionAdmin" on type User is not allowed in query expressions.
How do i achieve this? Any help is appreciated.
Per WS API documentation, SubscriptionAdmin field on a User object cannot be used in queries. You may iterate over users and use an if statement with a condition:
if (userQueryObject.get("SubscriptionAdmin").getAsBoolean() == false){
//your code here;
}
SubscriptionAdmin field must be included in the fetch. Here is a code fragment where the condition is evaluated:
QueryRequest userRequest = new QueryRequest("User");
userRequest.setFetch(new Fetch("UserName", "Subscription", "DisplayName", "SubscriptionAdmin"));
userRequest.setQueryFilter(new QueryFilter("UserName", "=", "someuser#co.com"));
QueryResponse userQueryResponse = restApi.query(userRequest);
JsonArray userQueryResults = userQueryResponse.getResults();
JsonElement userQueryElement = userQueryResults.get(0);
JsonObject userQueryObject = userQueryElement.getAsJsonObject();
String userRef = userQueryObject.get("_ref").getAsString();
System.out.println(userRef);
if (userQueryObject.get("SubscriptionAdmin").getAsBoolean() == false){
System.out.println(userQueryObject.get("SubscriptionAdmin"));
}