Elasticsearch tìm kiếm như thế nào

Để thực hành ES cung cập một tập dữ liệu mẫu, gồm 1000 bản ghi lưu trữ thông tin tài khoản bank, ta sẽ nạp dữ liệu này vào Elasticsearch để thực hành. Mỗi dữ liệu Document có cấu trúc dạng:

{ "account_number": 0, "balance": 16623, "firstname": "Bradshaw", "lastname": "Mckenzie", "age": 29, "gender": "F", "address": "244 Columbus Place", "employer": "Euron", "email": "[email protected]", "city": "Hobucken", "state": "CO" }

Trước tiên tải về file JSON chứa 1000 bản ghi dữ liệu này tại: Tải về JSON Data Bank, lưu nó thành file

curl -X GET localhost:9200/bank/_doc/3?pretty { "_index" : "bank", "_type" : "_doc", "_id" : "3", "_version" : 1, "_seq_no" : 800, "_primary_term" : 1, "found" : true, "_source" : { "account_number" : 3, "balance" : 44947, "firstname" : "Levine", "lastname" : "Burks", "age" : 26, "gender" : "F", "address" : "328 Wilson Avenue", "employer" : "Amtap", "email" : "[email protected]", "city" : "Cochranville", "state" : "HI" } } 6, rồi gõ lệnh sau để nạp nó vào index

curl -X GET localhost:9200/bank/_doc/3?pretty { "_index" : "bank", "_type" : "_doc", "_id" : "3", "_version" : 1, "_seq_no" : 800, "_primary_term" : 1, "found" : true, "_source" : { "account_number" : 3, "balance" : 44947, "firstname" : "Levine", "lastname" : "Burks", "age" : 26, "gender" : "F", "address" : "328 Wilson Avenue", "employer" : "Amtap", "email" : "[email protected]", "city" : "Cochranville", "state" : "HI" } } 7

curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json" curl "localhost:9200/_cat/indices?v"

Giờ bạn đang có Index tên

curl -X GET localhost:9200/bank/_doc/3?pretty { "_index" : "bank", "_type" : "_doc", "_id" : "3", "_version" : 1, "_seq_no" : 800, "_primary_term" : 1, "found" : true, "_source" : { "account_number" : 3, "balance" : 44947, "firstname" : "Levine", "lastname" : "Burks", "age" : 26, "gender" : "F", "address" : "328 Wilson Avenue", "employer" : "Amtap", "email" : "[email protected]", "city" : "Cochranville", "state" : "HI" } } 7 chứa 1000 Document với cấu trúc mỗi Document như trên.

Có thể thử kiểm tra một Document, ví dụ kiểm tra id = 3

curl -X GET localhost:9200/bank/_doc/3?pretty { "_index" : "bank", "_type" : "_doc", "_id" : "3", "_version" : 1, "_seq_no" : 800, "_primary_term" : 1, "found" : true, "_source" : { "account_number" : 3, "balance" : 44947, "firstname" : "Levine", "lastname" : "Burks", "age" : 26, "gender" : "F", "address" : "328 Wilson Avenue", "employer" : "Amtap", "email" : "[email protected]", "city" : "Cochranville", "state" : "HI" } }

Truy vấn đơn giản đến Elesticsearch

Để thực hiện tìm kiếm đến dữ liệu trong index có tên

curl -X GET localhost:9200/bank/_doc/3?pretty { "_index" : "bank", "_type" : "_doc", "_id" : "3", "_version" : 1, "_seq_no" : 800, "_primary_term" : 1, "found" : true, "_source" : { "account_number" : 3, "balance" : 44947, "firstname" : "Levine", "lastname" : "Burks", "age" : 26, "gender" : "F", "address" : "328 Wilson Avenue", "employer" : "Amtap", "email" : "[email protected]", "city" : "Cochranville", "state" : "HI" } } 7 thực hiện

GET /bank/_search { "query": { "match_all": {} } }0 đến

GET /bank/_search { "query": { "match_all": {} } }1, và gắn kèm thông tin cần tìm kiếm dưới dạng JSON.

Ví dụ sau tìm tất cả các Document có trong index /bank/

GET /bank/_search { "query": { "match_all": {} } }

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } } '

Kết quả là:

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "[email protected]", "city" : "Brogan", "state" : "IL" } }, .... ] } }

