elasticsearchJava学习笔记

es不简易指南

2017-08-30  本文已影响137人  刘阳0292

1.nested Object mapping及查询

//mapping中原来是object的地方替代之,就是 nested object了
//nested对象将不被平整化(flatted object fileds),object中的各字段仍保持关联
//平整化会将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}}  
                      ]  
                   }  
                }  
             }  
           }  
        ]  
     }  
  }  
}  

进阶:Elasticsearch之Nested(嵌套)系列es权威指南-嵌套-查询es权威指南-嵌套-对象(官方文档中文详细解释)

2.父子文档

//建立文档的父子关系要在创建索引的时候在mapping中声明哪个是父文档哪个是子文档。
/创建了一个索引,并制定了2个type和它们之间的父子关系。
PUT [index_name]
{
  "mappings": {
    "branch": {},
    "employee": {
      "_parent": {
        "type": "branch"
      }
    }
  }
}
//索引子文档要注明爸爸是谁,父文档不用标注儿子是谁
//子文档的每条文档设置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"
          }
        }
      }
    }
  }
}
//搜索最少有两个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"
        }
      }
    }
  }

进阶:Elasticsearch索引的父子关系(index parent-child)Elasticsearch Java API(七)--多级嵌套搜索(3级)

3.查看热点线程

//查看cpu占用高且执行时间长的Java线程
GET _nodes/_nodes/hot_threads

4.查看集群统计信息

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. 新建文档数据

ElasticSearch如何添加,检索数据

//新增和更新
PUT mcms_iflow/tbl_iflow_feature/11
{
      "id": "1",
      "tags": "woshi,我是,biaoiqan",
      "title": "title标题",
      "add_time": "1500000000000"
}

//新增文档,默认自增id
POST mcms_iflow/tbl_iflow_feature
{
      "id": "1",
      "tags": "woshi,我是,biaoiqan",
      "title": "title标题",
      "add_time": "1500000000000"
}
上一篇 下一篇

猜你喜欢

热点阅读