1. elasticsearch 学习笔记-基础知识-1

本文详细介绍Elasticsearch的使用方法,包括索引创建、映射设置、查询语法及聚合分析等功能,通过具体示例帮助读者掌握数据存储、搜索与分析技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 中存在 numberarray 两种类型,当将数据加载到 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 or range 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 or dis_max query), or to alter their behaviour (such as the constant_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;

其中 msgssigners 均为 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值