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
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

 

 

==================================================================

 

根据年龄进行分组,分组信息通过文档数从大到小排序(通过文档数)#

 

 
POST /nba/_search
{
  "query": {
    "term": {
      "teamNameEn": {
        "value": "Rockets"
      }
    }
  },
  "aggs": {
    "aggsAge": {
      "terms": {
        "field": "age",
        "size": 10,
        "order": {
          "_count": "desc"
        }
      }
    }
  },
  "size": 0
}

 

每支球队按该队所有球员的平均年龄进行分组排序(通过分组指标值)#

 

 
POST /nba/_search
{
  "query": {
    "term": {
      "teamNameEn": {
        "value": "Rockets"
      }
    }
  },
  "aggs": {
    "avgAge": {
      "avg": {
        "field": "age"
      }
    }
  },
  "size": 0
}

 

按球队平均年龄进行分组排序(指定值列表)#

 

 
POST /nba/_search
{
  "aggs": {
    "aggsTeamName": {
      "terms": {
        "field": "teamNameEn",
        "include": [
          "Lakers",
          "Rockets",
          "Warriors"
        ],
        "exclude": [
          "Warriors"
        ],
        "size": 30,
        "order": {
          "avgAge": "desc"
        }
      },
      "aggs": {
        "avgAge": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  },
  "size": 0
}

 

按球队平均年龄进行分组排序(正则表达式匹配值)#

 

 
POST /nba/_search
{
  "aggs": {
    "aggsTeamName": {
      "terms": {
        "field": "teamNameEn",
        "include": "Lakers|Ro.*|Warriors.*",
        "exclude": "Warriors",
        "size": 30,
        "order": {
          "avgAge": "desc"
        }
      },
      "aggs": {
        "avgAge": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  },
  "size": 0
}

 

按20,20-35,35这样分组#

 

 
POST /nba/_search
{
  "aggs": {
    "ageRange": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "to": 20
          },
          {
            "from": 20,
            "to": 35
          },
          {
            "to": 35
          }
        ]
      }
    }
  },
  "size": 0
}

 

年龄按20,20-35,35这样分组(起别名)#

 

按出生年月分组#

 

 
POST /nba/_search
{
  "aggs": {
    "birthDayRange": {
      "date_range": {
        "field": "birthDay",
        "format": "MM-yyy",
        "ranges": [
          {
            "to": "01-1989"
          },
          {
            "from": "01-1989",
            "to": "01-1999"
          },
          {
            "from": "01-1999",
            "to": "01-2009"
          },
          {
            "from": "01-2009"
          }
        ]
      }
    }
  },
  "size": 0
}

 

posted @ 2018-04-16 15:47  缤纷世界  阅读(246)  评论(0编辑  收藏  举报