您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
elasticSearch按日期查询索引
发布时间:2021-08-25 17:00:30编辑:雪饮阅读()
那么这里所谓的按日期查询索引是指的是之前索引名称也要按日期来建立,比如下面这样的一个索引的建立请求如:
http://localhost:9200/accountdetail-2021.08.23/type1/1 put
请求体:
{
"AccountID": "2021.08.23",
"Available": "可用资金",
"Balance": "期货结算准备金",
"BrokerID": "经纪公司代码",
"CashIn": "资金差额",
"CloseProfit": "平仓盈亏",
"Commission": "手续费",
"Credit": "信用额度",
"CurrMargin": "当前保证金总额",
"CurrencyID": "币种代码",
"DeliveryMargin": "投资者交割保证金",
"Deposit": "入金金额",
"ExchangeDeliveryMargin": "交易所交割保证金",
"ExchangeMargin": "交易所保证金",
"FrozenCash": "冻结的资金",
"FrozenCommission": "冻结的手续费",
"FrozenMargin": "冻结的保证金",
"FundMortgageAvailable": "货币质押余额",
"FundMortgageIn": "货币质入金额",
"FundMortgageOut": "货币质出金额",
"Interest": "利息收入",
"InterestBase": "利息基数",
"Mortgage": "质押金额",
"MortgageableFund": "可质押货币金额",
"PositionProfit": "持仓盈亏",
"PreBalance": "上次结算准备金",
"PreCredit": "上次信用额度",
"PreDeposit": "上次存款额",
"PreFundMortgageIn": "上次货币质入金额",
"PreFundMortgageOut": "上次货币质出金额",
"PreMargin": "上次占用的保证金",
"PreMortgage": "上次质押金额",
"Reserve": "基本准备金",
"ReserveBalance": "保底期货结算准备金",
"SettlementID": "结算编号",
"SpecProductCloseProfit": "特殊产品持仓盈亏",
"SpecProductCommission": "特殊产品手续费",
"SpecProductExchangeMargin": "特殊产品交易所保证金",
"SpecProductFrozenCommission": "特殊产品冻结手续费",
"SpecProductFrozenMargin": "特殊产品冻结保证金",
"SpecProductMargin": "特殊产品占用保证金",
"SpecProductPositionProfit": "特殊产品持仓盈亏",
"SpecProductPositionProfitByAlg": "根据持仓盈亏算法计算的特殊产品持仓盈亏",
"TradingDay": "交易日",
"Withdraw": "出金金额",
"WithdrawQuota": "可取资金"
}
那么上面这个索引名称中日期部分” 2021.08.23”正好是2天之前的日期,因为今天是2021.08.25
那么我们一个简单的按日期查询两天前的索引就可以这样来写了。如:
http://localhost:9200/<accountdetail-{now-2d{YYYY.MM.dd}}>/_search get
响应结果:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "accountdetail-2021.08.23",
"_type": "type1",
"_id": "1",
"_score": 1.0,
"_source": {
"AccountID": "2021.08.23",
"Available": "可用资金",
"Balance": "期货结算准备金",
"BrokerID": "经纪公司代码",
"CashIn": "资金差额",
"CloseProfit": "平仓盈亏",
"Commission": "手续费",
"Credit": "信用额度",
"CurrMargin": "当前保证金总额",
"CurrencyID": "币种代码",
"DeliveryMargin": "投资者交割保证金",
"Deposit": "入金金额",
"ExchangeDeliveryMargin": "交易所交割保证金",
"ExchangeMargin": "交易所保证金",
"FrozenCash": "冻结的资金",
"FrozenCommission": "冻结的手续费",
"FrozenMargin": "冻结的保证金",
"FundMortgageAvailable": "货币质押余额",
"FundMortgageIn": "货币质入金额",
"FundMortgageOut": "货币质出金额",
"Interest": "利息收入",
"InterestBase": "利息基数",
"Mortgage": "质押金额",
"MortgageableFund": "可质押货币金额",
"PositionProfit": "持仓盈亏",
"PreBalance": "上次结算准备金",
"PreCredit": "上次信用额度",
"PreDeposit": "上次存款额",
"PreFundMortgageIn": "上次货币质入金额",
"PreFundMortgageOut": "上次货币质出金额",
"PreMargin": "上次占用的保证金",
"PreMortgage": "上次质押金额",
"Reserve": "基本准备金",
"ReserveBalance": "保底期货结算准备金",
"SettlementID": "结算编号",
"SpecProductCloseProfit": "特殊产品持仓盈亏",
"SpecProductCommission": "特殊产品手续费",
"SpecProductExchangeMargin": "特殊产品交易所保证金",
"SpecProductFrozenCommission": "特殊产品冻结手续费",
"SpecProductFrozenMargin": "特殊产品冻结保证金",
"SpecProductMargin": "特殊产品占用保证金",
"SpecProductPositionProfit": "特殊产品持仓盈亏",
"SpecProductPositionProfitByAlg": "根据持仓盈亏算法计算的特殊产品持仓盈亏",
"TradingDay": "交易日",
"Withdraw": "出金金额",
"WithdrawQuota": "可取资金"
}
}
]
}
}
那么上面这个请求就是命中了这个索引
那么同样的,如果我创建一个这样的请求:
http://localhost:9200/accountdetail-2021.07.25/type1/6 put
请求体:
{
"AccountID": "2021.07.25",
"bbq": "上次货币质入金额",
"PreFundMortgageOut": "上次货币质出金额",
"PreMargin": "上次占用的保证金",
"PreMortgage": "上次质押金额"
}
那么我使用日期查询http://localhost:9200/<accountdetail-{now-M{YYYY.MM.dd}}>/_search get
也能查到:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "accountdetail-2021.07.25",
"_type": "type1",
"_id": "6",
"_score": 1.0,
"_source": {
"AccountID": "2021.07.25",
"bbq": "上次货币质入金额",
"PreFundMortgageOut": "上次货币质出金额",
"PreMargin": "上次占用的保证金",
"PreMortgage": "上次质押金额"
}
}
]
}
}
因为现在是2021.08.25,那么now-M就正好表示的是一个月之前的,而上面这次创建的这个索引也正好就是一个月之前的索引。
那么我们再来创建一个当前时间的索引,不包含日的,现在时间是2021.08.25,那么我们创建一个索引就是2021.08的索引如:
http://localhost:9200/accountdetail-2021.08/type1/6 put
请求头:
{
"AccountID": "2021.08",
"bbq": "上次货币质入金额",
"PreFundMortgageOut": "上次货币质出金额",
"PreMargin": "上次占用的保证金",
"PreMortgage": "上次质押金额"
}
那么要查看2021.08的索引,则get请求如:
http://localhost:9200/<accountdetail-{now{YYYY.MM}}>/_search
也是可以匹配搜索到该索引的。
从以上这些索引查询请求示例中可以发现这些个请求有一个规律:
<static_name{date_math_expr{date_format}}>
http://localhost:9200/<accountdetail-{now-2d{YYYY.MM.dd}}>/_search
static_name是表达式的一部分,在每个日期数学索引(如帐户详细信息)中保持相同。 date_math_expr包含动态确定日期和时间的数学表达式,如now-2d。date_format包含日期在索引中写入的格式,如YYYY.MM.dd。 如果今天的日期是2021年8月25日,则<accountdetail- {now-2d {YYYY.MM.dd}}>将返回accountdetail-2021.08.23。
还有就是说上面这种搜索是对索引名进行匹配的,并不是对索引中的类型或者数据进行匹配的。
关键字词:elasticSearch,日期,索引,查询索引
相关文章
- elasticSearch参数ignore_unavailable与allow_no_indi
- elasticSearch参数allow_no_indices的使用
- elasticSearch的expand_wildcards查询
- elasticSearch关闭索引(_close)
- elasticSearch字符串参数ignore_unavailable的用处
- elasticSearch通配符,与-
- elasticSearch通配符*
- elasticSearch的_all查询关键字(在所有索引中查询)
- 完全群集重新启动与滚动升级以及elasticSearch多索引
- elasticSearch安装及配置elasticSearch默认对外端口