es-03-DSL的簡單使用


以下操作在kibana中進行, 如果在linux的shell中, 請使用

curl -Xget 'http://node1:9200/index/type/id' -d '{ ... }' 的形式, 其中 -d 是傳參

1, 獲取集群狀態 

1), 查看健康狀況: 

GET /_cat/health?v

2), 查看節點: 

GET /_cat/nodes?v

2, index操作(類似數據庫databases)

1, index操作

1), 創建數據庫

put lag
{
    "settings": { "index": { "number_of_shards": 5, "number_of_replicas": 1 } } }

 

2), 修改settings

分片不可以更改, 副本可以更改

put lag/_settings
{
    "number_of_shards": 3 }

3), 獲取所有的索引

    get _all

 獲取索引

get lag/_settings
get _all/settings
get .kibana,lagou/_settings
get _settings

 

4), 查看所有index

GET /_cat/indices?v

5), 創建數據

put customer/_doc/1?pretty
{
  "name": "vini"
}

4), 查詢

get customer/_doc/1?pretty

5), 刪除index

delete customer?pretty
GET /_cat/indices?v

 

2, document操作(類似記錄 record)

1), 保存文檔

index/type/id 不指定id的話, 會自動生成uuid

put lag/job/1
{
    "title": 'python 爬蟲“,
    ‘salary”: 15000,
    ’city‘: ’bj‘,
    ’company“:   {
        "name": "Baidu",
        "company_addr": "bj"
    },
    "publish_date": "2018"
}

2), 獲取文檔

get lagou/job/1

或者

看下面query

 

3), 修改數據

PUT /customer/_doc/1/_update?pretty
{
  "name": "wenbronk"
}

就可以將原來的name進行更改

4), 使用post進行修改, 只修改某些字段

只能更新已經存在的id, 增量修改, 沒有的字段會添加, 有的會覆蓋

post lagou/doc/1/_update?pretty
{
  "doc": {
    "name": "vini",
    "age": 28
  }
}

5), 進行簡單的腳本計算

post customer/_doc/1/_update?pretty
{
  "script": "ctx._source.age += 5"
  
}

6), 刪除document

DELETE /customer/_doc/1?pretty

3, batch處理

可以合並多個操作, 比如index, delete, update, 也可以從一個index導入另一個index

1), 批量插入數據

每條數據由2行構成, delete除外, 第一行為元數據行, 第二行為數據行, upsert比較特殊, 可能為upsert, doc, 或者script

元數據必須放在一行!!!!!

POST /customer/_doc/_bulk?pretty
{"index":{"_id":"1"}}    # 針對哪個索引完成的
{"name": "John Doe" }    # 數據行, 必須放在一行, 不能做美化
{"index":{"_id":"2"}}
{"name": "Jane Doe" }

如果不寫index或者type, 需要在元數據中指定

2), 執行修改第一條, 刪除第二條

delte操作, 只有一行,沒有元數據行

POST /customer/_doc/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}

單條出錯不影響, 會繼續執行剩下的

3), 批量修改

post _bulk?pretty
{
    "update": {"_index": "lag", "_type": "job", "_id": 1}
    {"doc": {"fileds": "values"}
}

 

4), 批量獲取

get _mget{
    "docs": [
        {"index": "tested",
        "_type": "job",
        "_id": 1
        },
        {"_index": "lag",
            "_type": "job2",
            "_id": 2
        }
    ]
}

或者同一個index或者同一個 type

get lagou/job1
{
    "docs": [
        {"_id": 1},
        {"_id": 2}
    ]
}

或者縮寫

get lagou/job1
{
  "ids": [1, 2]
}

 

4, 查詢

基本查詢, 組合查詢, 過濾查詢

1), 導入基礎數據

https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json

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

2), 使用 q 進行查詢

GET /bank/_search?q=*&sort=account_number:asc&pretty

只獲取部分字段

get lag/job/1?_source

3)  使用body體進行查詢

from 從哪開始, size: 取多少條, sort: 排序

使用 wildcard 進行 * 通配符查詢

