How to set OpType on IndexQuery in Spring Data Elasticsearch - elasticsearch

Assume spring-boot-starter-data-elasticsearch version 2.1.0.RC1.
Take the following, simple implementation for indexing an entity:
IndexQuery indexQuery = new IndexQueryBuilder().withId(entity.getId()).withObject(entity).build();
String id = elasticsearchTemplate.index(indexQuery);
How do I set the OpType.CREATE on this operation, so that I can assure only documents get indexed which don't already exist?
The equivalent REST API request would look like the following:
POST /{index}/{entity id}?op_type=create
{
"id" : "{entity id}",
"attribute" : "value"
}

This is not supported at the moment by Spring Data ES.
There's a open issue that reports exactly that feature, you might want to check it out: https://jira.spring.io/browse/DATAES-247

Related

How to do mongodb aggregation lookup using mongotemplate that involves a localfield array?

I have 2 collections Customers and Accounts, Customers contains a accounts field that is an array of account ids. Using aggregation lookup I want to joint each customers account list entry to the corresponding account object. According to the MongoDB 5 manual this is perfectly doable and in fact I can create an aggregation pipeline in both the mongo shell and mongocompass like the following (and get the correct results):
db.customers.aggregate([{ "$lookup" : { "from" : "accounts", "localField" : "accounts", "foreignField" : "account_id", "as" : "accountInfo"}},{ "$limit": 10 }] )
In my java model for Customer I added an additonal field List accountInfo and run the following query using my mongotemplate from the Customer repo:
LookupOperation stage = Aggregation.lookup("accounts", "accounts", "account_id", "accountInfo");
Aggregation aggregation = Aggregation.newAggregation(stage);
AggregationResults<Customer> aggResults = secondaryMongoTemplate.aggregate(aggregation,"customers",Customer.class);
List<Customer> results = aggResults.getMappedResults();
When I run this, I get neither any errors OR any results. Any thoughts?

Update by query multiple fields using Spring Data Elasticsearch?

I want to update all the documents that have for exemple the same name. I've seen in the elasticsearch documentation that I can use _update_by_query. So I tried to implement it in my repository like this:
#Query("{\"script\": { \"inline\": \"ctx._source.name = ?1\"; \"ctx._source.username = ?2\"; \"ctx._source.avatar = ?3\", \"lang\": \"painless\" }, \"query\": { \"match\": { \"name\" : \"?1\" }}")
List<User> update(String name, String username, String avatar);
But I get the following error:
nested exception is ElasticsearchStatusException[Elasticsearch exception [type=parsing_exception, reason=[script] query does not support [inline]]]
at org.springframework.kafka.listener.SeekUtils.seekOrRecover(SeekUtils.java:157) ~[spring-kafka-2.5.0.RELEASE.jar:2.5.0.RELEASE]
Edit 26.06.2020:
This answer is not correct, I added a correct on.
Old incorrect answer:
Seems strange to me, that this error comes from org.springframework.kafka.listener.SeekUtils.
To update using a script, you can use the update(UpdateQuery updateQuery, IndexCoordinates index) of a ElasticsearchOperations instance.
To have this in your Repository, you will need to create a repository cusomization like it is described here. In the implementation, autowire a ElasticsearchOperations instance. In this custom repository interface, you define the method
List<User> update(String name, String username, String avatar);
In the implementation, build up a UpdateQuery object with the script and the other information and pass this to the ElasticsearchOperations instance.
After checking the code of Spring Data Elasticsearch, I need to withdraw what I wrote in the first answer:
Currently Spring Data Elasticsearch does not support update by query. It is only possible to update entities with a know id either in a single operation or in a batch update.
I created an issue in Jira to add support for that.

spring-data-mongodb using the fieldName instead of _id

I have a Pojo with an attribute as
Class A{
#Id
#Field("item_id")
private String itemId;
}
When i try to update a document in MongoDB collection based on the itemId as below, it worked well and able to see from mongo ops logs that the query was transformed as "_id in itemIds "
Query query = new Query(Criteria.where("itemId").in(itemIds));
Update update = new Update();
update.set("field2", "abd");
mongoTemplate.updateMulti(query, update, A.class)
When i upgraded to spring-data-mongodb-2.1.5.RELEASE, the query i saw in the mongo logs was "item_id in itemIds". Since item_id is not a field and no index for that field in the collection, the query took forever to complete.
Any help to understand why the spring-data library is building the query as _id in older version and using the field as it is in newer version?
After a 2 minute search on the Spring documentation (https://docs.spring.io/spring-data/mongodb/docs/1.3.3.RELEASE/reference/html/mapping-chapter.html):
The following outlines what field will be mapped to the '_id' document field:
A field annotated with #Id (org.springframework.data.annotation.Id) will be mapped to the '_id' field.
A field without an annotation but named id will be mapped to the '_id' field.
Did you try that already?

Convert ObjectId to String in Spring Data

How can I reference two mongodb collections using spring data while the localField is of type ObjectId and foreignField is of type String?
ProjectionOperation convertId=Aggregation.project().and("_id").as("agentId");
LookupOperation activityOperation = LookupOperation.newLookup().
from("activity").
localField("agentId").
foreignField("agent_id").
as("activities");
Aggregation aggregation = Aggregation.newAggregation(convertId,activityOperation);
return mongoTemplate.aggregate(aggregation, "agents", AgentDTO.class).getMappedResults()
However, this doesn't return any records because of the type issue. Is it possible to implement $toString or $convert in ProjectionOperation? or what other options are there?
I was able to solve it by writing native mongodb aggregation operation in java code as described in MongoDB $aggregate $push multiple fields in Java Spring Data
After implementing this solution I was able to add native $addfields as follows:
AggregationOperation addField=new GenericAggregationOperation("$addFields","{ \"agId\": { \"$toString\": \"$_id\" }}");

Spring Elastic Search Custom Field names

I am new to Elastic Search and I am trying to implement it using Spring-data-elasticsearch.
I have fields with names such as "Transportation", "Telephone_Number" in our elastic search documents.
When I try to map my #Domain object fields with those, I don't get any data for those as I couldn't successfully map those fields.
Tried to use #Field, was disappointed as it didn't have 'name' property in it to map with custom field name.
Tried different variations of a GETTER function, none of those seem to be mapping to those fields.
I started wondering if there's something I'm missing here.
How does a domain object field look like which should map to a filed called something like "Transportation" ?
Any help appreciated
You can use custom name. Spring Data ES use Jackson. So, you can use #JsonProperty("your_custom_name") to enable custom name in ES Mapping
for example:
#Document(indexName = "your_index_name", type = "your_type_name")
public class YourEntity {
....
#JsonProperty("my_transportation")
#Field(type = FieldType.String, searchAnalyzer = "standard", indexAnalyzer = "standard", store = true) // just for example
private String myTransportation;
....
}
Note: I'm sorry anyway, my english is bad.. :D

Resources