<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
public interface AuctionProductESService {
public Long saveAuctionProductBean(AuctionProductBean auctionProductBean);
public List<AuctionProductBean> searchAuctionProductBean(
Integer pageNumber, Integer pageSize, String searchContent);
}
@CacheConfig(cacheNames = "auction_product_es_service")
@Service
public class AuctionProductESServiceImpl implements AuctionProductESService {
@Autowired
AuctionProductBeanRepository auctionProductBeanRepository;
@Override
public Long saveAuctionProductBean(AuctionProductBean auctionProductBean) {
AuctionProductBean auctionProductBeanResult = auctionProductBeanRepository
.save(auctionProductBean);
return auctionProductBeanResult.getId();
}
@Override
public List<AuctionProductBean> searchAuctionProductBean(
Integer pageNumber, Integer pageSize, String searchContent) {
// 分页参数
Pageable pageable = new PageRequest(pageNumber, pageSize);
// Function Score Query
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders
.functionScoreQuery().add(
QueryBuilders.boolQuery().should(
QueryBuilders
.matchQuery("title", searchContent)),
ScoreFunctionBuilders.weightFactorFunction(1000));
// .add(QueryBuilders.boolQuery().should(
// QueryBuilders.matchQuery("crowdfunding_package_id",
// searchContent)),
// ScoreFunctionBuilders.weightFactorFunction(100));
// 创建搜索 DSL 查询
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withPageable(pageable).withQuery(functionScoreQueryBuilder)
.build();
System.out.println("\n searchAuctionProductBean(): searchContent ["
+ searchContent + "] \n DSL = \n "
+ searchQuery.getQuery().toString());
Page<AuctionProductBean> searchPageResults = auctionProductBeanRepository
.search(searchQuery);
return searchPageResults.getContent();
}
@Document(indexName = "auction_product_index", type = "auction_product_bean")
public class AuctionProductBean implements Serializable {