Solr: #Dynamic field not working with select query - spring-boot

I had Solr document as below :
#SolrDocument(collection = "test-core")
public class TestCore {
#Id
#Indexed
private Integer id;
#Indexed(name = "name", type = "string")
private String name;
#Dynamic
#Field("mappedField_*")
private Map<String, String> mappedFieldValues;
}
Its saving records successfully in solr as shown in below snapshot :
{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"q":"*:*",
"_":"1554871478940"}},
"response":{"numFound":1,"start":0,"docs":[
{
"id":"123",
"name":["Test name"],
"mappedField_firstName":["vishal"],
"mappedField_lastName":["rana"],
"_version_":1630400697277087744}]
}}
Now when I tried to find this record by id by below query :
Optional<TestCore> optional = testCoreRepo.findById(123);
Its not working, may be because of this #Dynamic fields are present there. That error log is as below :
org.springframework.dao.InvalidDataAccessApiUsageException: Incompartible types found. Expected class java.lang.String for mappedFieldValues with name mappedField_*, but found class java.util.ArrayList; nested exception is java.lang.IllegalArgumentException: Incompartible types found. Expected class java.lang.String for mappedFieldValues with name mappedField_*, but found class java.util.ArrayList
I tried to solve this whole day but didn't found any solution.
Please guide for the better solution.
Thank you.

After tried many things. I got it working now, and I think may be it can be helpful to other.
I changes #Dynamic field datatype as below
#Dynamic
#Field("mappedField_*")
private Map<String, List<String>> mappedFieldValues;
By change this map value from String to List its working now.

Related

Using projection with a interface in `#Query` annotation with a native query gives "Target type is not an interface and no matching Converter found"

I have a table with a clob in an oracle 19 database which I try to fetch with a native query using a #Query annotation with a projection from a Spring boot 2.7.4 application. I get the following error message:
java.lang.UnsupportedOperationException: Cannot project jdk.proxy2.$Proxy281 implementing java.sql.Clob,org.hibernate.engine.jdbc.WrappedClob,java.io.Serializable to java.lang.String; Target type is not an interface and no matching Converter found
The query from my repository class:
#Query(
value = """
select submission_id as "submissionId", text as "textAnswer"
from answer
where answer_id = :answerId
""",
nativeQuery = true)
public MyDTO findDTO(Long answerId);
My interface which I use for the projection:
public interface MyDTO {
String getTextAnswer();
}
From my domain object annotated with #Entity:
private String textAnswer;
My testcase which reproduce which reproduce this error. If I comment out the line with a.getTextAnswer() it runs ok.
#Test
public void findFormPublishedAnswersInterfaceDTOById() {
FormPublishedAnswersInterfaceDTO a = answerRepository.findFormPublishedAnswersInterfaceDTOById(21540241L);
assertEquals("test", a.getTextAnswer());
}
I have tried different hints I found by the help of google :) like annotating private String textAnswer with #Lob, #Type(type = "text") and #Column(name = "text", columnDefinition = "CLOB") without any luck.
If you read the exception carefully, you should understand that the JDBC driver reports that the type is a Clob, so your DTO must look like this instead:
public interface MyDTO {
Clob getTextAnswer();
}

Spring data mongodb repository. How can I search by a list of IDs?

I have the following class!
public class Task{
ObjectId id;
String title;
String description;
/* Getters and Setters removed for brevity */
}
and I have the following mongoRepository class, very simple :
public interface TaskRepository extends MongoRepository<Task, String> {
}
As you can see, I have not yet tried to extend this class - What would I want to do here if I want to have a find method, where I could just hand it a list of Ids, and get my list of corresponding tasks back?
The CrudRepository which MongoRepository extends has a findAll method, which takes an Itereable<ID>
I think that is exactly what you are looking for.
Note that it is renamed to findAllById in the latest Milestone releases.
You can create a custom query method that searches for an array of _id values:
#Query(value = "{ '_id' : {'$in' : ?0 } }", fields = "{ 'description': 0 }")
Iterable<Task> findAllThin(Iterable<String> ids);
(in this case it returns the fields id and title only)
#Neil Lunn brought me to the answer.

Id field handling in Spring Data Mongo for child objects

