Ignore field on mapping and indexing using ElasticSearch and java API - java

I'm developing an application using ElasticSearch, I want to index a document but ignoring some field.
#Document(indexName = "my_index", type = "user")
public class User{
#Id
private Long userId;
private String name;
private Adress adresse; //ignored field
getters and setters
}
Thanks in advance

Related

Nested reference object is persisting as null

I am trying to store below pojo in couchbase with spring-data, but persist json is storing "user field of type User" as null.
#JsonInclude(value = Include.NON_EMPTY)
#Document
public class ProjectXYZ {
#Id
#GeneratedValue(strategy = GenerationStrategy.UNIQUE)
private String id;
#Field
private String prjname;
#Field
private User user;
//setter and getter
}
Update:
User Pojo
#JsonInclude(value = Include.NON_EMPTY)
#Document
public class User {
#Id
#Field
private String id;
#Field
private String email;
#Field
private String firstName;
#Field
private String lastName;
//setter and getter
}
And as below I am saving it, All works fine and as expected but User object get stored as null.
ProjectXYZ project = new ProjectXYZ();
project.setUser(getUser(request));
project = projectXYZService.createProject(project);
References are not directly supported through Spring data couchbase as it needs to store meta information about the reference document id.
However there is support for fetching associated entities through N1QL ANSI Joins available in current Lovelace (3.1.x) versions. Here is the documentation for it.

Spring data rest is not saving all field of my entity

I have the following #Entity:
#Entity
public class Person {
#Id #GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private Date birthDate;
private String status;
private String city;
...
// many more attributes
}
I'm using the spring data rest as follow:
#RepositoryRestResource(collectionResourceRel = "person", path = "person")
public interface PersonRepositorio extends PagingAndSortingRepository<Person, Long>{
}
When I send the a post to the /api/person/ with a JSON containing all attributes of Person, only status is not set. Can someone help me?
Well, I think you're missing the get and set for status. Did you check it?

How can I include references in spring rest response?

I have defined two JPARepository with spring: "Person" and "Address". I also specified a relation between Person and Address.
I can fetch all persons with:
http://localhost:8080/person/
and all address with: http://localhost:8080/address/
Also I can get the address of a single person with http://localhost:8080/person/1/address
Now I'd like to get the address to every person as a nested address when I get request: http://localhost:8080/person/
How can I include the relations in the response?
My classes as requested by #nicolasl
#Entity
public class Person {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
#OneToOne
#JoinColumn(name = "address_id")
private Address address;
//Getter/Setter here
}
#Entity
public class Address {
#Id
#GeneratedValue
private long id;
#Column(nullable = false)
private String location;
#OneToOne(mappedBy = "address")
private Person person;
//Getter/Setter here
}
Well, I guess you just can't, not using the automated rest engine provided by spring. The rest API will return links to retrieve the related entities in the "_links" section of your JSON response (you can read more about that in http://stateless.co/hal_specification.html). You'd have to call those links to retrieve your Addresses.
Another way to solve this, is implementing your own repository and assembling your own JSON response.

Json is not getting any of the fields with annotations in pojo class - Spring

It is getting only name out of all the fields. Please see the below class.
Below are Hibernate Annotations
public class Project {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
#OneToMany(mappedBy="project" , fetch=FetchType.EAGER)
private Set<Employee> employees;
}
Request URL: http://localhost:8089/appln/allProjects
JSON Response
[{"name":"XYZ"},{"name":"Abc"}]
I missed getters and setters. To get fields in Json data, either the fields has to be public or their getters has to be public.

Ebean and Play! not filtering columns with .select()

I'm trying to fetch just a part of the model using Ebean in Play! Framework, but I'm having some problems and I didn't found any solutions.
I have these models:
User:
#Entity
#Table(name = "users")
#JsonInclude(JsonInclude.Include.NON_NULL)
public class User extends Model{
#Id
private int id;
#NotNull
#Column(name = "first_name", nullable = false)
private String firstName;
#Column(name = "last_name")
private String lastName;
#NotNull
#Column(nullable = false)
private String username;
#NotNull
#Column(nullable = false)
private String email;
private String gender;
private String locale;
private Date birthday;
private String bio;
#NotNull
#Column(nullable = false)
private boolean active;
private String avatar;
#Column(name = "created_at",nullable = false)
private Date createdAt;
#OneToMany
private List<UserToken> userTokens;
// Getters and Setters omitted for brevity
}
UserToken:
#Entity
#Table(name = "user_tokens")
public class UserToken extends Model {
#Id
private int id;
#Column(name = "user_id")
private int userId;
private String token;
#Column(name = "created_at")
#CreatedTimestamp
private Date createdAt;
#ManyToOne
private User user;
// Getters and Setters omitted for brevity
}
And then, I have a controller UserController:
public class UserController extends Controller{
public static Result list(){
User user = Ebean.find(User.class).select("firstName").where().idEq(1).findUnique();
return Results.ok(Json.toJson(user));
}
}
I expected that, when using the .select(), it would filter the fields and load a partial object, but it loads it entirely.
In the logs, there is more problems that I don't know why its happening.
It is making 3 queries. First is the one that I want. And then it makes one to fetch the whole Model, and another one to find the UserTokens. I don't know why it is doing these last two queries and I wanted just the first one to be executed.
Solution Edit
After already accepted the fact that I would have to build the Json as suggested by #biesior , I found (out of nowhere) the solution!
public static Result list() throws JsonProcessingException {
User user = Ebean.find(User.class).select("firstName").where().idEq(1).findUnique();
JsonContext jc = Ebean.createJsonContext();
return Results.ok(jc.toJsonString(user));
}
I render only the wanted fields selected in .select() after using JsonContext.
That's simple, when you using select("...") it always gets just id field (cannot be avoided - it's required for mapping) + desired fields, but if later you are trying to access the field that wasn't available in first select("...") - Ebean repeats the query and maps whole object.
In other words, you are accessing somewhere the field that wasn't available in first query, analyze your controller and/or templates, find all fields and add it to your select (even if i.e. they're commented with common HTML comment in the view!)
In the last version of Play Framework (2.6) the proper way to do this is:
public Result list() {
JsonContext json = ebeanServer.json();
List<MyClass> orders= ebeanServer.find(MyClass.class).select("id,property1,property2").findList();
return ok(json.toJson(orders));
}

Categories

Resources