Sorting RealmResults by relevance - java

I am trying to create a search engine that will show results containing different tags. The results must be sorted by relevance.
Lets say I have the following models:
public class Article extends RealmObject{
#PrimaryKey
private String aID = UUID.randomUUID().toString();
private RealmList<Tags> tags;
}
public class Tags extends RealmObject{
#PrimaryKey
private String tID = UUID.randomUUID().toString();
private String tag;
private RealmList<articleTagsRelation> articlesTag;
}
public class articleTagsRelation extends RealmObject{
private String tID;
private String aID;
private long timesArticleSelectedByTag;
}
So the relation between RealmObjects are:
Article (many-to-many) tags (many-to-many) articleTagsRelation
When users search by tag, the app should return all the Articles that fill the search:
realm.where(Article.class).equalTo("tags.tag", userSearch).findAll().
However, I would like to sort the results by relevance. Relevance in this example is the number of times users have selected the article when searching by the related tag (articleTagsRelation.timesArticleSelectedByTag).
I have been struggling for a while to find a direct way to accomplish this with no result. Is there any direct way to do it? if not, sorting the results one by one will be the unique solution?
Thanks

Realm 3.5.0+:
public class Article extends RealmObject{
#PrimaryKey
private String aID = UUID.randomUUID().toString();
private RealmList<Tags> tags;
#LinkingObjects("article")
private final RealmResults<ArticleTagsRelation> articleOfRelation = null;
}
public class Tags extends RealmObject{
#PrimaryKey
private String tID = UUID.randomUUID().toString();
private String tag;
#LinkingObjects("tag")
private final RealmResults<ArticleTagsRelation> tagOfRelation = null;
}
public class ArticleTagsRelation extends RealmObject{
private Tags tag;
private Article article;
private long timesArticleSelectedByTag;
}
realm.where(Article.class)
.equalTo("tags.tag", userSearch)
.findAllSorted("articleOfRelation.timesArticleSelectedByTag", Sort.DESCENDING);

Related

Trying to create save method so user can save trips to his profile

