文章目录
What is elasticsearch
you can use elasticsearch to store, search and analytics data
office document: https://www.elastic.co/guide/index.html
How to use
Index
document: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html
create index
document: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
Example:
-
Uri: localhost:9200/tx
-
Method: PUT
put mapping
document: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html
Example:
-
Uri: localhost:9200/tx/_mapping
-
Method: PUT
-
Body:
-
-
{ "properties":{ "time":{ "type":"date" }, "height":{ "type":"long" }, "tx_hash":{ "type":"keyword" }, "from":{ "type":"keyword" }, "to":{ "type":"keyword" }, "amount":{ "type":"nested", "properties":{ "denom":{ "type":"keyword" }, "amount":{ "type":"double" } } }, "type":{ "type":"keyword" }, "fee":{ "properties":{ "gas":{ "type":"long" }, "amount":{ "type":"nested", "properties":{ "denom":{ "type":"keyword" }, "amount":{ "type":"double" } } } } }, "memo":{ "type":"text", "fields":{ "raw":{ "type":"keyword" } } }, "status":{ "type":"byte" }, "code":{ "type":"short" }, "log":{ "type":"keyword", "index":false }, "gas_used":{ "type":"long" }, "gas_wanted":{ "type":"long" }, "gas_price":{ "type":"long" }, "actual_fee":{ "properties":{ "denom":{ "type":"keyword" }, "amount":{ "type":"double" } } }, "proposal_id":{ "type":"integer" }, "signers":{ "type":"nested", "properties":{ "addr_hex":{ "type":"keyword" }, "addr_bech32":{ "type":"keyword" } } }, "msgs":{ "type":"nested", "properties":{ "type":{ "type":"keyword" }, "msg":{ "properties":{ "amount":{ "type":"nested", "properties":{ "amount":{ "type": "double" } } }, "inputs":{ "type":"nested", "properties":{ "coins":{ "type":"nested", "properties":{ "amount":{ "type":"double" } } } } }, "outputs":{ "type":"nested", "properties":{ "coins":{ "type":"nested", "properties":{ "amount":{ "type":"double" } } } } }, "coins":{ "type":"nested", "properties":{ "amount":{ "type":"double" } } }, "delegation":{ "properties":{ "amount":{ "type":"double" } } }, "initial_deposit":{ "type":"nested", "properties":{ "coins":{ "type":"nested", "properties":{ "amount":{ "type":"double" } } } } } } } } } } }
-
Note:
- 同一字段只能由一种确定的类型。例如上述
msg
结构中amount
字段在 db 中存在number
和array
两种类型,当将数据加载到 es 中时,将number
类型的amount
重命名为了mint_token_amount
- 采用动态映射时,es 中整形的默认类型为
long
,如需更改默认类型,需要显示的定义其类型。例如上述xxx.*.amount
需要定义为double
QSL
document: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
Elasticsearch provides a full Query DSL (Domain Specific Language) based on JSON to define queries. Think of the Query DSL as an AST (Abstract Syntax Tree) of queries, consisting of two types of clauses:
Leaf query clauses
Leaf query clauses look for a particular value in a particular field, such as the
match
,term
orrange
queries. These queries can be used by themselves.Compound query clauses
Compound query clauses wrap other leaf or compound queries and are used to combine multiple queries in a logical fashion (such as the
bool
ordis_max
query), or to alter their behaviour (such as theconstant_score
query).
Compound
document: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
select tx_hash, height, code, type, msgs from xx_table where type="transfer" and (from="iaa1" or "to"="iaa2" or "signer"="iaa3") order by height desc skip 0 limit 10;
Example:
-
Uri: localhost:9200/tx/_search
-
Method: GET
-
Body
-
-
{ "from": 0, "size": 10, "_source": { "includes": ["tx_hash", "height", "code", "type", "msgs"] }, "query": { "bool": { "filter": [ { "term": { "type": "Transfer" } } ], "should": [ { "term": { "from": "faa176dd0tgn38grpc8hpxfmwl6sl8jfmknesgawx7" } }, { "term": { "to": "faa176dd0tgn38grpc8hpxfmwl6sl8jfmknesgawx7" } }, { "term": { "signer": "faa176dd0tgn38grpc8hpxfmwl6sl8jfmknesgawx7" } } ], "minimum_should_match": 1 } }, "sort": [ { "height": { "order": "desc" } } ] }
-
以上查询包含: 分页, 限定返回字段, 排序
Boolean 包含以下几种语法:
- must: The clause (query) must appear in matching documents and will contribute to the score.
- filter: The clause (query) must appear in matching documents. However unlike
must
the score of the query will be ignored. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.- must_not: The clause (query) must not appear in the matching documents. Clauses are executed in filter context meaning that scoring is ignored and clauses are considered for caching. Because scoring is ignored, a score of
0
for all documents is returned.- should: The clause (query) should appear in the matching document. 多个 should 之间是 “或” 的关系,使用
minimum_should_match
可以达到类似or
查询效果
Leaf query clauses
Term
document: https://www.elastic.co/guide/en/elasticsearch/reference/current/term-level-queries.html
Full text queries
document: https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html
Match all
document: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html
Nested
document: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html
select tx_hash, height, code, type, msgs from xx_table where msgs.type in ("Delegate", "BeginUnbonding", "BeginRedelegate") and (from="iaa1" or "to"="iaa1" or "signer"="iaa1") order by height desc skip 0 limit 10;
其中 msgs
和 signers
均为 nested
(数组对象) 结构,详见:
index mapping
{
"msgs":{
"type":"nested",
"properties":{
"msg":{
"properties":{
// ...
}
}
}
},
"signers":{
"type":"nested",
"properties":{
"addr_bech32":{
"type":"keyword"
},
"addr_hex":{
"type":"keyword"
}
}
}
}
Example:
{
"from":0,
"size":10,
"_source":{
"includes":[
"tx_hash",
"height",
"code",
"type",
"msgs"
]
},
"query":{
"bool":{
"must":[
{
"nested":{
"path":"msgs",
"query":{
"terms":{
"msgs.type":[
"Delegate","BeginUnbonding","BeginRedelegate"
]
}
},
"inner_hits":{
}
}
}
],
"should":[
{
"nested":{
"path":"signers",
"query":{
"term":{
"signers.addr_bech32":"faa12t4gfg502wra9lhtjjvqudq82rrzu2sk5j2l09"
}
}
}
},
{
"term":{
"from":"faa12t4gfg502wra9lhtjjvqudq82rrzu2sk5j2l09"
}
},
{
"term":{
"to":"faa12t4gfg502wra9lhtjjvqudq82rrzu2sk5j2l09"
}
}
],
"minimum_should_match":1
}
},
"sort":[
{
"height":{
"order":"desc"
}
}
]
}
Aggregation
Metrics Aggregation
Docs: aggregations-metrics
Single value metric aggregation
- avg: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html
- min: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-min-aggregation.html
- max: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html
- sum: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html
Multiple value metric aggregation
- stats: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-stats-aggregation.html
- percentiles: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html