Elasticsearch Java API query [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I`m newbie with elasticsearch querybuilder, Could someone give a constructed query for this below one in Java API
curl -XGET "http://localhost:9200/mone/mone/_search?pretty=true" -d'
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "ABC",
"fields": ["Data.Type"]
}
},
"filter": {
"term": { "Data.Date": "01.06.2014" }
}
}
}
}'

Using FilterQueryBuilder I got it to work
FilteredQueryBuilder builder = QueryBuilders.filteredQuery(QueryBuilders.queryString("Spectra"), FilterBuilders.termFilter("Data.Date", "01.06.2014"));
SearchResponse response = elasticClient.prepareSearch("mone")
.setTypes("mone")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(builder)
.execute()
.actionGet();
System.out.println(response);
Hope this answer will be useful to some newbies like me.

Related

Find way to know content of text message generated from api [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 days ago.
Improve this question
We have 2 APIS one to generate SMS other to retrieve and post content of SMS.
http://54.68.219.97:8086//TigoWapPromotion/OmanTelServlet?product=29&service=27465&PID=test
To generate OTP
http://54.68.219.97:8086/IntegrationMServices/Generate_OTP/api/{msisdn}/{PromoID}/{PartnerID}/{TransactionID}
Response:
SUCCESS -
{
"Code": 0,
"Message": "Success",
"TransactionID": "190822141841961ZOTHERZ209Z0Z29224"
}
OR ERROR -
{
"Code": 99,
"Message": "System Error",
"TransactionID": ""
}
To Validate OTP
http://54.68.219.97:8086/IntegrationMServices/OTPValidate/api/{msisdn}/{OTP}/{TransactionID}
Response:
SUCCESS -
{
"Code": 0,
"Message": "Success",
"TransactionID": "190822141841961ZOTHERZ209Z0Z29224"
}
OR ERROR -
{
"Code": 1,
"Message": "PIN Error",
"TransactionID": ""
}

java - List remove specific object base on object property [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Supposed I have a list of objects
[
Post(postid=1, title=testtitle1, post=test, created_at=null, updated_at=null),
Post(postid=2, title=testtitle2, post=test, created_at=null, updated_at=null),
Post(postid=4, title=testtitle4, post=test, created_at=null, updated_at=null),
]
and I have an incoming request like this:
[
Post(postid=4, title=zzzzzzz, post=testpost, created_at=null, updated_at=null)
]
How can I replace the old post with post id 4
Post(postid=4, title=testtitle4, post=test, created_at=null, updated_at=null),
with the new request
[
Post(postid=4, title=zzzzzzz, post=testpost, created_at=null, updated_at=null)
]
so that the list will become like this
[
Post(postid=1, title=testtitle1, post=test, created_at=null, updated_at=null),
Post(postid=2, title=testtitle2, post=test, created_at=null, updated_at=null),
Post(postid=4, title=zzzzzzz, post=testpost, created_at=null, updated_at=null)
]
There are many ways, one of them is :
posts.removeIf(p -> Objects.equals(newPost().getId(), p.getId()));
posts.add(newPost);
I recommend you to use Map data structure for your usecase, instead of using list:
Write code like below:
public class PostBase {
private Map<Integer,Post> posts = new HashMap();
public void addPost(Post post) {
posts.put(post.getPostid(), post);
}
public Post getPost(int id){
return posts.get(id);
}
}
If you still want to use list, you can remove existing element by find element by id and add new one like below(similar to answered by YCF_L):
Post newpost = new Post(4, "xxx", "tt", null, null);
postlist.removeIf((post)->post.getPostid() == newpost.getPostid());
postlist.add(newpost);
Just do,
for(Post post : posts) {
if(idOfIncomingRequest.equals(post.postid)) {
// update your data here
}
}

Access sub environment variable in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have the following environment variable:
$ printenv
...
VCAP_SERVICES={"mariadbent":[{
"label": "mariadbent",
"provider": null,
"plan": "usage",
"name": "stackoverflow-database",
"tags": [
"mariadb",
"mysql"
],
"instance_name": "stackoverflow-database",
"binding_name": null,
"credentials": {
"host": "some-url-to-the-database.service",
"hostname": "some-url-to-the-database.service",
"port": 7689,
"name": "JDFJHDJF_DFJKDHFUD_DFUZDKFJDKJF",
"database": "JDFJHDJF_DFJKDHFUD_DFUZDKFJDKJF",
"username": "hsdfhsjkfhsjkhfjk",
"password": "iuzwerhsdjkfjkasd",
"database_uri": "mysql://dfdfdfdfdf:jrb4j4QxzgbAcfLk#some-url-to-the-database.service:3306/JDFJHDJF_DFJKDHFUD_DFUZDKFJDKJF?reconnect=true",
"uri": "mysql://dfdfdfdfdf:jrb4j4QxzgbAcfLk#some-url-to-the-database.service:3306/JDFJHDJF_DFJKDHFUD_DFUZDKFJDKJF?reconnect=true",
"jdbcUrl": "jdbc:mysql://some-url-to-the-database.service:3306/JDFJHDJF_DFJKDHFUD_DFUZDKFJDKJF?user=dfdfdfdfdf&password=jrb4j4QxzgbAcfLk"
},
"syslog_drain_url": null,
"volume_mounts": [
]
}]}
I can get the whole "pack" of data with System.out.println("VCAP_SERVICES: " + System.getenv("VCAP_SERVICES"));, but I would like to extract some field in the above output, like the username.
How could I do that?
Your VCAP_SERVICE hold an json. You can use an json parser to get a value from it.
Here is an example using Jackson, but there a more libs which can do this.
try{
String json = System.getenv("VCAP_SERVICES"); //NullPointerException, SecurityException
JsonNode jsonNode = (new ObjectMapper()).readTree(json); //IOException
if(jsonNode.has("mariadbent") && jsonNode.get("mariadbent").isArray()){
for(JsonNode elem : jsonNode.get("mariadbent")){
if(elem.has("credentials")){
JsonNode cred = elem.get("credentials");
if(cred.has("host")){
System.out.println(
cred.get("host").asText() //some-url-to-the-database.service
);
}else{ System.out.println("no host"); }
}else{ System.out.println("no credentials"); }
}
}else{ System.out.println("no mariadbent or not array"); }
}catch(Exception e){ e.printStackTrace(); }
For this you need lib Jackson Databind: https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.9.9.3

creating JSON format output [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to write a program which can create an output in JSON format, how would be best way of doing this? and programming languages?
This is an example output of JSON (expected output) which I need to input in the Name, Gender, Qualification and other attributes in a user friendly way during the execution of script. And in which outputs in following JSON format. Sorry, I am new in programming, but so much interested to learn Perl (or) Python (or) Java. What could be the best here?
Any suggestions?
P.S Sorry I am quite new to JSON as well, please apologize me for this basic one.
[
{
"Name":"Steven Mark",
"gender":"male",
"Qualification": {
"college":"Bachelor in Science",
"tech":"certified pro"
},
"contributions": [
{
"name":"biography",
"type":"book",
},
]
},
{
"Name":"Andrea Mark",
"Gender":"female",
"Qualifications": {
"college":"Bachelor in physics",
},
"contributions": [
{
"name":"my insights",
"type":"movie",
},
]
}
]
Virtually every language has a JSON library, including Perl.
use JSON;
my $data = [
{
"Name" => "Steven Mark",
"gender" => "male",
"Qualification" => {
"college" => "Bachelor in Science",
"tech" => "certified pro"
},
"contributions" => [
{
"name" => "biography",
"type" => "book",
},
]
},
{
"Name" => "Andrea Mark",
"Gender" => "female",
"Qualifications" => {
"college" => "Bachelor in physics",
},
"contributions" => [
{
"name" => "my insights",
"type" => "movie",
},
]
}
];
print(encode_json($data));
If you agree to use ANY programming language, i can suggest python. With its json lib you can do following (lines with # is comments):
# import lib
import json
# fill data into variable (this is list with dict objects inside):
data = [{"name":"john"},{"name": "bill"}]
# dump json
json.dumps(data)
Which will output your data as json.
You can start writing python using something from https://wiki.python.org/moin/BeginnersGuide
If you are going to use Python, you can try to use simplejson or json module to create a json object.
For example,
try:
import simplejson
except:
import json
data = dict(a=1,b=2)
with open("results.json", "w") as fp:
json.dump(data, fp, indent=3, encoding="utf-8")
For dumping, json is faster than simplejson (but not by an order of magnitude). For loading, simplejson is faster (but not by an order of magnitude).
You can check here for more detailed comparison between simplejson and json.

How to create a query for embedded object in Mongo? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Any one help me to create mongo query for deleting where "name" : "gdfgdfgdfg" embedded document
The object stored as below in Mongo db.
{
"_id": ObjectId("50656f33a4e82d3f98291eff"),
"description": "gdfgdfgdfg",
"menus": [
{
"name": "gdfgdfgdfg"**,
"description": "dfgdgd",
"text": "dfgdfg",
"key": "2",
"onSelect": "yyy",
"_id": ObjectId("50656f3ca4e82d3f98291f00")
},
{
"name": "dfg",
"description": "dfgdfgdfgdf",
"text": "dfgdgf",
"key": "1",
"onSelect": "uuuu",
"_id": ObjectId("50656f44a4e82d3f98291f01")
}
]
}
Any one help me, I'm new to Mongo
In the JavaScript shell you can do this:
var query = {"_id": ObjectId("50656f33a4e82d3f98291eff")};
db.collection.update(query, {'$pull':{ menus: {name : 'gdfgdfgdfg'} } });
or use the Id.
db.collection.update(query, {'$pull': { menus: {"_id": ObjectId("50656f3ca4e82d3f98291f00")} } });
With the Java Driver should be something like this:
BasicDBObject query = new BasicDBObject("_id", new ObjectId("50656f33a4e82d3f98291eff"));
BasicDBObject docToRemove = new BasicDBObject("name", "gdfgdfgdfg");
BasicDBObject updateCommand = new BasicDBObject("$pull", new BasicDBObject("menus", docToRemove));
collection.update(query, updateCommand);
Mongo won't let you delete the embedded document. What you have to do is take the object from the collection, delete the one object in the list, and then save it back into the database.
obj = db.collection.findOne({"_id": ObjectId("50656f33a4e82d3f98291eff")});
menus = obj.menus.splice(0,1); // Or some other way to manually delete
// the one item in the list
db.collection.update({"_id": ObjectId("50656f33a4e82d3f98291eff")},
{$set: {menus: menus}});
It's complicated, see here.
EDIT: If you don't know the index, you can try this:
obj = db.collection.findOne({"_id": ObjectId("50656f33a4e82d3f98291eff")});
var i = 0;
for(i=0;i<obj.menus.length;i++) {
if(obj.menus[i].name === "gdfgdfgdfg")
break;
}
menus = obj.menus.splice(i,1);
db.collection.update({"_id": ObjectId("50656f33a4e82d3f98291eff")},
{$set: {menus: menus}});
$conditionArray=array("_id"=>ObjectId("50656f33a4e82d3f98291eff"));
$dataArray=array("description"=>"");
$db->$collectionName->update($conditionArray,array('$unset' =>$dataArray))->limit(1);

Categories