Field with #Field not getting translated to the correct value - spring

I have a document with one of the fields' names overridden by #Field:
public User {
#Id
private String id;
private String username;
#Field("profiles")
private List<BusinessProfile>
businessProfiles;
...
}
And a aggregation operation with a match operation as follows:
match(where("businessProfiles.services").elemMatch(Criteria.where("category").is(serviceCategory)))
However, in the query that this ultimately generates, the businessProfiles is not traslated to profiles. Here is the query I got from the log files:
Executing aggregation: [ { "$match" : { "businessProfiles.services" : { "$elemMatch" : { "category" : "Cloud_Initiation"}}}} ...]
This behavior seems very odd. Is this supposed to work this way? Thanks.

Field mapping is only done for TypedAggregation providing the mapping source type.
TypedAggregation<Product> agg = newAggregation(User.class,
match(where("businessProfiles.services")...
I created DATAMONGO-2310 to improve the documentation in that area.

Related

How to filter Range criteria using ElasticSearch Repository

I need to fetch Employees who joined between 2021-12-01 to 2021-12-31. I am using ElasticsearchRepository to fetch data from ElasticSearch index.
How can we fetch range criteria using repository.
public interface EmployeeRepository extends ElasticsearchRepository<Employee, String>,EmployeeRepositoryCustom {
List<Employee> findByJoinedDate(String joinedDate);
}
I have tried Between option like below: But it is returning no results
List<Employee> findByJoinedDateBetween(String fromJoinedDate, String toJoinedDate);
My Index configuration
#Document(indexName="employee", createIndex=true,type="_doc", shards = 4)
public class Employee {
#Field(type=FieldType.Text)
private String joinedDate;
Note: You seem to be using an outdated version of Spring Data Elasticsearch. The type parameter of the #Document
annotation was deprecated in 4.0 and removed in 4.1, as Elasticsearch itself does not support typed indices since
version 7.
To your question:
In order to be able to have a range query for dates in Elasticsearch the field in question must be of type date (the
Elasticsearch type). For your entity this would mean (I refer to the attributes from the current version 4.3):
#Nullable
#Field(type = FieldType.Date, pattern = "uuuu-MM-dd", format = {})
private LocalDate joinedDate;
This defines the joinedDate to have a date type and sets the string representation to the given pattern. The
empty format argument makes sure that the additional default values (DateFormat.date_optional_time and DateFormat. epoch_millis) are not set here. This results in the
following mapping in the index:
{
"properties": {
"joinedDate": {
"type": "date",
"format": "uuuu-MM-dd"
}
}
}
If you check the mapping in your index (GET localhost:9200/employee/_mapping) you will see that in your case the
joinedDate is of type text. You will either need to delete the index and have it recreated by your application or
create it with a new name and then, after the application has written the mapping, reindex the data from the old
index into the new one (https://www.elastic.co/guide/en/elasticsearch/reference/7.16/docs-reindex.html).
Once you have the index with the correct mapping in place, you can define the method in your repository like this:
List<Employee> findByJoinedDateBetween(LocalDate fromJoinedDate, LocalDate toJoinedDate);
and call it:
repository.findByJoinedDateBetween(LocalDate.of(2021, 1, 1), LocalDate.of(2021, 12, 31));

MongoDB embedded Document Array: Get only one embedded document with a spezific attribute

I want to get one Embedded Document with a specific field (version) from an array with mongodb and spring boot.
This is the data structure:
{
"_id": 5f25882d28e40663719d0b52,
"versions": [
{
"versionNr": 1
"content": "This is the first Version of some Text"
},
{
"versionNr": 2
"content": "This is the second Version of some Text"
},
...
]
...
}
Here are my entities:
#Data
#Document(collection = "letters")
public class Letter {
#Id
#Field("_id")
private ObjectId _id;
#Field("versions")
private List<Version> versions;
}
//There is no id for embedded documents
#Data
#Document(collection = "Version")
public class Version{
#Field("content")
private String content;
#Field("version")
private Long version;
}
And this is the query that doesn't work. I think the "join" isn't correct. But can't figure out the right way.
public Optional<Version> findByIdAndVersion(ObjectId id, Long version) {
Query query = new Query(Criteria.where("_id").is(id).and("versions.version").is(version));
return Optional.ofNullable(mongoTemplate.findOne(query,Version.class,"letters"));
}
}
EDIT: This is a working Aggregation, I'm sure it isn't a pretty solution but it works
#Override
public Optional<Version> findByIdAndVersion(ObjectId id, Long version) {
MatchOperation match = new MatchOperation(Criteria.where("_id").is(id).and("versions.version").is(version));
Aggregation aggregate = Aggregation.newAggregation(
match,
Aggregation.unwind("versions"),
match,
Aggregation.project()
.andInclude("versions.content")
.andInclude("versions.version")
);
AggregationResults<Version> aggregateResult = mongoTemplate.aggregate(aggregate, "letters", Version.class);
Version version = aggregateResult.getUniqueMappedResult();
return Optional.ofNullable(mongoRawPage);
}
Query query = new Query(Criteria.where("_id").is(id).and("versions.version").is(version));
return Optional.ofNullable(mongoTemplate.findOne(query,Version.class,"letters"));
You are querying the Letter document but your entity class is specified as Version.class, since findOne from MongoDB doesn't return the subdocument by itself but rather the whole document, you need to have Letter.class as return type and filter (project) what fields to get back. So you can either project the single version subdocument that you want to receive, like so:
Query query = new Query()
.addCriteria(Criteria.where("_id").is(id).and("versions.version").is(version))
.fields().position("versions", 1);
Optional.ofNullable(mongoTemplate.findOne(query, Letter.class))
.map(Letter::getVersions)
.findFirst()
.orElse(null);
or use aggregation pipeline:
newAggregation(
Letter.class,
match(Criteria.where("_id").is(id)),
unwind("versions"),
replaceRoot("versions"),
match(Criteria.where("version").is(version))),
Version.class)
Note -- I typed this on a fly.

Spring Mongo mapping variable data

I'm using Spring Data MongoDB for my project. I work with a mongo database containing a lot of data, and I want to map this data within my Java application.
The problem I have is that some data back in time had a different structure.
For example sport_name is an array now, while in some old records is a String:
sport_name: "Soccer" // Old data
sport_name: [ // Most recent entries
{
"lang" : "en",
"val" : "Soccer"
},
{
"lang" : "de",
"val" : "Fussball"
}
]
Here is what I have until now:
#Document(collection = "matches")
public class MatchMongo {
#Id
private String id;
private ??? sport_name; // Best way?!
(What's the best way to)/(How would you) handle something like this?
If old data can be considered as "en" language, then separate structure can be used to store localized text:
class LocalName {
private String language;
private String text;
// getters/setters
}
So mapped object will store the collection of localized values:
public class MatchMongo {
// it can also be a map (language -> text),
// in this case we don't need additional structure
private List<LocalName> names;
}
This approach can be combined with custom converter, to use plain string as "en" locale value:
public class MatchReadConverter implements Converter<DBObject, MatchMongo> {
public Person convert(DBObject source) {
// check what kind of data located under "sport_name"
// and define it as "en" language text if it is an old plain text
// if "sport_name" is an array, then simply convert the values
}
}
Custom mapping is described here in details.
Probably you can write a utility class which will fetch all the data where sport_name is not an array and update the element sport_name to array. But this all depends on the amount of data you have.
You can use query {"sport_name":{$type:2}}, here 2 stands for String.
Refer for more details on $type: http://docs.mongodb.org/manual/reference/operator/query/type/

Mongodb query for embedded doc

I am having trouble to query that, I want to find all the docs that contains the id "5418a26ce4b0e4a40ea1d548" in the individualUsers field. would be esp useful if you know how to do that in Spring Data MongoDB query.
db.collection.find({individualUser:{"5418a26ce4b0e4a40ea1d548"}})
example of one doc
{ "_id" : ObjectId("5418c3b9e4b03feec4345602"), "creatorId" : "5418a214e4b0e4a40ea1d546", "individualUsers" : { "5418a26ce4b0e4a40ea1d548" : null, "5418a278e4b0e4a40ea1d54a" : null } }
Update #001
Entity code
#Document
class Idea{
#Id
String id;
String creatorId;
Map<String,String> individualUsers;
/*getter and setter omitted*/
}
Interface
public interface IdeaRepository extends MongoRepository<Idea,String> {
}
Update #002
So when spring-mongodb saves hashmap to json, it will look like
"individualUsers" : { "5418a26ce4b0e4a40ea1d548" : null, "5418a278e4b0e4a40ea1d54a" : null }
In java program I can easily get the data using the key value. but in the mongodb query, I can't query the key?
so the question is can I query inside the "individualUsers": {} with the key ??

How can I store a Huge String in my Domain Class?

I'm making a web application in Grails, and I have this domain Class
String name
String query
static mapping = {
query type: "text"}
but when I bring the query from another query in Oracle, It returns a really huge String and I get this Error:
ORA-01461: can bind a LONG value only for insert into a LONG column
From the database that i get the information, the fiels is varchar(63760)
Any idea? thanks
Try adding this to your mapping: sqlType: 'clob'. So you would have the following:
String name
String query
static mapping = {
query type: "text", sqlType: "clob"
}
Also, see this SO question.
Grails allows to set maximum size which gets converted to mySQL size, so you can try this if it works in Oracle.
class Class {
String name;
String query;
static constraints = {
query(maxSize: 2048000)
}
static mapping = {
query type: "text"
}}

Resources