Bạn cú ý những phần dữ liệu trả về:

  • GET /bank/_search { "query": { "match_all": {} } }2 – thời gian (mili giây) hoàn thành truy vấn

  • GET /bank/_search { "query": { "match_all": {} } }3 - cho biết vượt thời gian (thời gian hoàn thành vượt ngưỡng bị dừng) hay không

  • GET /bank/_search { "query": { "match_all": {} } }4 - cho biết tìm trên bao nhiêu shard (phân đoạn index)

  • GET /bank/_search { "query": { "match_all": {} } }5 - kết quả tìm kiếm

  • GET /bank/_search { "query": { "match_all": {} } }6 - tổng số kết quả tìm được

  • GET /bank/_search { "query": { "match_all": {} } }7 - mảng dữ liệu thực sự trả về (mặc định 10 document đầu tiên)

Trong các câu truy vấn search, bạn có thể đưa vào nhiều loại tham số để có được kết quả thích hợp, ví dụ cho thêm vào tham số

GET /bank/_search { "query": { "match_all": {} } }8 để lấy số kết quả trả về (mặc định nếu không chỉ ra nó sẽ trả về tối đa 10)

GET /bank/_search { "query": { "match_all": {} }, "size": 1 }

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "size": 1 } '

Bạn cũng có thể chỉ ra dữ liệu trả về bắt đầu từ phần tử nào với tham số

GET /bank/_search { "query": { "match_all": {} } }9

GET /bank/_search { "query": { "match_all": {} }, "from": 10, "size": 10 }

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "from": 10, "size": 10 } 'sắp xếp kết quả trả về

Ví dụ các dữ liệu sắp xếp giảm dần theo trường dữ liệu

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } } '0

curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json" curl "localhost:9200/_cat/indices?v"0

curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json" curl "localhost:9200/_cat/indices?v"1

Thực hiện tìm kiếm với Elasticsearch

Phần này sẽ thực hiện các truy vấn tìm kiếm với tham số phức tạp hơn.

Tùy chọn trường dữ liệu trả về

Mặc định Document trả về từ các truy vấn tìm kiếm đúng như cấu trúc nó lưu vào Index, nếu muốn giới hạn lấy những trường dữ liệu cụ thể nào đó thôi thì dùng tham số

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } } '1 với mảng liệt kê các trường dữ liệu muốn này. Ví dụ chỉ lấy các dữ liệu

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } } '2 và

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } } '0 của

curl -X GET localhost:9200/bank/_doc/3?pretty { "_index" : "bank", "_type" : "_doc", "_id" : "3", "_version" : 1, "_seq_no" : 800, "_primary_term" : 1, "found" : true, "_source" : { "account_number" : 3, "balance" : 44947, "firstname" : "Levine", "lastname" : "Burks", "age" : 26, "gender" : "F", "address" : "328 Wilson Avenue", "employer" : "Amtap", "email" : "[email protected]", "city" : "Cochranville", "state" : "HI" } } 7

curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json" curl "localhost:9200/_cat/indices?v"2

curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json" curl "localhost:9200/_cat/indices?v"3

Tìm kiếm dữ liệu phù hợp

Ở các ví dụ trước, sử dụng

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } } '5 ở

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } } '6 có nghĩa là trả về toàn bộ Document có trong Index. Giờ đến lúc sẽ thực hiện tìm kiếm, sẽ sử dụng dến

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } } '7, giả sử tìm các tài khoản bank có

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } } '2 bằng

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } } '9, sẽ viết truy vấn như sau:

curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json" curl "localhost:9200/_cat/indices?v"4

curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json" curl "localhost:9200/_cat/indices?v"5

Tương tự, nếu muốn tìm các tài khoản mà địa chỉ

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "[email protected]", "city" : "Brogan", "state" : "IL" } }, .... ] } } 0 có chứa

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "[email protected]", "city" : "Brogan", "state" : "IL" } }, .... ] } } 1 HOẶC

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "[email protected]", "city" : "Brogan", "state" : "IL" } }, .... ] } } 2

curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json" curl "localhost:9200/_cat/indices?v"6

curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json" curl "localhost:9200/_cat/indices?v"7

Nếu muốn trả về cả cụm từ

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "[email protected]", "city" : "Brogan", "state" : "IL" } }, .... ] } } 3 thì cần đến

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "[email protected]", "city" : "Brogan", "state" : "IL" } }, .... ] } } 4

curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json" curl "localhost:9200/_cat/indices?v"8

curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json" curl "localhost:9200/_cat/indices?v"9Sử dụng điều kiện logic bool trong truy vấn tìm kiếm

Sử đụng điều kiện

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "[email protected]", "city" : "Brogan", "state" : "IL" } }, .... ] } } 5 kết quả trả về khi tất cả các truy vấn là đúng, ví dụ sau tìm tất cả các địa chỉ có chứa từ

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "[email protected]", "city" : "Brogan", "state" : "IL" } }, .... ] } } 1 VÀ

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "[email protected]", "city" : "Brogan", "state" : "IL" } }, .... ] } } 2

