我正在尝试测试REST服务,该服务使用DBUnit和MockMvc从MySQL数据库查询餐馆数据。 除了执行全文搜索的测试外,所有测试都有效。这些总是没有返回结果。测试示例:
@Test
public void searchRestaurantsForSimpleName() throws Exception {
mockMvc.perform(get("/restaurants?query=first").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.restaurantList[0].name").value("first"));
}
执行且未返回任何结果的DAO代码为:
@Query(value = "select *, match(search1) against(?1 in boolean mode) as score1," + ...
MySQL引擎类型为 InnoDB ,仅在构建索引时支持全文搜索。可能问题是DBUnit没有重建索引,因此它没有搜索结果?
您是否有任何其他问题可以解决问题或如何进行全文搜索测试?
修改(更多代码):
RestaurantDao,整个查询:
public interface RestaurantDao extends PagingAndSortingRepository<Restaurant, Long> {
//native query does not support pagination, which has to be done manually
@Query(value = "select *," +
" match(search1) against(?1 in boolean mode) as score1," +
" match(search2) against(?1 in boolean mode) as score2" +
" from restaurant r where" +
" (?2 is null or rating_point >= ?2)" +
" and (?3 is null or rating_point <= ?3)" +
" and (?4 is null or rating_bottle = ?4)" +
" and (?5 is null or ?5 = FALSE or accommodation = ?5)" +
" and (?6 is null or ?6 = FALSE or garden = ?6)" +
" and (?7 is null or rating_star = ?7)" +
" and (?8 is null or oecar = ?8)" +
" and (?9 = TRUE or hidden = ?9)" +
" having (length(?1) = 0 or score1 > 0 or score2 > 0)" +
" order by score1 desc, score2 desc, rating_point desc, rating_star desc, oecar asc, name asc" +
" limit ?10 offset ?11", nativeQuery = true)
List<Restaurant> fullTextSearch(
String query,
Integer minRating,
Integer maxRating,
Integer bottleRating,
Boolean accommodation,
Boolean garden,
Integer stars,
Integer oecar,
boolean includeHidden,
int limit,
int offset
);
MySQL表,由hibernate自动创建(为了便于阅读,我排除了一些字段):
restaurant | CREATE TABLE `restaurant` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`accommodation` bit(1) DEFAULT NULL,
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`garden` bit(1) DEFAULT NULL,
`hidden` bit(1) DEFAULT NULL,
`kitchen_chef` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`oecar` int(11) DEFAULT NULL,
`rating_point` int(11) DEFAULT NULL,
`search1` varchar(255) DEFAULT NULL,
`search2` varchar(255) DEFAULT NULL,
`rating_star` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `search1_hidx` (`search1`),
KEY `search2_hidx` (`search2`),
KEY `accommodation_hidx` (`accommodation`),
KEY `garden_hidx` (`garden`),
KEY `seats_hidx` (`seats`),
KEY `rating_star_hidx` (`rating_star`),
KEY `oecar_hidx` (`oecar`),
FULLTEXT KEY `ft_search1` (`search1`),
FULLTEXT KEY `ft_search2` (`search2`)
) ENGINE=InnoDB AUTO_INCREMENT=909 DEFAULT CHARSET=utf8 |
DBUnit数据定义
<dataset>
<restaurant id="1" name="first" search1="first" search2="wien test" garden="true" accommodation="true" seats="50" rating_point="90" rating_bottle="2" rating_star="5" oecar="3" hidden="false" />
...
</dataset>