ElasticSearch入門2: 基本用法

 

 

 

 基本用法:html

 

 1、索引建立json

(啓動集羣和索引請看上一篇文章:http://www.cnblogs.com/liuxiaoming123/p/8081883.html)瀏覽器

1.打開瀏覽器,輸入請求:http://localhost:9100app

2.點擊後搜索bookless

3.點擊OK 顯示建立成功elasticsearch

4.在概覽中查看ide

5.點擊索引信息,查看結構化和非結構化索引信息post

 

 

 

 1.點擊 複合查詢ui

  1.1 加入:book/novel/_mappersspa

  1.2加入:

{
  "novel": {
    "properties": {
      "title": {
        "type": "text"
      }
    }
  }
}

 

    1.3 勾選易讀

  1.4點擊 驗證JSON

  1.5點擊 提交請求

2.點擊概覽,刷新頁面

2.1 點擊索引信息

2.2 mappings 中 不爲空則 表示 結構化索引建立成功

注:上面是結構化索引的建立在head插件中實現的,其中json編寫較爲繁瑣,下面採用postman方式進行建立

1.   在postman中編寫json字符串(採起put提交方式提交)

2.   請求路徑:127.0.0.1:9200/people

3.  json字符串

{
    "settings":{
        "number_of_shards":3,
        "number_of_replicas":1
    },
    "mappings":{
        "man":{
            "properties":{
                "name":{
                    "type":"text"
                },
                "country":{
                    "type":"keyword"
                },
                "age":{
                    "type":"integer"
                },
                "date":{
                    "type":"date",
                    "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                }
            }
        }
    }
}

4.json字符串的各字段介紹:

      settings: 設置

  number_of_shards: 分片數

  number_of_replicas:備份數

  mappings: 索引的映射

  man: 映射名稱,此處的映射名稱只能有一個不能出現多個映射(這是一個設計失誤,後面的版本將再也不支持。官方給出解釋是:https://www.elastic.co/guide/en/elasticsearch/reference/6.0/removal-of-types.html

  properties:屬性的集合 :(它下面的爲各個屬性,都是"屬性名":{"(類型)type":"對應的類型"})注:其中Data屬性有format設置日期格式

5.編寫完畢,postman發送請求

6.瀏覽器刷新請求:http://localhost:9100

 

7.點擊 索引信息

8.查看結構化索引:

{
    "state": "open",
    "settings": {
        "index": {
            "creation_date": "1513925050386",
            "number_of_shards": "3",
            "number_of_replicas": "1",
            "uuid": "vGaF5hq4Te21atouyphn_Q",
            "version": {
                "created": "6010199"
            },
            "provided_name": "people"
        }
    },
    "mappings": {
        "man": {
            "properties": {
                "date": {
                    "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
                    "type": "date"
                },
                "country": {
                    "type": "keyword"
                },
                "name": {
                    "type": "text"
                },
                "age": {
                    "type": "integer"
                }
            }
        }
    },
    "aliases": [],
    "primary_terms": {
        "0": 1,
        "1": 1,
        "2": 1
    },
    "in_sync_allocations": {
        "0": [
            "DkW46MGNQi-fpBkd9Odpbw",
            "EzcKH6oSSOCWMPIrwZVu0Q"
        ],
        "1": [
            "YbF3JOriS6iYJ-57Dc2eNA",
            "h1Zkz1kaS0iAkoA_XekMIQ"
        ],
        "2": [
            "0AtuJLLJQEe_0HeF6CMoRw",
            "oQukwljIS4K7gZ2ZXKJc5g"
        ]
    }
}

 

2、插入

 

 

1.在postman中輸入請求:127.0.0.1:9200/people/man/1 (PUT請求)

2.插入json 數據:

{
    "name":"曉明",
    "country":"china",
    "age":26,
    "date":"1992-08-08"
}

注:people 是索引, man是類型, 1是文檔ID

3.點擊send發送

4.查看瀏覽器 http://localhost:9100

 未刷新以前:

刷新以後

5.點擊數據瀏覽查看所插入的數據:此時ID是手動指定的ID

 

 

1.瀏覽器輸入請求:127.0.0.1:9200/people/man/ (post請求) 點擊send發送

json字符串:

{
    "name":"Auto曉明",
    "country":"Autochina",
    "age":26,
    "date":"1992-08-08"
}

2.查看瀏覽器 :http://localhost:9100

3.點擊數據瀏覽:此時新生成的爲自動生成的ID

 

 

3、修改

 

 

1.postman提交請求:127.0.0.1:9200/people/man/1/_update

2.請求json串爲:

{
    "doc":{
        "name":"修改曉明"
    }
}

4.查看瀏覽器:http://localhost:9100

 

 

 

第一種方式:

1. postman輸入請求:127.0.0.1:9200/people/man/1/_update(post請求)

2.json字符串:

{
    "script":{
        "lang":"painless",
        "inline":"ctx._source.age += 10"
    }
}

注:script 是腳本標識

  lang 是腳本語言

  painless 是內置腳本語言

  inline 是指定腳本內容

  ctx 是腳本上下文

  _source 爲當前文檔

 

3.瀏覽器查看:http://localhost:9100

第二種方式:

1. postman輸入請求:127.0.0.1:9200/people/man/1/_update(post請求)

2.json字符串:

{
    "script":{
        "lang":"painless",
        "inline":"ctx._source.age = params.age",
        "params":{
            "age":100
        }
    }
}

 

3.查看瀏覽器:http://localhost:9100

 

、刪除

 

 

 

1.postman輸入請求: 127.0.0.1:9200/people/man/1 (DELETE方式)

 

2.查看瀏覽器:http://localhost:9200/ :ID等於1的已經成功刪除

 

第一種:經過head插件直接刪除索引

1.查看瀏覽器 :http://localhost:9100 刪除book索引

 

2.輸入 :刪除 點擊 「好」

3.顯示刪除成功

4.點擊關閉

 

第二種:經過postman刪除索引 刪除people

1.postman輸入請求:127.0.0.1:9200/people(DELETE方式) 點擊send發送請求

2.查看瀏覽器:http://localhost:9100 

 、查詢

 

 

1.準備:

   1.1 post建立機構化索引:

   1.1.1 請求:127.0.0.1:9200/people (PUT方式)

   1.1.2 json字符串:

{
    "settings":{
        "number_of_shards":3,
        "number_of_replicas":1
    },
   "novel": {
    "properties": {
      "title": {
        "type": "text"
      },
      "author": {
        "type": "keyword"
      },
      "tittle": {
        "type": "text"
      },
      "publish_date": {
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
        "type": "date"
      }
    }
  }
}

 

 1.2 postman插入數據:

  1.2.1 請求:127.0.0.1:9200/book/novel/1 (post方式)

  1.2.2 json 數據

{
    "name":"曉明1",
    "country":"china1",
    "age":26,
    "date":"1992-08-08"
}

 

 

 

1.postman請求:127.0.0.1:9200/book/novel/1(get方式)

 

 

 

1.postman請求:127.0.0.1:9200/book/_search(post請求)

2.請求json串:

 2.1 所有查詢

{
    "query":{
        "match_all":{}
    },
    "from":1,
    "size":1
}

 2.2 按關鍵字查詢(含有按指定字段排序)

{
    "query":{
        "match":{
            "name":"曉明"
        }
    },
    "sort":[
        {"_id":"desc"}
        ]
}

3.解釋請求json串:

match_all 是所有查詢

query 是查詢關鍵字

from 是從哪裏查

size 是顯示多少條數據

sort 是排序設置

 

 

 

 

(多組聚合)

 1.postman請求:127.0.0.1:9200/book/_search(post請求)

 2.請求json字符串:

{
   "aggs":{
           "group_by_name":{
               "terms":{
                   "field":"name"
               }
           },
           "group_by_country":{
               "terms":{
                   "field":"country"
               }
           },
           "group_by_date":{
               "terms":{
                   "field":"date"
               }
           }
           
   }
}

 3.解析請求json字符串:

aggs:聚合查詢關鍵字

group_by_word_count :聚合條件的名字(名字可自定義)

terms 關鍵詞

field 是指定字段

4.點擊send發送獲得以下查詢信息:

{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "name": "曉明",
                    "country": "china",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "8",
                "_score": 1,
                "_source": {
                    "name": "曉明",
                    "country": "china",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 1,
                "_source": {
                    "name": "曉明9",
                    "country": "china9",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "name": "曉明1",
                    "country": "china1",
                    "age": 26,
                    "date": "1992-08-08"
                }
            }
        ]
    },
    "aggregations": {
        "group_by_date": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 713232000000,
                    "key_as_string": "1992-08-08T00:00:00.000Z",
                    "doc_count": 4
                }
            ]
        },
        "group_by_country": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "china",
                    "doc_count": 2
                },
                {
                    "key": "china1",
                    "doc_count": 1
                },
                {
                    "key": "china9",
                    "doc_count": 1
                }
            ]
        },
        "group_by_name": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "明",
                    "doc_count": 4
                },
                {
                    "key": "曉",
                    "doc_count": 4
                },
                {
                    "key": "1",
                    "doc_count": 1
                },
                {
                    "key": "9",
                    "doc_count": 1
                }
            ]
        }
    }
}

 