I have user in database and trip holidays, now, I want to create a document that will store holidays that user saved. Something like when you go on some website for buying products and then you 'favorite - save - bookmark' something.
I have a couple of entities for trip holidays like AdventureHolidays, Backpacking, CruiseHolidays etc.
Model looks like this:
#Document("adventureholidays")
public class AdventureHolidays {
#Id
private String id;
private String title;
private String description;
private String state;
private String typeOfAdventureHolidays;
private String image;
And I have a lot of them for each holiday as I said, Backpacking, CruiseHolidays etc.
I have user model like this:
#Document
public class User {
#Id
private String id;
#Indexed(unique = true)
#NonNull
private String username;
#Indexed(unique = true)
#NonNull
private String email;
#JsonIgnore
#NonNull
private String password;
Next I created a new model that will actually store userId and documents that he has saved.
#Document
public class UserSavedTrips {
#Id
private String userId;
#DBRef
AdventureHolidays adventureHolidays;
#DBRef
User user;
I dont know did I even implement that document for storing user saved holidays right.
Next I created repository:
#Repository
public interface UserSavedTripsRepository extends MongoRepository<UserSavedTrips, String> {
}
And service:
#Service
public class UserSavedTripsServiceImpl implements UserSavedTripsService {
#Autowired
UserSavedTripsRepository userSavedTripsRepository;
#Override
public UserSavedTrips save(UserSavedTrips userSavedTrips) {
return userSavedTripsRepository.save(userSavedTrips);
}
}
How I can now create controller for this so actually I can do what I want when I hit save button? And also, is this even good? Will that actually save holidayID to userSavedHolidays with also userID?

Complex Object as Inner Class inside Hibernate Entity

Now, I'm aware that an Inner class cannot be an Entity in Hibernate.
I'll first show my code, please refer to my question below:
#Entity
#Table(name = "bags")
public class Bags extends AbstractModel {
private String brand;
private String condition;
private String size;
private Extras extras;
#ManyToOne
private Customer customer;
private class Extras {
private boolean box;
private boolean authenticity_card;
private boolean shoulder_strap;
private boolean dustbag;
private boolean pouch;
private boolean padlock_and_key;
private boolean bagcharm;
private boolean nameTag;
private boolean mirror;
}
}
Getters and setters are ommited. My question is:
If I want to have a slightly more complex object such as Extras, in which I represent the absence or not of several accessories, would it be better to create an additional table associated with bags OR is there a way around this?
Please let me know if I was not clear or you require additional information.
#Embeddable annotation is used to declare a class will be embedded by other entities.
#Embeddable
public class Extras {
private boolean box;
private boolean authenticity_card;
private boolean shoulder_strap;
private boolean dustbag;
private boolean pouch;
private boolean padlock_and_key;
private boolean bagcharm;
private boolean nameTag;
private boolean mirror;
}
#Embedded is used to embed a type into another entity.
#Entity
#Table(name = "bags")
public class Bags extends AbstractModel {
private String brand;
private String condition;
private String size;
private Extras extras;
#ManyToOne
private Customer customer;
#Embedded
private Extras extras;
}

How to put propper DBFLow annotation

I want to insert doctor object to database, how should I put annotations for properties?
I tried to do it with te code shown below.
But i don't know how to do it on list properties specializations and phoneNumbers.
#Table(databaseName = WMDatabase.NAME)
public class Doctor extends BaseModel{
#Column
#PrimaryKey
#Unique(unique = true)
private String doctorId;
#Column
private FullName fullName;
#Column
private String organizationId;
#Column What shuld i put here?????
private List<Specialization> specializations;
#Column What shuld i put here?????
private Contacts contacts;
}
Below are the classes I use for doctor attributes:
public class Contacts extends BaseModel {
private List<PhoneNumber> phoneNumbers;
private String email;
private String fax;
}
public class Specialization extends BaseModel {
#Column
#PrimaryKey
#Unique(unique = true)
private String doctorId;
#Unique(unique = true)
private String specializationName;
public String getSpecializationName() {
return specializationName;
}
public void setSpecializationName(String specializationName) {
this.specializationName = specializationName;
}
DBFlow is a relational database system (not a mongo-type key/value store) and doesn't support lists as columns, according to the doc here.
List : List columns are not supported and not generally proper for a relational database. However, you can get away with a non-generic List column via a TypeConverter. But again, avoid this if you can.
The documentation on relationships may help you refine the model to suit your needs.

How to represent List of Objects field in a model with ORMLite?

I am trying to use ORMLite to represent comments in a conversation, like this:
#DatabaseTable(tableName = "comments")
public class Comment implements Parcelable{
#DatabaseField(id = true)
private Long id;
#DatabaseField
private Long conversation_id;
#DatabaseField
private String text;
...
public static class List extends ArrayList<Comment>{
}
}
...and...
#DatabaseTable(tableName = "conversations")
public class Conversation implements Parcelable{
#DatabaseField(id = true)
private Long id;
...
#ForeignCollectionField
private Comment.List comments;
#DatabaseField
private Date created_at;
...
}
And I am getting this error:
Field class for 'comments' must be of class ForeignCollection or
Collection
I am also using GSON so these models are populated automatically from json. For example:
{
"created_at":"2013-08-12T20:38:11Z",
"id":31,
"comments":[
{
"conversation_id":31,
"id":46,
"text":"IE sucks",
},
{
"conversation_id":31,
"id":47,
"text":"Yes it does",
}
]
}
Is there a way to achieve this just by changing descriptors?
Is it necessary to rework the Conversation class to use ForeignCollection as a type for comments or change Comment.List class to extend ForeignCollection? I'd like to avoid doing any of those because I am afraid it would break GSON implementation which currently works fine.
In Comment class:
...
#DatabaseField(
foreign = true
)
private Conversation conversation_id;
...
The conversation_id will actually only store id of the Conversation object, not the object itself.
There is a really good (if somehow unformatted) documentation here: http://ormlite.com/docs/foreign-object

Java Mapping objects to objects - Moo

I have a question about Moo (https://github.com/geoffreywiseman/Moo/) that I haven't been able to solve on my own. I have this class structure:
class Middle{
private int id;
private Upper upper;
private List<Child> children;
private List<Middle> brothers;
}
class Upper{
private int id;
private String name;
private String lastname;
}
class Child{
private int id;
private String name;
}
and I want to translate them to:
class OutputMiddle{
private int id;
#Property(translation="Upper")
private OutputUpper outputUpper;
#CollectionProperty(itemTranslation = Upper.class)
private List<OutputChild> outputChildren;
private List<OutputMiddle> outputBrothers;
}
class OutputUpper{
private int id;
private String outputName;
}
class OutputChild{
private int id;
private String outputName;
}
What I don't know is:
Am I able to translate one attribute from one class to another attribute of another class?
And do the same but to a collection of objects?
And the same but to a collection of objects of the same class?
Why do I need this? Because I'm returning objects of the class "Middle" as JSON (or XML) and I need it to have an structure such as the "OutputMiddle", but I don't want to intervene the JSON after its creation and change the names of the nodes manually.
Thanks!
Try JMapper Framework, it's ease to use and require few configuration

Categories

Resources