下面列出了org.hibernate.search.mapper.orm.session.SearchSession#org.hibernate.search.mapper.orm.Search 实例代码,或者点击链接到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();
}
@Transactional
void onStart(@Observes StartupEvent ev) throws InterruptedException {
// only reindex if we imported some content
if (Book.count() > 0) {
Search.session(em)
.massIndexer()
.startAndWait();
}
}
@GET
@Path("author/search")
@Transactional
public List<Author> searchAuthors(@QueryParam String pattern,
@QueryParam Optional<Integer> size) {
List<Author> authors = Search.session(em)
.search(Author.class)
.where(f -> pattern == null || pattern.trim().isEmpty() ? f.matchAll()
: f.simpleQueryString()
.fields("firstName", "lastName", "books.title").matching(pattern))
.sort(f -> f.field("lastName_sort").then().field("firstName_sort"))
.fetchHits(size.orElse(20));
return authors;
}
@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";
}
@Test
@Transactional(timeout=30)
public void testSearchIndex() throws Exception {
final List<DocumentContentData> result = Search.session(entityManager).search(DocumentContentData.class).asEntity()
.predicate(t -> t.match().fields("content").matching("programmering")).fetchHits(500);
assertTrue("expect some result",result.size()> 0);
}
@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 )
);
}
@POST
@Path("/client/reindex")
@Transactional(TxType.NEVER)
public void reindex() throws InterruptedException {
Search.mapping( entityManagerFactory )
.scope( Client.class )
.massIndexer()
.startAndWait();
}
@GET
@Path("/client/search")
public List<ClientRetrieveDto> search(@QueryParam("terms") String terms) {
List<Client> result = Search.session( Panache.getEntityManager() )
.search( Client.class )
.predicate( f -> f.simpleQueryString()
.fields( "name", "assignedManager.name" )
.matching( terms )
.defaultOperator( BooleanOperator.AND )
)
.fetchHits( 20 );
return result.stream().map( mapper::toDto ).collect( Collectors.toList() );
}
private HibernateOrmSearchQueryResultDefinitionContext<Dog> createDogSearch() {
return Search.getSearchSession(entityManager)
.search(Dog.class);
}
@Override
public void updateSearchIndex() throws InterruptedException {
Search.session(entityManager).massIndexer().transactionTimeout(TIMEOUT_IN_SECONDS).startAndWait();
}
/**
* Gets the full text entity manager.
*
* @return the full text entity manager
*/
protected final SearchSession getFullTextEntityManager() {
return Search.session(getEntityManager());
}