試想一下。若是咱們想找一下含有「月」的古詩,咱們會先去想哪些詩句包含月,例如「牀前明月光」,包含月。這麼在咱們腦海裏就有「牀前明月光」爲KEY,「月」爲Value的索引。html
什麼是倒索引。若是咱們事先在腦海裏創建了,「月」爲KEY,「靜夜思」爲Value的索引,這個問題也就不假思索的回答。這就是倒索引。java
Lucene是如何作到的。創建索引分爲一下幾步:mysql
Near Realtime(NRT)git
近實時,es從數據寫入到數據被搜索到有一個延時(大概1秒),基於es執行的搜索和分析能夠達到妙級github
Clustersql
集羣,包含多個節點,每一個節點屬於哪一個集羣是經過一個配置(集羣名稱)來決定,節點能夠分散到各個機器上。chrome
Node數據庫
節點,集羣中的一個節點,若是默認啓動1個或者多個節點,那麼他們自動組成一個集羣。一個elasticsearch實例即就是一個節點。每一個節點能夠有多個shard,可是primary shard和對應replica shard 不能在同一個節點上。json
indexbash
索引,包含一堆具備類似結構的文檔數據
type
類型,type是index中的一個邏輯數據分類,7.X版本只有一個
document
文檔,es中最小的數據單元,有json串組成,裏面包含多個field,每一個field便是一個數據字段。
mapping
映射,一旦創建,沒法修改,只能刪除索引,從新創建,或者冗餘數據。text:當一個字段是要被全文搜索的。keyword:類型適用於索引結構化的字段。字段只能經過精確值搜索到。
聚合-桶 聚合-指標
分類成爲桶,統計成爲指標
ES的安裝簡單,chrome插件elasticsearch-head從git上找https://github.com/mobz/elasticsearch-head/tree/master/crx。crx文件,解壓成文件夾,導入插件。
版本變更很大,全部的API查官網的一手資料纔是最準確的, 官方Search APIs:www.elastic.co/guide/en/el…
/test
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"field1": {
"type": "text"
}
}
}
}
複製代碼
分頁
:9200/movie/adventure/_search?from=1&size=5
查詢某列
:9200/movie/adventure/1?_source=name
字符串查詢
:9200/movie/adventure/_search?q=name:life
DSL搜索
{
"query": {
"match": {
"tag": "中國"
}
}
}
複製代碼
正浮點型,分數越高,文檔越相關
GET /_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Search" }},
{ "match": { "content": "Elasticsearch" }}
],
"filter": [
{ "term": { "status": "published" }},
{ "range": { "publish_date": { "gte": "2015-01-01" }}}
]
}
}
}
複製代碼
##### 實現如下sql
SELECT product
FROM products
WHERE (price = 20 OR productID = "XHDK-A-1293-#fJ3")
AND (price != 30)
##### 布爾過濾器, 過濾查詢已被棄用,並在ES 5.0中刪除。
必須(must) 匹配
不能(must not) 匹配
至少有一個語句要匹配
{
"bool" : {
"must" : [],
"should" : [],
"must_not" : [],
}
}
##### 查詢
GET /my_store/products/_search
{
"query" : {
"bool" : {
"should" : [
{ "term" : {"price" : 20}},
{ "term" : {"productID" : "XHDK-A-1293-#fJ3"}}
],
"must_not" : {
"term" : {"price" : 30}
}
}
}
}
複製代碼
官方文檔 www.elastic.co/guide/en/el… 2.X版本和5版本,中文官方 www.elastic.co/guide/cn/el…
Elasticsearch多表關聯問題是討論最多的問題之一。ES能夠實現關聯查詢,可是不推薦使用,性能也會有影響。
三種解決方案:
參考鏈接:blog.csdn.net/laoyang360/…
創建索引
/lhhb-order
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"cashname": {
"type": "keyword"
},
"memo_no": {
"type": "keyword"
},
"payment": {
"type": "double"
},
"create":{
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"remark":{
"type": "text"
}
}
}
}
添加數據
/lhhb-order/_doc/1
{
"cashname": "人民幣",
"memo_no": "1233455754534",
"payment": 10000.12345,
"create":"2015-01-01 23:33:12",
"remark":"備註啊書法大賽打發士大夫"
}
基本查詢
term是精確查詢
match是模糊查詢
gt : 大於
lt : 小於
gte : 大於等於
lte :小於等於
{
"query": {
"bool":{
"must": [
{ "range": { "create": {"gte": "2015-02-01"} }}
]
}
}
}
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Search" }},
{ "match": { "content": "Elasticsearch" }}
],
"filter": [
{ "term": { "status": "published" }},
{ "range": { "publish_date": { "gte": "2015-01-01" }}}
]
}
}
}
聚合: 分類統計
{
"size" : 0,
"aggs" : {
"popular_colors" : {
"terms" : {
"field" : "color.keyword"
}
}
}
}
複製代碼
logstash-7.3.2\bin 安裝
logstash-plugin install logstash-input-jdbc
目錄下放驅動文件
文件格式UTF-8
input {
jdbc {
jdbc_driver_library => "mysql-connector-java-5.1.33-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_connection_string => "jdbc:mysql://IP:3306/test"
jdbc_user => "root"
jdbc_password => "****"
schedule => "*/1 * * * *"
jdbc_default_timezone => "Etc/UTC"
statement => "SELECT * FROM biz_transfer_info where id > :sql_last_value"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
last_run_metadata_path => "E:\cyd\WorkPro\ELK\logstash-7.3.2\logstash-7.3.2\log\mysql_test.txt"
use_column_value => true
record_last_run => true
tracking_column => "id"
}
}
output {
stdout {
codec => json_lines
}
elasticsearch {
hosts => "127.0.0.1:9200"
index => "mysql_test"
}
}
複製代碼
.\logstash.bat -f .\mysqldata.conf
最好參考官方文檔
官網上的安裝和運行都很簡單。
kibana報表截圖:elkguide.elasticsearch.cn/kibana/phan…