下面列出了怎么用org.hibernate.search.mapper.orm.session.SearchSession的API类实例代码及写法,或者点击链接到github查看源代码。
@GET
@Path("/search")
@Produces(MediaType.TEXT_PLAIN)
public String testSearch() {
SearchSession searchSession = Search.session(entityManager);
List<Person> person = searchSession.search(Person.class)
.where(f -> f.match().field("name").matching("john"))
.sort(f -> f.field("name_sort"))
.fetchHits(20);
assertEquals(2, person.size());
assertEquals("John Grisham", person.get(0).getName());
assertEquals("John Irving", person.get(1).getName());
person = searchSession.search(Person.class)
.where(f -> f.nested().objectField("address").nest(
f.match().field("address.city").matching("london")))
.sort(f -> f.field("name_sort"))
.fetchHits(20);
assertEquals(1, person.size());
assertEquals("David Lodge", person.get(0).getName());
return "OK";
}
@POST
@Path("/reindex")
public Response reindex(@QueryParam("limit") Long limit,
@QueryParam("idfetchsize") Integer idfetchsize,
@QueryParam("fetchsize") Integer fetchsize,
@QueryParam("threads") Integer threads) {
SearchSession searchSession = Search.session( em );
MassIndexer indexer = searchSession.massIndexer( Page.class, User.class )
.dropAndCreateSchemaOnStart( true )
.typesToIndexInParallel( 2 )
.batchSizeToLoadObjects( fetchsize == null ? 200 : fetchsize )
.idFetchSize( idfetchsize == null ? 200 : idfetchsize )
.threadsToLoadObjects( threads == null ? 5 :threads )
.cacheMode( CacheMode.IGNORE ); // Cache is likely to do more harm than good in our case (very few relations)
if ( limit != null ) {
indexer.limitIndexedObjectsTo( limit );
}
indexer.start();
return Response.accepted().build();
}
@PUT
@Path("/purge")
@Produces(MediaType.TEXT_PLAIN)
public String testPurge() {
SearchSession searchSession = Search.session(entityManager);
searchSession.workspace().purge();
return "OK";
}
@PUT
@Path("/refresh")
@Produces(MediaType.TEXT_PLAIN)
public String testRefresh() {
SearchSession searchSession = Search.session(entityManager);
searchSession.workspace().refresh();
return "OK";
}
@GET
@Path("/search-empty")
@Produces(MediaType.TEXT_PLAIN)
public String testSearchEmpty() {
SearchSession searchSession = Search.session(entityManager);
List<Person> person = searchSession.search(Person.class)
.where(f -> f.matchAll())
.fetchHits(20);
assertEquals(0, person.size());
return "OK";
}
@PUT
@Path("/mass-indexer")
@Produces(MediaType.TEXT_PLAIN)
public String testMassIndexer() throws InterruptedException {
SearchSession searchSession = Search.session(entityManager);
searchSession.massIndexer().startAndWait();
return "OK";
}
@Override
public SearchResult<Page> search(String term, PageSort sort, int offset, int limit) {
SearchSession searchSession = Search.session( getEm() );
return new SearchResult<>( searchSession.search( Page.class )
.where( f -> {
if ( term == null || term.isEmpty() ) {
return f.matchAll();
}
else {
return f.match()
.field( "title" ).boost( 2.0f )
.field( "content" )
.matching( term );
}
} )
.sort( f -> {
switch ( sort ) {
case TITLE:
return f.field( "title_sort" );
case RELEVANCE:
default:
return f.score();
}
} )
.fetch( offset, limit )
);
}
/**
* Gets the full text entity manager.
*
* @return the full text entity manager
*/
protected final SearchSession getFullTextEntityManager() {
return Search.session(getEntityManager());
}