Elasticsearch 备忘

Elasticsearch7.0版本在查询时需要增加 “track_total_hits”:true 来强制进行准确的计数,默认为 “track_total_hits”:10000,

而且返回的hits下结构也和Elasticsearch 6.x 版本的稍有不同,可能还有其他不同的地方,所以将6.x 升级到7.0还是要小心一些

  

 创建index  (注意:7.0版本之前mappings中需要增加_doc,7.0版之后已去掉)

 PUT http://10.10.x.x:9200/test

{
    "settings": {
        "index": {
            "number_of_shards": "5",
            "number_of_replicas": "1"
        }
    },
    "mappings": {
        "_doc": {
            "properties": {
                "pk": {
                    "type": "long"
                },
                "create_time": {
                    "format": "yyyy-MM-dd HH:mm:ss",
                    "type": "date"
                },
                "name": {
                    "type": "keyword"
                }
            }
        }
    }
}

排序

{
    "size": 10,
    "query": {
        "bool": {
            "must": [
            {
                "term":
                {
                    "request_uri":"/blog_expand/get_article_expand"
                }
            }
            ]
        }
    },
    "sort": [
    {    

     "request_time": { "order": "desc"   }
      
    }
  ]
}

 根据条件删除

POST http://IP:9200/索引名/_delete_by_query

POST http://10.10.14.111:9200/t_sys_login_log/_delete_by_query

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "area_code": "abc"
                    }
                },
                {
                    "range": {
                        "request_time": {
                            "gte": "2018-08-21 00:00:01",
                            "lt": "2018-08-21 23:59:59"
                        }
                    }
                }
            ]
        }
    }
}

创建mapping

PUT http://192.168.0.1:9200/student_zipper

{
    "mappings": {
        "doc": {
            "properties": {
                "student_id": {
                    "type": "keyword"
                },
                "student_name": {
                    "type": "keyword"
                },
                "dw_start_date": {
                    "type": "date",
                    "format": "yyyy-MM-dd"
                },
                "dw_end_date": {
                    "type": "date",
                    "format": "yyyy-MM-dd"
                }
            }
        }
    }
}

插入数据

POST http://192.168.0.1:9200/student_zipper/doc

{
    "student_id": "0417EB01-3F8F-4BC1-9012-436BFF466FBD",
    "student_name": "严立诚",
    "dw_start_date": "2019-03-10",
    "dw_end_date": "9999-01-01"
}
{
    "size": 0,
    "query": {
        "bool": {
            "must": [{
                "term": {
                    "area_code": "abcd"
                }
            }, {
                "term": {
                    "b_use": "1"
                }
            }, {
                "terms": {
                    "xb_id": [1, 2]
                }
            }, {
                "terms": {
                    "stage_id": [4, 5, 6]
                }
            }, {
                "terms": {
                    "district_id": ["300353","300356","300358","300349","300352","300354","310056","300357","300355","300351","300350","300360","300359"]
                }
            }, {
                "range": {
                    "entrance_year": {
                        "gte": "2013",
                        "lte": "2018"
                    }
                }
            }]
        }
    },
    "aggs": {
        "district_id": {
            "terms": {
                "field": "district_id",
                "size": 1000
            },
            "aggs": {
                "stage_id": {
                    "histogram": {
                        "field": "stage_id",
                        "min_doc_count": 0,
                        "extended_bounds": {
                            "min": 4,
                            "max": 6
                        },
                        "interval": 1
                    },
                    "aggs": {
                        "entrance_year": {
                            "histogram": {
                                "field": "entrance_year",
                                "min_doc_count": 0,
                                "extended_bounds": {
                                    "min": 2013,
                                    "max": 2018
                                },
                                "interval": 1
                            },
                            "aggs": {
                                "xb_id": {
                                    "histogram": {
                                        "field": "xb_id",
                                        "min_doc_count": 0,
                                        "extended_bounds": {
                                            "min": 1,
                                            "max": 2
                                        },
                                        "interval": 1
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/kgdxpr/p/8856239.html