I have been working in Spring Boot with the Spring Data MongoDB project and I am seeing behavior I am not clear on. I understand that the id field will go to _id in the Mongo repository per http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mapping.conventions.id-field. My problem is that it also seems to be happening for child entities which does not seem correct.
For example I have these classes (leaving out setters and getters for brevity) :
public class MessageBuild {
#Id
private String id;
private String name;
private TopLevelMessage.MessageType messageType;
private TopLevelMessage message;
}
public interface TopLevelMessage {
public enum MessageType {
MapData
}
}
public class MapData implements TopLevelMessage {
private String layerType;
private Vector<Intersection> intersections;
private Vector<RoadSegment> roadSegments;
}
public class RoadSegment {
private int id;
private String name;
private Double laneWidth;
}
and I create an object graph using this I use the appropriate MongoRepository class to save I end up with an example document like this (with _class left out):
{
"_id" : ObjectId("57c0c05568a6c4941830a626"),
"_class" : "com.etranssystems.coreobjects.persistable.MessageBuild",
"name" : "TestMessage",
"messageType" : "MapData",
"message" : {
"layerType" : "IntersectionData",
"roadSegments" : [
{
"_id" : 2001,
"name" : "Road Segment 1",
"laneWidth" : 3.3
}
]
}
}
In this case a child object with a field named id has its mapping converted to _id in the MongoDB repository. Not the end of the world although not expected. The biggest problem is now that this is exposed by REST MVC the _id fields are not returned from a query. I have tried to set the exposeIdsFor in my RepositoryRestConfigurerAdapter for this class and it exposes the id for the top level document but not the child ones.
So circling around the 2 questions/issues I have are:
Why are child object fields mapped to _id? My understanding is that this should only happen on the top level since things underneath are not really documents in their own right.
Shouldn't the configuration to expose id fields work for child objects in a document if it is mapping the field names?
Am I wrong to think that RoadSegment does not contain a getId() ? From Spring's documentation:
A property or field without an annotation but named id will be mapped
to the _id field.
I believe Spring Data does this even to nested classes, when it finds an id field. You may either add a getId(), so that the field is named id or annotate it with #Field:
public class RoadSegment {
#Field("id")
private int id;
private String name;
private Double laneWidth;
}
I agree this automatic conversion of id/_id should only be done at the top level in my opinion.
However, the way Spring Data Mongo conversion is coded, all java ojects go through the exact same code to be converted into json (both top and nested objects):
public class MappingMongoConverter {
...
protected void writeInternal(Object obj, final DBObject dbo, MongoPersistentEntity<?> entity) {
...
if (!dbo.containsField("_id") && null != idProperty) {
try {
Object id = accessor.getProperty(idProperty);
dbo.put("_id", idMapper.convertId(id));
} catch (ConversionException ignored) {}
}
...
if (!conversions.isSimpleType(propertyObj.getClass())) {
// The following line recursively calls writeInternal with the nested object
writePropertyInternal(propertyObj, dbo, prop);
} else {
writeSimpleInternal(propertyObj, dbo, prop);
}
}
writeInternal is called on the top level object, and then recalled recursively for each subobjects (aka SimpleTypes). So they both go through the same logic of adding _id.
Perhaps this is how we should read Spring's documentation:
Mongo's restrictions on Mongo Documents:
MongoDB requires that you have an _id field for all documents. If you
don’t provide one the driver will assign a ObjectId with a generated
value.
Spring Data's restrictions on java classes:
If no field or property specified above is present in the Java class
then an implicit _id file will be generated by the driver but not
mapped to a property or field of the Java class.

spring-data-cassandra, Cassandra storing List<Integer> as field

I have problem storing field of List as field of my table in cassandra database.
I'm using spring-data-cassandra, version 1.4.1.RELEASE.
My model contains:
#Column
#CassandraType(type = DataType.Name.LIST)
private List<Integer> somedata;
I would like to auto create my db tables with SchemaAction.RECREATE_DROP_UNUSED Schema action in my config class:
#Override
public SchemaAction getSchemaAction() {
return SchemaAction.RECREATE_DROP_UNUSED;
}
It tries to create table, but following exception occurs:
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: expected 1 of typed arguments for the property 'somedata' type is 'interface java.util.List' in the entity <my class here>
Ok, found solution after digging into spring-data-cassandra source:
#CassandraType(type = DataType.Name.LIST, typeArguments = { DataType.Name.FLOAT } )
private List<Float> position;
You need to add typeArguments, to specify DataType of generic type. Exception mentioned in question is thrown when invalid number of those typeArguments is passed. In my case there was 0 and 1 is required.
#Column annotation proved to be not required here.
There is no need to provide CassandraType annotation over the field as long as you are using primitive or Wrapper type objects.
Following works fine for me.
#PrimaryKey
int id;
String name;
int num;
List<Integer> marks;
I am using spring-data-cassandra with version 1.4.2.RELEASE.
Please update if you still face the issue.

Spring Data query over two documents

I have an m:n connection in my MongoDB, MessageUserConnection is the class between User and Message.
Now I will get all MessageUserConnections where MessageUserConnection#confirmationNeeded is true, read is false and Message#receiveDate is not older than the last week.
Is there any possibility to do this with Spring Data?
Thanks a lot!
public class MessageUserConnection {
#Id
private String id;
#DBRef
private User userCreatedMessage;
#DBRef
private Message message;
private Boolean confirmationNeeded;
private Boolean read;
}
public class Message {
#Id
private String id;
private String title;
private String message;
private DateTime receiveDate;
}
[EDIT]
I have tried it by my own:
#Query("FROM MessageUserConnection AS muc WHERE muc.confirmationNeeded = ?0 AND muc.message.receiveDate = ?1")
List<MessageUserConnection> findMessageUserConnectionByConfirmationNeededAndReceiveDate(final Boolean confirmationNeeded, final DateTime receiveDate);
and I get the following exception:
Caused by: com.mongodb.util.JSONParseException:
FROM MessageUserConnection AS muc WHERE muc.confirmationNeeded = "_param_0" AND muc.message.receiveDate = "_param_1"
Does anyone know what I am doing wrong here?
Thanks a lot!
[EDIT]
I run into another problem. My query currently looks like this.
#Query("{$and : [{'confirmationNeeded' : ?0}, {'message.receiveDate' : ?1}]}")
where confirmationNeeded is a boolean and message.receiveDate is Joda#DateTime. With this query I get the following exception:
org.springframework.data.mapping.model.MappingException: Invalid path reference message.receiveDate! Associations can only be pointed to directly or via their id property!
Does that mean that I only can join to message.id?
Thanks a lot!

Resources