4), match 分詞匹配, 部分匹配

a. match_all 查詢所有

get /bank/_search
{
  "query": {"match_all": {}}, 
  "from": 10,
  "size": 10,
  "sort": [
    {"account_number": "asc"}
    ]
}

_source: 顯示取字段

get bank/_search
{
  "query": {"match": {
    "age": 37
  }}, 
  "_source": [
    "account_number", "age", "address"
    ]
  
}

5), macth_parse 短語匹配

會將 查詢進行分詞, 滿足所有分詞才會返回結果

term: 完全匹配, 不分詞

get bank/_search
{
  "query": {"match_phrase": {
    "address": "mill lane", 
“slop”: 6 # 必須大於設置的詞距離才會被搜索到
}}, "_source": [ "account_number", "age", "address" ] }

 

6) term查詢, 完全匹配

如果沒有指定schema是什么類型的, 可能會查詢失敗

get /ban/_search
{
    "query" : {
        "term" : {
            "abc": "1234"
        }
    }
}

terms 查詢

可傳入多個詞, 只要有一個匹配, 就可以被查詢到

get /ban/_search
{
    "query" : {
        "term" : {
            "abc": ["1234", “568”, “23”]
        }
    }
}

 

7), 使用range查詢, 范圍查詢

get /ban/_search
{
    "query": {
        "range": {
            "price": {
                "gte": 10,
                "lte": 99
            }
        }
    }
}

8) multi_match: 多字段匹配

get /bank/_search 
{
  "query': {
    "bool": {
      "must": {
        "multi_match": {
          "operator": "and",
          "fileds": [ "name", "author^3"] # 把titil的權重提高, 分值較高的
          "query": "Guide"
        }
      },
      "filter": { 
        "terms": {
          "price": [ 35.99, 188.99]
        }
      }
    }
  }
}

5 bool匹配

1) must

get bank/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {"address": "mill"}},
        {"match": {"address": "lane"}}
        ]
      }
    }
  }
}

2) or 匹配, should

GET /bank/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}

3) must_not匹配

GET /bank/_search
{
  "query": {
    "bool": {
      "must_not": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}

4) 混搭

GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "age": "40" } }
      ],
      "must_not": [
        { "match": { "state": "ID" } }
      ]
    }
  }
}
get lag/testjob/_search
{
    "query":{
        "bool": {
            "should": [
                {"term": {"title"; "python"}},
                {"bool": {
                    "must": [
                        {"term": {"title": "es"}}, 
                        {"term": {"salary": 30}}
                    ]
                }
            }
        }
    }
 }
select * from test job where title = 'python' or
(title = 'es' and salary = 30)

 

5)  fliter查詢, es5.x之后, 被 bool 替換, 包裹在bool查詢內

1), 使用filtre實現 gte lte

GET /bank/_search
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000, 
"boost": 2.0
} } } } } }
GET /bank/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "abc": "123"
        }
      }
    }
  }
}

 fitler查詢多個值

GET /bank/_search
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "term": [‘adb’, ‘12']
      }
    }
  }
}

判斷字段是否存在

exists

7, 聚合查詢

默認 limit 10

size : 0 為了不顯示搜索結果

GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword"
      }
    }
  }
}

相當於

SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC LIMIT 10;

2), 增加avg聚合

GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword",
        "order": {
          "average_balance": "desc"
        }
      },
      "aggs": {
        "average_balance": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}

3), from-to, 控制查詢的返回數量, 其實就是分頁

from: 從..開始

to: 到..結束

size: 10

GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_age": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 40
          },
          {
            "from": 40,
            "size": 10
          }
        ]
      },
      "aggs": {
        "group_by_gender": {
          "terms": {
            "field": "gender.keyword"
          },
          "aggs": {
            "average_balance": {
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }
  }
}

 4), sort

get lagou/_search
{
    'query': {
        'match_all': {}
    }
    "sort": [{
        "comments": {
             "order": "asa"
         }
    }]
}

 注意, 被排序的字段, 必須被存儲, 即  stored: true


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM