Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter full text search results by userId field with hibernate

Suppose I have 2 entities annotated for full text search:

@Entity
public class User implements Serializable {

    @Id
    public Long id;    
    ...

}

@Entity
@Indexed
public class Post {

    @Id
    public Long id;

    @Field(name = "content")
    public String content;

    @ManyToOne
    public User user;

    ...

}

It's very simple to write full text search on just content field. But how can I filter the results by text keyword and user id? For example a query to search "hello" on content field and user.id equal to 10?

like image 352
user1079877 Avatar asked Nov 01 '22 08:11

user1079877


1 Answers

The keyword you are looking for is the annotation IndexedEmbedded.

@ManyToOne
@org.hibernate.search.annotations.IndexedEmbedded(includePaths={ "userId" })    
public User user;

If you now create your LuceneIndex, there will be an new Field in your LuceneDocument called User.userId. You now can search for UserId (as String) and the content in combination. Please take a look at Luke to inspect your Index files.

like image 107
Vincent Avatar answered Nov 13 '22 19:11

Vincent