es不簡易指南

1.nested Object mapping及查詢

//mapping中原來是object的地方替代之,就是 nested object了
//nested對象將不被平整化(flatted object fileds),object中的各字段仍保持關(guān)聯(lián)
//平整化會(huì)將object中的字段解析成object_name.object_filed_name的形式
PUT [index_name]
{  
  "mappings":{  
     "blogpost":{  
         "properties":{  
             "comments":{  
                "type":"nested",  
                "include_in_parent":true,  
                "properties":{  
                   "name":    {"type":"string"    },  
                   "comment": { "type": "string"  },  
                   "age":     { "type": "short"   },  
                   "stars":   { "type": "short"   },  
                   "date":    { "type": "date"    }  
                }  
             }  
         }  
     }  
  }  
}  
//nested查詢
GET [index_name]/[type_name]/_search
{  
  "query":{  
     "bool":{  
        "must":[  
           {"match":{"title":"eggs"}},  
           {  
             "nested":{  
                "path":"comments",  
                "query":{  
                   "bool":{  
                      "must":[  
                         {"match":{"comments.name":"john"}},  
                         {"match":{"comments.age":28}}  
                      ]  
                   }  
                }  
             }  
           }  
        ]  
     }  
  }  
}  

進(jìn)階:Elasticsearch之Nested(嵌套)系列es權(quán)威指南-嵌套-查詢、es權(quán)威指南-嵌套-對象(官方文檔中文詳細(xì)解釋)

2.父子文檔

//建立文檔的父子關(guān)系要在創(chuàng)建索引的時(shí)候在mapping中聲明哪個(gè)是父文檔哪個(gè)是子文檔。
/創(chuàng)建了一個(gè)索引,并制定了2個(gè)type和它們之間的父子關(guān)系。
PUT [index_name]
{
  "mappings": {
    "branch": {},
    "employee": {
      "_parent": {
        "type": "branch"
      }
    }
  }
}
//索引子文檔要注明爸爸是誰,父文檔不用標(biāo)注兒子是誰
//子文檔的每條文檔設(shè)置parent屬性的value為父文檔id
PUT [index_name]/company/employee/1?parent=london&pretty
{
  "name":  "Alice Smith",
  "dob":   "1970-10-24",
  "hobby": "hiking"
}
//通過子文檔查父文檔
//搜索含有1980年以后出生的employee的branch
GET [index_name]/branch/_search
{           
  "query": {      
    "has_child": {       
      "type": "employee",
      "query": {  
        "range": {
          "dob": {             
            "gte": "1980-01-01"
          }
        }
      }
    }
  }
}
//搜索最少有兩個(gè)employee的branch
GET [index_name]/branch/_search
{
  "query": {
    "has_child": {
      "type":"employee",
      "min_children": 2,
      "query": {
        "match_all": {}
      }
    }
  }
}
//通過父文檔查子文檔
GET [index_name]/employee/_search
{
  "query": {       
    "has_parent": {    
      "type": "branch", 
      "query": {  
        "match": {       
          "country": "UK"
        }
      }
    }
  }

進(jìn)階:Elasticsearch索引的父子關(guān)系(index parent-child)、Elasticsearch Java API(七)--多級嵌套搜索(3級)

3.查看熱點(diǎn)線程

//查看cpu占用高且執(zhí)行時(shí)間長的Java線程
GET _nodes/_nodes/hot_threads

4.查看集群統(tǒng)計(jì)信息

GET _stats
GET _stats?pretty'

5. 禁用all字段

PUT my_index
{
  "mappings": {
    "type_1": { 
      "properties": {...}
    },
    "type_2": { 
      "_all": {
        "enabled": false//禁用
      },
      "properties": {...}
    }
  }
}

6. 刪除文檔

//刪除type下所有
DELETE /mytest/test/_query
{
"query": {
"match_all": {}
}
}
//刪除指定id文檔
DELETE /website/blog/1234

7. 新建mapping

es 官方 put mapping

PUT mcms_iflow/_mapping/tbl_iflow_feature 
{
  "_all": {
    "enabled": false
  },
  "properties": {
    "id": {
      "type": "string"
    },
    "tags": {
      "type": "string"
    },
    "title": {
      "type": "string"
    },
    "add_time": {
      "type": "long"
    }
  }
}

8. 新建文檔數(shù)據(jù)

ElasticSearch如何添加,檢索數(shù)據(jù)

//新增和更新
PUT mcms_iflow/tbl_iflow_feature/11
{
      "id": "1",
      "tags": "woshi,我是,biaoiqan",
      "title": "title標(biāo)題",
      "add_time": "1500000000000"
}

//新增文檔,默認(rèn)自增id
POST mcms_iflow/tbl_iflow_feature
{
      "id": "1",
      "tags": "woshi,我是,biaoiqan",
      "title": "title標(biāo)題",
      "add_time": "1500000000000"
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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