es簡易指南

head
sense

1.查看所有index:

GET _cat/indices

2.查看索引的type、mapping:

GET  [indexName]/_mapping
GET  [indexName]/_mappings
GET  [indexName]/_mapping/[type]

3.查看某索引的某類型的數(shù)據(jù):

GET [indexName]/[type]/_search

GET browser_device_idx/tbl_device/_search
{
  "query":
  {
    "match": {
      "groupid":1174
    }
  }
}

GET browser_device_idx/tbl_device/_search
{
  "query":
  {
    "match": {
      "groupid":1174
    }
  }
}

4.過濾:

DELETE [indexName]/[type]/_query
{
    "query": {
        "filtered": {
            "query":  { "match": { "email": "business opportunity" }},
            "filter": { "term": { "folder": "inbox" }}
        }
    }
}

5.查看分詞器分詞:

常用分詞器:standard whitespace simple english pinyin ik
GET /_analyze?analyzer=ik
{
  "text":"ahaha傻笑high"
}
GET /_analyze
{
  "analyzer":"ik",
  "text":"ahaha傻笑high"
}

6.查詢指定的部分字段:

通過限定 _source 字段來請求指定字段
GET [indexName]/[type]/_search
{
    "query":   { "match_all": {}},
    "_source": [ "title", "created" ]
}

7.范圍查詢:

GET [indexName]/[type]/_search
{
  "query":{
   "range":{
   "字段名":{//范圍[148,200)
      "gte":148,
      "lt":200
     }
   }
  }
}

8.重建索引:

使用【scan-scoll】來批量讀取舊索引的文檔,然后將通過【bulk API】來將它們推送給新的索引。
GET /old_index/_search?search_type=scan&scroll=1m
{
  //獲取數(shù)據(jù)應(yīng)該通過范圍查詢進行
  //scan-scoll性能比通過范圍在各個分片排序再獲取好
    "query": {
        "range": {
            "date": {
                "gte":  "2014-01-01",
                "lt":   "2014-02-01"
            }
        }
    },
    "size":  1000
}
POST _bulk//批量操作
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

9.索引別名alias aliases:

檢測這個別名指向哪個索引:
GET /*/_alias/my_index
檢測哪些別名指向這個索引:
GET /my_index_v1/_alias/*
新索引中添加別名的同時從舊索引中刪除它。這個操作需要原子化,所以我們需要用 _aliases :
POST /_aliases
{
    "actions": [
        { "remove": { "index": "my_index_v1", "alias": "my_index" }},
        { "add":    { "index": "my_index_v2", "alias": "my_index" }}
    ]
}

10.范圍查詢并排序:

GET [indexName]/[type]/_search
{
     "query": {
        "range": {
            "date": {
                "gte":  "2014-01-01",
                "lt":   "2014-02-01"
            }
        }
    },
    "sort":  [
        {
          "createTime":"desc"
        }
      ]
}

11.排序

GET mcms_iflow/tbl_news_iflow/_search
{
    "_source": ["abstract_desc","title","channel_id"],
  "query":{
    "match_phrase": {
      "abstract_desc":"可愛"
    }
  },
  "sort": [
    {
      "channel_id": {//一級
        "order": "asc",
        "missing":"_last"http://該字段缺省的文檔排到最后
      }
    },
        {
      "add_time": {//二級
        "order": "desc"
      }
    }
  ]
}

12.驗證查詢語句:

GET [indexName]/[type]/_validate/query?explain
{//能夠查看es對查詢語句的解釋
     "query": {
         ...
    }
}

13.查詢結(jié)果數(shù)量、分頁:

//如果無from、size設(shè)置,from默認(rèn)為0,size默認(rèn)為10,默認(rèn)查詢出前10個
GET mcms_iflow/tbl_news_iflow/_search
{
  "query":{
    "match_all": {}
  },
    "size":20//查前20條
}
//查詢出從第10條開始,取20條
GET mcms_iflow/tbl_news_iflow/_search
{
  "query":{
    "match_all": {}
  },
    "from":10,
    "size":20
}
//如果搜索size大于10000,需要設(shè)置index.max_result_window參數(shù) 
//注意:size的大小不能超過index.max_result_window這個參數(shù)的設(shè)置,默認(rèn)為10,000。 
PUT _settings
{
    "index": {
        "max_result_window": "10000000"
    }
}

進階:Elasticsearch——分頁查詢From&Size VS scroll、Elasticsearch from+size 超過10000結(jié)果解決方法


14.搜索 實例:

//(標(biāo)題含k或摘要含k)&&(范圍[a,b])&&(品類為x&&文本類型為y)
//bool可以嵌套,match_phrase精確匹配,范圍可lt lte gt gte,
GET mcms_iflow/tbl_news_iflow/_search
{
  "query":{
    "bool":{
      "must":[{
        "bool":{
          "should":[{
            "match":{
              "title":{
                "query":"櫻子",
                "type":"phrase"
              }
            }
          },{
            "match_phrase":{
              "abstract_desc":"櫻子"
            }
          }]
        }
      },{
        "range":{
          "add_time": {
            "from": "1487927871500",
            "to": "1492185599999",
            "include_lower":true,
            "include_upper":true
          }
        }
      },{
        "bool":{
          "must":[{
            "term":{
              "variety_type": "1"
            }
          },{
            "term":{
              "articletype": "1"
            }
          }]
        }
      }  ]
    }
  }
}

15.聚合-高級統(tǒng)計

POST mcms_iflow/tbl_news_iflow/_search
{
  "size":0,
  "aggs":{
    "grades_stats":{
      "extended_stats": {
        "field": "add_time"
      }
    }
  }
}

更多聚合:Elasticsearch分析聚合官方-Aggregations

16.短語匹配 match_phrase

GET mcms_iflow/tbl_news_iflow/_search
{
  "_source": ["abstract_desc","title"],//只顯示指定字段
  "query":{
    "match_phrase": {//短語匹配
      "abstract_desc":"可愛"
    }
  }
}

es不簡易指南


參考資料:
23 款實用的 Elasticsearch 查詢示例

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 博客原文一博客原文二 翻譯作品,水平有限,如有錯誤,煩請留言指正。原文請見 官網(wǎng)英文文檔 起步 Elasticse...
    rabbitGYK閱讀 3,396評論 0 68
  • 介紹 elasticsearch是一個高效的、可擴展的全文搜索引擎 基本概念 Near Realtime(NRT)...
    imsilence閱讀 879評論 0 0
  • SQL 優(yōu)化(載錄于:http://m.jb51.net/article/5051.htm) 作者: (一)深入淺...
    yuantao123434閱讀 805評論 0 7
  • “啊”我從那個嚇人的夢驚醒過來,發(fā)現(xiàn)只是一個夢而已,就沒有太在意。我穿好衣服走了出來,可我并沒有發(fā)現(xiàn)床頭邊有一個骨...
    王星冉閱讀 928評論 0 1
  • 這學(xué)期伊始,我們有門商務(wù)英語課。 是位女老師,我很佩服她,她用了兩節(jié)課,就完全“馴化”了我們。 當(dāng)然,講英語是主要...
    蒔語閱讀 422評論 0 0

友情鏈接更多精彩內(nèi)容