curl -X GET localhost:9200/bank/_doc/3?pretty { "_index" : "bank", "_type" : "_doc", "_id" : "3", "_version" : 1, "_seq_no" : 800, "_primary_term" : 1, "found" : true, "_source" : { "account_number" : 3, "balance" : 44947, "firstname" : "Levine", "lastname" : "Burks", "age" : 26, "gender" : "F", "address" : "328 Wilson Avenue", "employer" : "Amtap", "email" : "[email protected]", "city" : "Cochranville", "state" : "HI" } } 0

curl -X GET localhost:9200/bank/_doc/3?pretty { "_index" : "bank", "_type" : "_doc", "_id" : "3", "_version" : 1, "_seq_no" : 800, "_primary_term" : 1, "found" : true, "_source" : { "account_number" : 3, "balance" : 44947, "firstname" : "Levine", "lastname" : "Burks", "age" : 26, "gender" : "F", "address" : "328 Wilson Avenue", "employer" : "Amtap", "email" : "[email protected]", "city" : "Cochranville", "state" : "HI" } } 1

Sử đụng điều kiện

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "[email protected]", "city" : "Brogan", "state" : "IL" } }, .... ] } } 8 nó tương tự như phép logic HOẶC

curl -X GET localhost:9200/bank/_doc/3?pretty { "_index" : "bank", "_type" : "_doc", "_id" : "3", "_version" : 1, "_seq_no" : 800, "_primary_term" : 1, "found" : true, "_source" : { "account_number" : 3, "balance" : 44947, "firstname" : "Levine", "lastname" : "Burks", "age" : 26, "gender" : "F", "address" : "328 Wilson Avenue", "employer" : "Amtap", "email" : "[email protected]", "city" : "Cochranville", "state" : "HI" } } 2

Sử đụng điều kiện

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "[email protected]", "city" : "Brogan", "state" : "IL" } }, .... ] } } 9 tử tự như phép logic PHỦ ĐỊNH

curl -X GET localhost:9200/bank/_doc/3?pretty { "_index" : "bank", "_type" : "_doc", "_id" : "3", "_version" : 1, "_seq_no" : 800, "_primary_term" : 1, "found" : true, "_source" : { "account_number" : 3, "balance" : 44947, "firstname" : "Levine", "lastname" : "Burks", "age" : 26, "gender" : "F", "address" : "328 Wilson Avenue", "employer" : "Amtap", "email" : "[email protected]", "city" : "Cochranville", "state" : "HI" } } 3

Kết hợp nhiều điều kiện

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "[email protected]", "city" : "Brogan", "state" : "IL" } }, .... ] } } 5,

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "[email protected]", "city" : "Brogan", "state" : "IL" } }, .... ] } } 9,

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "[email protected]", "city" : "Brogan", "state" : "IL" } }, .... ] } } 8 vào truy vấn

curl -X GET localhost:9200/bank/_doc/3?pretty { "_index" : "bank", "_type" : "_doc", "_id" : "3", "_version" : 1, "_seq_no" : 800, "_primary_term" : 1, "found" : true, "_source" : { "account_number" : 3, "balance" : 44947, "firstname" : "Levine", "lastname" : "Burks", "age" : 26, "gender" : "F", "address" : "328 Wilson Avenue", "employer" : "Amtap", "email" : "[email protected]", "city" : "Cochranville", "state" : "HI" } } 4

Lọc kết quả Elasticsearch

Để lọc dùng đến

GET /bank/_search { "query": { "match_all": {} }, "size": 1 }3, ví dụ sử dụng loại

GET /bank/_search { "query": { "match_all": {} }, "size": 1 }4 để lọc lấy lấy dữ liệu

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } } '0 trong khoảng nào đó.

curl -X GET localhost:9200/bank/_doc/3?pretty { "_index" : "bank", "_type" : "_doc", "_id" : "3", "_version" : 1, "_seq_no" : 800, "_primary_term" : 1, "found" : true, "_source" : { "account_number" : 3, "balance" : 44947, "firstname" : "Levine", "lastname" : "Burks", "age" : 26, "gender" : "F", "address" : "328 Wilson Avenue", "employer" : "Amtap", "email" : "[email protected]", "city" : "Cochranville", "state" : "HI" } } 5

Trên đây là phần tìm kiếm cơ bản, ngoài ra còn nhiều loại cú pháp tạo query tìm kiếm khác như

GET /bank/_search { "query": { "match_all": {} }, "size": 1 }6,

GET /bank/_search { "query": { "match_all": {} }, "size": 1 }7,

GET /bank/_search { "query": { "match_all": {} }, "size": 1 }8,

GET /bank/_search { "query": { "match_all": {} }, "size": 1 }9,

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "size": 1 } '0 ...