# Elastic Search + Kibana Document ## Tổng quan và cài đặt ElasticSearch gồm có 2 phần: - server lưu trữ và xử lý data: Elastic - giao diện web tương tác với server: Kibana docker-compose.yaml ``` version: '3' services: elasticsearch: image: elasticsearch:7.14.1 container_name: elasticsearch environment: - node.name=elasticsearch - cluster.name=datasearch - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" - cluster.initial_master_nodes=elasticsearch ulimits: memlock: soft: -1 hard: -1 ports: - "9200:9200" volumes: - esdata:/usr/share/elasticsearch/data kibana: image: kibana:7.14.1 ports: - "5601:5601" volumes: esdata: driver_opts: device: ./esdata o: bind type: ntfs ``` #### Sau khi run Elastic bằng `docker-compose up -d` ta check lại bằng cách ``` # Trên command line $ curl -X GET localhost:9200 ``` Truy cập Kibana trên trình duyệt tại http://localhost:5601/ Check server elastic bằng Dev tools trên kibana http://localhost:5601/app/dev_tools#/console ``` GET / ``` ## Tạo và cập nhật Index, Document trong Elasticsearch Từ màn hình Dev tools: - Healthcheck `GET /_cat/health?v` ![](https://i.imgur.com/ATp6aud.png) ### Index có thể hiểu giống như table ở trong RDBMS - Truy vấn số lượng index `GET /_cat/indices?v` - Tạo index **product** `PUT /product?pretty` ![](https://i.imgur.com/K9meY3K.png) - Xóa index **article** `DELETE /article?pretty` ### Document hiểu là các bản ghi trong Index - Thêm document ``` PUT /product/_doc/1 # 1 là id tự setup cho document, nếu ko có thì id sẽ tự sinh { "name": "Macbook Pro", "price": 36000000 } ``` ![](https://i.imgur.com/YGOnAXs.png) - Đọc chi tiết document theo ID ``` GET /product/_doc/1?pretty ``` - Xóa document ``` DELETE /product/_doc/1 ``` - Bulk create document Mỗi document tương ứng 2 dòng: Dòng 1 là tên index, id nếu có Dòng 2 là nội dung document bằng json ``` POST /_bulk {"index": {"_index": "product", "_id": 5}} {"name": "Cafe Trung Nguyen", "price": 75000, "score": [10, 10, 9]} {"index": {"_index": "product", "_id": 6}} {"name": "Loa Xiaomi", "price": 250000, "score": [10, 10, 7]} {"index": {"_index": "product", "_id": 7}} {"name": "La Roche Possay", "price": 360000, "score": [6, 10, 9]} {"index": {"_index": "product", "_id": 8}} {"name": "Tinh dau Thao Nguyen", "price": 25000, "score": [4, 5, 9]} ``` ![](https://i.imgur.com/DG4q1WI.png) - Bulk create data từ file ``` $ curl -XPOST "http://localhost:9200/shake/_bulk?pretty&refresh" -H 'Content-Type: application/json' --data-binary "@shakespeare_6.0.json" ``` ## Truy vấn tìm kiếm trên Elasticsearch Tải file dữ liệu mẫu của ELK [TẠI ĐÂY](https://raw.githubusercontent.com/xuanthulabnet/elasticsearch-learning/master/photo/accounts.json) ``` # import data to ELK server $ curl -XPOST "http://localhost:9200/bank/_bulk?pretty&refresh" -H 'Content-Type: application/json' --data-binary "@accounts.json" ``` - Truy vấn trong ELK ``` # query: cau truy van # size: SL kq trả về GET /bank/_search?pretty { "query": {"match_all": {"_name": "Amber"}}, "size": 20 } ``` ![](https://i.imgur.com/5HzKbkQ.png) - Truy vấn với sắp xếp và paging ``` GET /bank/_search?pretty { "query": {"match_all": {}}, "sort": [ { "balance": { "order": "desc" } } ], "size": 5, "from": 10 } ``` - Truy vấn với số field xác định ``` GET /bank/_search?pretty { "query": {"match_all": {}}, "_source": ["account_number", "firstname", "email"], "sort": [ { "balance": { "order": "desc" } } ] } ``` - Truy vấn tìm kiếm theo field Để ý kết quả trả về được sắp xếp theo score, cái nào có score lớn hơn lên trước ``` GET /bank/_search?pretty { "query": {"match": {"address":"mill lane"}} } ``` ![](https://i.imgur.com/REk4oKP.png) - Truy vấn kiểu bool Case: Kết quả cần có cả mill và lane - dùng **must** ``` GET /bank/_search?pretty { "query": { "bool": { "must": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } } ``` Case: Kết quả có mill hoặc lane - dùng **should** ``` GET /bank/_search?pretty { "query": { "bool": { "should": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } } ``` Case: Kết quả không có mill - dùng **must_not** - Kết hợp must, must_not ``` GET /bank/_search?pretty { "query": { "bool": { "must": [ { "match": { "age": 36 } } ], "must_not": [ { "match": { "state": "SC" } }, { "match": { "state": "AZ" } }, { "match": { "state": "WA" } } ] } } } ``` ## Xây dựng chức năng tìm kiếm của ứng dụng với PHP và Elasticsearch ... # Interview question ## Shard là gì? ES cung cấp cơ chế cho phép chia index thành nhiều phần nhỏ các phần này được gọi là shards. khi tạo một index có thể configure số lượng shards mà chúng ta muốn lưu trữ index này. Mỗi shards, bản thân nó là một index đầy đủ chức năng và độc lập do đó chúng có thể được host bởi bất kỳ node (server) nào.