**1. 封裝一個實體類**
```java
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "product", shards = 3, replicas = 1)
public class Product {
/** 產品唯一標識 */
@Id
private Long id;
/** 產品名稱 */
@Field(type = FieldType.Text)
private String title;
/** 產品品牌 */
@Field(type = FieldType.Keyword)
private String brand;
/** 產品價格 */
@Field(type = FieldType.Double)
private Double price;
/** 產品圖片地址 */
@Field(type = FieldType.Text, index = false)
private String images;
}
```
**2. 繼承接口ElasticsearchRepository**
```java
/**
* 繼承接口ElasticsearchRepository<實體類, 實體類中的ID類型>,便可以調用接口的
* 方法與Elasticsearch交互了。
*
* 在接口中聲明的方法的方法名是有一定規律的,
* 查詢以 find開始、新增和更新以save開始、刪除以delete開始、統計以count開始、存在exists開始。
* 聲明一個方法按照從左到右編寫的話,IDEA是可以給出參考的方法名的。
*/
@Repository
public interface ProductDao extends ElasticsearchRepository<Product, Long> {
/** 根據產品名稱查詢產品 */
List<Product> findByTitle(String title);
/** 查詢價格在[price1, price2]之間的產品 */
List<Product> findByPriceBetween(double price1, double price2);
/** 根據品牌查詢 */
List<Product> findByBrand(String brand);
/** 根據品牌查詢,然后按照價格降序排序 */
List<Product> findByBrandOrderByPriceDesc(String brand);
/** 根據品牌與價格區間查詢 */
List<Product> findByBrandAndPriceBetween(String brand, double price1, double price2);
}
```
更過命名規則參考官方文檔:https://docs.spring.io/spring-data/elasticsearch/docs/4.2.6/reference/html/#elasticsearch.query-methods.criterions
**3. 調用接口中的方法**
```java
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {SpringDataEsApplication.class})
public class ProductControllerTest {
@Autowired
private ProductDao productDao;
/**
* 新增或更新。
* 新增與更新都是調用save方法,沒有該條數據時是新增,已經有了就是更新。
*/
@Test
public void saveDocument() {
Product product = Product.builder()
.id(1001L)
.title("華為 Mate 40 Pro 4G版")
.brand("華為")
.price(5899.0d)
.images("https://res.xx455CB694209CB8A2mp.png")
.build();
Product result = productDao.save(product);
}
/**
* 刪除文檔。
*/
@Test
public void deleteDocument() {
Product product = Product.builder().id(1001L).build();
productDao.delete(product);
}
/**
* 批量新增文檔。//調用deleteAll便可實現批量刪除
*/
@Test
public void saveAll() {
List<Product> products = new ArrayList<>();
products.add(new Product(1001L, "華為 Mate 30 Pro 4G版", "華為", 1000.0d, "http://1.jpg"));
products.add(new Product(1002L, "華為 Mate 40 Pro 4G版", "華為", 2000.0d, "http://2.jpg"));
products.add(new Product(1003L, "華為 Mate 50 Pro 4G版", "華為", 3000.0d, "http://3.jpg"));
Iterable<Product> iterable = productDao.saveAll(products);
}
/**
* 查詢所有。
* 根據id字段升序排序
*/
@Test
public void findAll() {
Iterable<Product> iterable = productDao.findAll(Sort.by(Sort.Direction.ASC, "id"));
iterable.forEach(product -> {
System.out.println(product.toString());
});
//Product(id=1001, title=華為 Mate 30 Pro 4G版, brand=華為, price=1000.0, images=http://1.jpg)
//Product(id=1002, title=華為 Mate 40 Pro 4G版, brand=華為, price=2000.0, images=http://2.jpg)
//Product(id=1003, title=華為 Mate 50 Pro 4G版, brand=華為, price=3000.0, images=http://3.jpg)
}
/**
* 根據id查詢產品
*/
@Test
public void findById() {
Product product = Product.builder().id(1001L).build();
Product result = productDao.findById(product.getId()).get();
System.out.println(result);
//Product(id=1001, title=華為 Mate 30 Pro 4G版, brand=華為, price=1000.0, images=http://1.jpg)
}
/**
* 按照產品名稱查詢。
*/
@Test
public void findByTitle() {
List<Product> products = productDao.findByTitle("華為 Mate 30 Pro 4G版");
products.forEach(System.out::println);
//Product(id=1001, title=華為 Mate 30 Pro 4G版, brand=華為, price=1000.0, images=http://1.jpg)
}
/**
* 按照價格區間查詢。
*/
@Test
public void findByPriceBetween() {
List<Product> products = productDao.findByPriceBetween(1000, 3000);
products.forEach(System.out::println);
//Product(id=1002, title=華為 Mate 40 Pro 4G版, brand=華為, price=2000.0, images=http://2.jpg)
//Product(id=1003, title=華為 Mate 50 Pro 4G版, brand=華為, price=3000.0, images=http://3.jpg)
//Product(id=1001, title=華為 Mate 30 Pro 4G版, brand=華為, price=1000.0, images=http://1.jpg)
}
/**
* 按照品牌查詢。
*/
@Test
public void findByBrand() {
List<Product> products = productDao.findByBrand("華為");
products.forEach(System.out::println);
//Product(id=1002, title=華為 Mate 40 Pro 4G版, brand=華為, price=2000.0, images=http://2.jpg)
//Product(id=1003, title=華為 Mate 50 Pro 4G版, brand=華為, price=3000.0, images=http://3.jpg)
//Product(id=1001, title=華為 Mate 30 Pro 4G版, brand=華為, price=1000.0, images=http://1.jpg)
}
/**
*按照品牌查詢,然后按照價格降序排序
*/
@Test
public void findByBrandOrderByPriceDesc() {
List<Product> products = productDao.findByBrandOrderByPriceDesc("華為");
products.forEach(System.out::println);
//Product(id=1003, title=華為 Mate 50 Pro 4G版, brand=華為, price=3000.0, images=http://3.jpg)
//Product(id=1002, title=華為 Mate 40 Pro 4G版, brand=華為, price=2000.0, images=http://2.jpg)
//Product(id=1001, title=華為 Mate 30 Pro 4G版, brand=華為, price=1000.0, images=http://1.jpg)
}
/**
* 根據品牌與價格區間查詢。
*/
@Test
public void findByBrandAndPriceBetween() {
List<Product> products = productDao.findByBrandAndPriceBetween("華為", 2000, 3000);
products.forEach(System.out::println);
//Product(id=1002, title=華為 Mate 40 Pro 4G版, brand=華為, price=2000.0, images=http://2.jpg)
//Product(id=1003, title=華為 Mate 50 Pro 4G版, brand=華為, price=3000.0, images=http://3.jpg)
}
/**
* 分頁查詢
*/
@Test
public void findByPageable() {
//根據價格降序排序
Sort sort = Sort.by(Sort.Direction.DESC, "price");
int currentPage = 0; //從第0頁開始
int pageSize = 2; //每一頁顯示2條數據
PageRequest request = PageRequest.of(currentPage, pageSize, sort);
Page<Product> products = productDao.findAll(request);
products.forEach(System.out::println);
//Product(id=1003, title=華為 Mate 50 Pro 4G版, brand=華為, price=3000.0, images=http://3.jpg)
//Product(id=1002, title=華為 Mate 40 Pro 4G版, brand=華為, price=2000.0, images=http://2.jpg)
}
}
```
- Elasticsearch是什么
- 全文搜索引擎
- Elasticsearch與Solr
- 數據結構
- 安裝Elasticsearch
- Linux單機安裝
- Windows單機安裝
- 安裝Kibana
- Linux安裝
- Windows安裝
- es基本語句
- 索引操作
- 文檔操作
- 映射操作
- 高級查詢
- es-JavaAPI
- maven依賴
- 索引操作
- 文檔操作
- 高級查詢
- es集群搭建
- Linux集群搭建
- Windows集群搭建
- 核心概念
- 索引(Index)
- 類型(Type)
- 文檔(Document)
- 字段(Field)
- 映射(Mapping)
- 分片(Shards)
- 副本(Replicas)
- 分配(Allocation)
- 系統架構
- 分布式集群
- 單節點集群
- 故障轉移
- 水平擴容
- 應對故障
- 路由計算
- 分片控制
- 寫流程
- 讀流程
- 更新流程
- 多文檔操作流程
- 分片原理
- 倒排索引
- 文檔搜索
- 動態更新索引
- 近實時搜索
- 持久化變更
- 段合并
- 文檔分析
- 內置分析器
- 分析器使用場景
- 測試分析器
- 指定分析器
- 自定義分析器
- 文檔處理
- 文檔沖突
- 樂觀并發控制
- 外部系統版本控制
- es優化
- 硬件選擇
- 分片策略
- 合理設置分片數
- 推遲分片分配
- 路由選擇
- 寫入速度優化
- 批量數據提交
- 優化存儲設備
- 合理使用合并
- 減少Refresh的次數
- 加大Flush設置
- 減少副本的數量
- 內存設置
- 重要配置
- es常見問題
- 為什么要使用Elasticsearch
- master選舉流程
- 集群腦裂問題
- 索引文檔流程
- 更新和刪除文檔流程
- 搜索流程
- ES部署在Linux時的優化方法
- GC方面ES需要注意的點
- ES對大數據量的聚合實現
- 并發時保證讀寫一致性
- 字典樹
- ES的倒排索引
- Spring Data Elasticsearch
- 環境搭建
- 索引操作
- 文檔操作