I was finding on the internet how to update all the document field values with lowercase.
I luckily found a query which I modified as per my requirement and it is working correctly.
db.messages.updateMany({},
[
{
$set: {
recipientEmail: {
$toLower: '$recipientEmail'
},
senderEmail: {
$toLower: '$senderEmail'
}
}
}
],{ multi: true })
But now I am trying to convert this query into Java code, I am not able to convert it.
I again started looking into the internet, but couldn’t find any code.
So, can anyone help me convert this query to Java code so that I can use it in my Spring Boot application?
Thanks in advance.
You can use #Query annotation in your repository interface and pass your query as it is (above the method signature).
Here is an example :
#Query("{$and:["
+ " {'id': ?0},"
+ " {$or:["
+ " {'customerId': ?1},"
+ " {'specificCode': ?4}"
+ " ]},"
+ " {'beginDate' : { $gte: ?2}},"
+ " {$or:["
+ " {'endDate' : { $lte: ?2}},"
+ " {'endDate' : {$exists: false}}"
+ " ]},"
+ " {'numberOfTimesUsed': { $lt: ?3}}"
+ "]}")
You can try something like this:
Query query = new Query();
Update update = new Update();
update.set("recipientEmail", StringOperators.valueOf("recipientEmail").toUpper());
update.set("senderEmail", StringOperators.valueOf("senderEmail").toUpper());
mongoTemplate.updateMulti(query, update, Messages.class);
Since you are aggregation pipeline form of update, you can try this:
Query query = new Query();
AggregationUpdate update = AggregationUpdate.update().set("recipientEmail").toValue(StringOperators.valueOf("recipientEmail").toUpper()).set("senderEmail").toValue(StringOperators.valueOf("senderEmail").toUpper());
mongoTemplate.updateMulti(query, update, Messages.class);
Related
I read through https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#reference to begin with
My requirements
I want to use percolator. Is there any support for it in spring data elasticsearch? I don't see any in the above link although I understand that percolating is same as indexing (technically from using spring data elasticsearch's perspective). So I can use the indexing part of spring data elasticsearch but just checking if there are any that are specific to percolator.
I want to create an index dynamically. I do understand I can achieve that using SpEL template expression as mentioned in https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.mapping.meta-model.annotations but my case is slightly different, I will get the index name via the RequestParam as part of the API call. So this means as of my knowledge I cannot use SpEL or try something like https://stackoverflow.com/a/33520421/4068218
I see I can use ElasticsearchOperations or ElasticsearchRepository to create Index. Because of #2 (i.e index name via request parameter) I think ElasticsearchOperations better suits but I see IndexOperations facilitating createMapping, createSettings but not both together. I see putMapping too but I dont see anything that says both mapping and settings. The reason I want both is I want to create something like below to begin with
"settings" : {
"index" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
}
},
"mappings": {
"properties": {
"message": {
"type": "text"
},
"query": {
"type": "percolator"
}
}
}
Bottom line :- How do I create an index (name of the index will be dynamic via request param) with mappings, settings using ElasticsearchOperations?
Any lead/help is much appreciated
First of all thank you very much #P.J.Meisch. Upvoted both your comments as a token of gratitude.
Below worked for me. Below might help others in future
Document mapping = Document.create().fromJson("{\n" +
"\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"query\": {\n" +
" \"type\": \"percolator\"\n" +
" }\n" +
" }\n" +
"\n" +
"}");
Map<String, Object> settings = ImmutableMap.of( "number_of_shards" ,2,"number_of_replicas",1);
elasticsearchOperations.indexOps(IndexCoordinates.of("whatever-indexname-you-need")).create(settings,mapping);
I am using java low level REST client api.
I am fetching particular fields on matching or related datas from elasticsearch by using wildcards.
I am struck with it getting data as well as searching data with wildcard.
How to fetch result in java list .
Here is my code:
HttpEntity entity1 = new NStringEntity( "{\n" + " \"query\" : {\n" + " \"wildcard\": { \"Content\":\"java *\"} \n" + "} \n"+ "}",ContentType.APPLICATION_JSON);
Response response = restClient.performRequest("GET", "/test/_search",Collections.singletonMap("pretty", "true"), entity1);
System.out.println(EntityUtils.toString(response.getEntity()));
Could you please help me out? Thank you!
I need to create dynamic query wherein the where condition will be changed based on the request coming in to mule.The request will be always get with query parameter. Here goes the example:
http://localhost:8084/basePath?name=balwant&age=26 , OR
http://localhost:8084/basePath?name=balwant&age=26&gender=M
Likewise it will be dynamic.Now I need a way by which I can create a query where the WHERE condition will be added based on the query parameters in request.
Haven't tested this, but something like this. Check if the inboundProperty is there and build up the query programatically :
SELECT * FROM USERS WHERE NAME = '#[message.inboundProperties.name]' #[message.inboundProperties.gender !=null ? ' AND GENDER=' + message.inboundProperties.gender] #[message.inboundProperties.age !=null ? ' AND AGE=' + message.inboundProperties.age]
I had this in my mind of using Custom Transformers. So I used java transformer for this.
The logic looks something like this :
public class QueryBuilder extends AbstractMessageTransformer {
#Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
System.out.println("Query Params : "
+ message.getInboundProperty("http.query.params").getClass()
.getName());
Map<?, ?> map = message.getInboundProperty("http.query.params");
System.out.println("Map keys : " + map.keySet());
String where = "";
for (Map.Entry<?, ?> entry : map.entrySet()) {
System.out.println(entry.getKey() + "/" + entry.getValue());
where = where+" "+entry.getKey()+"="+"'"+entry.getValue()+"'"+" and";
}
String whereCondition = where.substring(0, where.lastIndexOf(" "));
System.out.println("Where condition is : "+ where.substring(0, where.lastIndexOf(" ")));
return whereCondition;
}}
Now this returns the payload which is string type.
In DB connector, select Query type as Dynamic. After WHERE condition add #[payload].
Cheers
Above query works if the values are available in the message inbound properties. But if you want to build you SQL query with request query param values then you need to use like below (as the query param values will be available under message inbound properties http.query.param from mule 3.6.0 onward)
SELECT * FROM USERS WHERE NAME = '#[message.inboundProperties.'http.query.params'.name]' #[message.inboundProperties.'http.query.params'.gender !=null ? ' AND GENDER=' + message.inboundProperties.'http.query.params'.gender] #[message.inboundProperties.'http.query.params'.age !=null ? ' AND AGE=' + message.inboundProperties.'http.query.params'.age]
Learning Accumulo at the moment and I noticed there wasn't a direct call that I found for figuring out the column family for an entry. I need data from an Accumulo table in the format of
for example:
{key:"XPZ-878-S12",
columns:[{name:"NAME",value:"FOO BAR"},
{name:"JOB",value:"ENGINEER"}
]
}
And these spots are where I am trying to take data from:
{key:"key value from table",
columns:[{name:"name of column family",value:"value from table"},
{name:"name of column family",value:"value from table"}
]
}
So obviously key and value are easy to get ahold of, but what I call the "name" is extremely important to me as well, aka the column family name.
Yes it is possible. For example take a look at this:
for (Entry<Key, Value> entry : scan) {
Text key = entry.getKey().getRow();
Value val = entry.getValue();
returnVal.append("KEY" + key + " " + entry.getKey().getColumnFamily() + ": " + val + "\n");
}
The solution being for whatever entry you are looking at do entry.getKey().getColumnFamily()
I have data index in Solr. One parameter is an array and looks like this:
<arr name="sm_vid_Code_of_Federal_Regulations">
<str>Section 1.13</str>
<str>Subpart A</str>
<str>Part 1</str>
<str>Subtitle A</str>
<str>Title 7</str>
</arr>
I need to restrict the results using two or more of these fields.
I tried the following but it does not seem to be working correctly:
params.set("fq", "(sm_vid_Code_of_Federal_Regulations:\"Part " + "1" +"\")" + " OR "
+ "(sm_vid_Code_of_Federal_Regulations:\"Title " + "7" +"\")");
Is this the right approach to combine elements from a field array?
Turns out that I should use the class SolrQuery and the method addField.
So the correct code will be:
SolrQuery query = new SolrQuery();
query.addFilterQuery("(sm_vid_Code_of_Federal_Regulations:\"Title " + "7" +"\")",
"(sm_vid_Code_of_Federal_Regulations:\"Part " + "1" +"\")");
You can add apply multiple query filters using the addFilterQuery method.
I am very grateful to the anonymous person who wrote the fantastic Solr tutorial:http://www.solrtutorial.com/solrj-tutorial.html