Elasticsearch in action

1. 建立Blog存储库,继承 ElasticsearchRepository 类,重写 findByTitleLikeOrContentLike 方法。

public interface BlogRepository extends ElasticsearchRepository<Blog, String> {
	/**
	 * 分页查询博客
	 * @param title
	 * @param content
	 * @param pageable
	 * @return
	 */
	Page<Blog> findByTitleLikeOrContentLike(String title, String content, Pageable pageable);
}

2. 测试类,存储数据到blogRepository,

@RunWith(SpringRunner.class)
@SpringBootTest
public class BlogRepositoryTest {
	
	@Autowired
	private BlogRepository blogRepository;
	
	@Test
	public void testFindByTitleLikeOrFindContentLike() {
		// 清空所有
		blogRepository.deleteAll();
		
		blogRepository.save(new Blog("1", "好了歌", "世上皆知神仙好"));
		blogRepository.save(new Blog("2", "功名利禄忘不了", "窗前明月光"));
		blogRepository.save(new Blog("3", "疑似地上霜", "举头望明月"));
		
		Pageable pageable = new PageRequest(0, 20);
		Page<Blog> page = blogRepository.findByTitleLikeOrContentLike("好了歌","窗前", pageable);
		assertThat(page.getTotalElements()).isEqualTo(2);
	}
}

3. 向URL传参,返回查询结果。

@RestController
@RequestMapping("/blogs")
public class BlogController {
	
	@Autowired
	private BlogRepository blogRepository;
	
	@GetMapping
	public List<Blog> list(@RequestParam(value = "title", required = false, defaultValue = "") String title,
	                       @RequestParam(value = "content", required = false, defaultValue = "") String content,
	                       @RequestParam(value = "pageIndex", required = false, defaultValue = "0") int pageIndex,
	                       @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {
		
		// 数据在 Test 里面先初始化了,这里只管取数据
		Pageable pageable = new PageRequest(pageIndex, pageSize);
		Page<Blog> page = blogRepository.findByTitleLikeOrContentLike(title, content, pageable);
		return page.getContent();
	}
}

Summary Elasticsearch in Action teaches you how to build scalable search applications using Elasticsearch. You'll ramp up fast, with an informative overview and an engaging introductory example. Within the first few chapters, you'll pick up the core concepts you need to implement basic searches and efficient indexing. With the fundamentals well in hand, you'll go on to gain an organized view of how to optimize your design. Perfect for developers and administrators building and managing search-oriented applications. Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology Modern search seems like magic—you type a few words and the search engine appears to know what you want. With the Elasticsearch real-time search and analytics engine, you can give your users this magical experience without having to do complex low-level programming or understand advanced data science algorithms. You just install it, tweak it, and get on with your work. About the Book Elasticsearch in Action teaches you how to write applications that deliver professional quality search. As you read, you'll learn to add basic search features to any application, enhance search results with predictive analysis and relevancy ranking, and use saved data from prior searches to give users a custom experience. This practical book focuses on Elasticsearch's REST API via HTTP. Code snippets are written mostly in bash using cURL, so they're easily translatable to other languages. What's Inside What is a great search application? Building scalable search solutions Using Elasticsearch with any language Configuration and tuning About the Reader For developers and administrators building and managing search-oriented applications. About the Authors Radu Gheorghe is a search consultant and software engineer. Matthew Lee Hinman develops highly available, cloud-based systems. Roy Russo is a specialist in predictive analytics.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值