注: 若聚合查詢報錯 請參考:http://www.cnblogs.com/liuxiaoming123/p/8117786.html

PUT         127.0.0.1:9200/book/_mapping/novel/ 


{
  "properties": {
    "name": { 
      "type":     "text",
      "fielddata": true
    },
     "country": { 
      "type":     "text",
      "fielddata": true
    }
   
  }
}

注:聚合查詢其餘用法:

計算年齡的統計 總條數、最大值、最小值、平均值、總和 若:stats改爲min則只顯示最小值

請求URL:

POST  127.0.0.1:9200/book/_search

請求json:

{
   "aggs":{
           "group_by_age":{
               "stats":{
                   "field":"age"
               }
           }
           
   }
}

返回結果:

{
    "took": 33,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "name": "曉明",
                    "country": "china",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "8",
                "_score": 1,
                "_source": {
                    "name": "曉明",
                    "country": "china",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 1,
                "_source": {
                    "name": "曉明9",
                    "country": "china9",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "name": "曉明1",
                    "country": "china1",
                    "age": 26,
                    "date": "1992-08-08"
                }
            }
        ]
    },
    "aggregations": {
        "group_by_age": {
            "count": 4,
            "min": 26,
            "max": 26,
            "avg": 26,
            "sum": 104
        }
    }
}

相關文章
相關標籤/搜索