Last active
March 1, 2017 07:35
-
-
Save thjanssen/8e2e41ac040738d884d9e77776044ef5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EntityManager em = emf.createEntityManager(); | |
em.getTransaction().begin(); | |
FullTextEntityManager fullTextEm = Search.getFullTextEntityManager(em); | |
QueryBuilder tweetQb = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity(Tweet.class).get(); | |
Query tweetQuery = tweetQb.all().createQuery(); | |
FullTextQuery fullTextQuery = fullTextEm.createFullTextQuery(tweetQuery, Tweet.class); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FacetingRequest postedAtFR = tweetQb.facet() | |
.name("userNameFR") | |
.onField(Tweet_.userName.getName()) | |
.discrete() | |
.orderedBy(FacetSortOrder.COUNT_DESC) | |
.includeZeroCounts(false) | |
.maxFacetCount(3) | |
.createFacetingRequest(); | |
FacetManager facetMgr = fullTextQuery.getFacetManager(); | |
facetMgr.enableFaceting(postedAtFR); | |
List<Facet> facets = facetMgr.getFacets("userNameFR"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
06:44:05,467 INFO TestSearchTweets:179 - thjanssen123 3 | |
06:44:05,467 INFO TestSearchTweets:179 - baeldung 1 | |
06:44:05,467 INFO TestSearchTweets:179 - nipafx 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Indexed | |
@Entity | |
public class Tweet { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
@Column(name = "id", updatable = false, nullable = false) | |
private Long id; | |
@Column | |
@Field(name = "postedAt", analyze = Analyze.NO) | |
@DateBridge(resolution = Resolution.MONTH) | |
private Date postedAt; | |
@Column | |
@Field(analyze = Analyze.NO) | |
@Facet | |
private String userName; | |
@Column | |
private String message; | |
@Column | |
private String url; | |
@Version | |
private Long version; | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a FullTextQuery and select Facets as shown in previous code snippets | |
FacetSelection facetSelection = facetMgr.getFacetGroup( "userNameFR" ); | |
facetSelection.selectFacets( facets.get( 0 ) ); | |
List<Tweet> tweets = fullTextQuery.getResultList(); | |
for (Tweet t : tweets) { | |
log.info(t); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
08:10:47,793 INFO TestSearchTweets:213 - Selected Facets | |
08:10:47,795 INFO TestSearchTweets:215 - thjanssen123 3 | |
08:10:47,795 INFO TestSearchTweets:215 - baeldung 1 | |
08:10:47,795 INFO TestSearchTweets:215 - nipafx 1 | |
08:10:47,832 DEBUG SQL:92 - select this_.id as id1_0_0_, this_.message as message2_0_0_, this_.postedAt as postedAt3_0_0_, this_.url as url4_0_0_, this_.userName as userName5_0_0_, this_.version as version6_0_0_ from Tweet this_ where (this_.id in (?, ?, ?)) | |
08:10:47,836 INFO TestSearchTweets:224 - Tweets of Facet [thjanssen123] | |
08:10:47,836 INFO TestSearchTweets:226 - Tweet [id=1, postedAt=2017-01-31 00:00:00.0, userName=thjanssen123, message=The Ultimate Guide to JPQL Queries with JPA, url=http://www.thoughts-on-java.org/jpql/, version=0] | |
08:10:47,837 INFO TestSearchTweets:226 - Tweet [id=2, postedAt=2017-01-24 00:00:00.0, userName=thjanssen123, message=5 tips to write efficient queries with JPA and Hibernate, url=www.thoughts-on-java.org/5-tips-write-efficient-queries-jpa-hibernate/, version=0] | |
08:10:47,837 INFO TestSearchTweets:226 - Tweet [id=3, postedAt=2017-02-02 00:00:00.0, userName=thjanssen123, message=How to automatically validate entities with Hibernate Validator BeanValidation, url=http://www.thoughts-on-java.org/automatically-validate-entities-with-hibernate-validator/, version=0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment