elasticsearch的基礎語句介紹 彙總功能很強大 可以分析資料,elasticsearch彙總

來源:互聯網
上載者:User

elasticsearch的基礎語句介紹 彙總功能很強大 可以分析資料,elasticsearch彙總

elasticsearch彙總功能很強大 可以分析資料,比MySQL更加強大。可以根據爬山歸類到運動。。。
下面是 安裝以及一些基礎命令
安裝
curl -L -O http://download.elasticsearch.org/PATH/TO/VERSION.zip <1>
unzip elasticsearch-VERSION.zipcdelasticsearch−VERSION

外掛程式
./bin/plugin -i elasticsearch/marvel/latest

禁用監控,關閉Marvel:
echo ‘marvel.agent.enabled: false’ >> ./config/elasticsearch.yml

運行
cd /usr/share/elasticsearch
./bin/elasticsearch

開啟另一個終端運行測試:
curl ‘http://localhost:9200/?pretty’
你能看到以下資訊:{
“status”: 200,
“name”: “Shrunken Bones”,
“version”: {
“number”: “1.4.0”,
“lucene_version”: “4.10”
},
“tagline”: “You Know, for Search”
}
這說明你的ELasticsearch叢集已經啟動並運行

資料庫對比
關聯式資料庫(Relational DB) -> 庫(Databases) -> 表(Tables) -> 行(Rows) -> 列(Columns)
Elasticsearch -> 索引(Indices) -> 類型(Types) -> 文檔(Documents) -> 欄位(Fields)

建立資料
PUT /megacorp/employee/1
{
“first_name” : “John”,
“last_name” : “Smith”,
“age” : 25,
“about” : “I love to go rock climbing”,
“interests”: [ “sports”, “music” ]
}

PUT /megacorp/employee/3
{
“first_name”: “Douglas”,
“last_name”:”Fir”,
“age”:35,
“about”:”I like to build cabinets”,
“interests”:[“forestry”]
}
注意到路徑 /megacorp/employee/1 包含三部分資訊:
名字
說明
megacorp 索引名
employee 類型名
1 這個職員的ID

搜尋全部員工的請求:
GET /megacorp/employee/_search
查詢一條
GET /megacorp/employee/_search?q=_id:AU3leyatuy05ZviT4RuM

DSL語句查詢 json格式
GET /megacorp/employee/_search
{
“query” : {
“match” : {
“last_name” : “Smith”
}
}
}
全文模糊 全文檢索搜尋
GET /megacorp/employee/_search
{
“query” : {
“match” : {
“about” : “rock climbing”
}
}
}
查詢 about 包含完整短語“rock climbing”的員工。
GET /megacorp/employee/_search
{
“query” : {
“match_phrase” : {
“about” : “rock climbing”
}
}
}

高亮查詢結果
GET /megacorp/employee/_search
{
“query”:{
“match_phrase”:{
“about”:”rock climbing”
}
},
“highlight”:{
“fields”:{
“about” : {}
}
}
}

牛逼的彙總功能
GET /megacorp/employee/_search
{
“aggs”: {
“all_interests”: {
“terms”: { “field”: “interests” }
}
}
}

結果
“aggregations”: {
“all_interests”: {
“doc_count_error_upper_bound”: 0,
“sum_other_doc_count”: 0,
“buckets”: [
{
“key”: “music”,
“doc_count”: 4
},
{
“key”: “sports”,
“doc_count”: 4
},
{
“key”: “forestry”,
“doc_count”: 3
}
]
}
}

自動把員工的愛好歸類 比如 爬山 歸類到運動!!彙總功能就是在分析資料

姓”Smith”的興趣愛好最多的是
GET /megacorp/employee/_search
{
“query”: {
“match”: {
“last_name”: “smith”
}
},
“aggs”: {
“all_interests”: {
“terms”: {
“field”: “interests”
}
}
}
}

統計每種興趣下職員的平均年齡
GET /megacorp/employee/_search
{
“aggs” : {
“all_interests” : {
“terms” : { “field” : “interests” },
“aggs” : {
“avg_age” : {
“avg” : { “field” : “age” }
}
}
}
}
}

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.