elasticsearch创建索引和查看索引及结构命令

命令

查看索引及结构:

命令: GET /索引名/_mapping?pretty

创建索引:

命令: PUT 索引名

示例:
number_of_shards:分片数量(按机器节点数量计算,创建后不能更改)
number_of_replicas:副本数量

PUT admin_log_0821
{
	  "settings":{
	    "number_of_shards":6,
      "number_of_replicas":	1
	  },
		"mappings": {
			"logs": {
				"properties": {
				"esCreateTime": {
					"type": "long"
				},
				"id": {
					"type": "long"
				},
				"ip": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"uri": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"userId": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"userName": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				}
			}
			}
		}
	}

基础索引示例:

{
  "settings": {
    "number_of_shards": 16,
    "number_of_replicas": 1
  },
  "mappings": {
    "risk_event_ads": {
      "dynamic": "strict",
      "properties": {
        "flowNo": {
          "type": "String",
          "index": "not_analyzed"
        },
        "elapseTime": {
          "type": "long"
        },
        "ipLon": {
          "type": "float"
        },
        "createTime": {
          "type": "date"
        },
        "occurTimeYear": {
          "type": "short"
        },
        "occurTimeMonth": {
          "type": "byte"
        },
        "cpuCurFreq": {
          "index": "not_analyzed",
          "type": "string"
        },
        "extraInfoObj": {
          "type": "object",
          "dynamic": true
        }
      }
    }
  }
}

crul命令: curl -XPUT “http://es地址:9200/索引名” -H ‘Content-Type: application/json’ -d’索引json’

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ElasticSearch是一种分布式的搜索和分析引擎,它可以帮助用户快速地搜索和分析大量的数据。在使用ElasticSearch之前,需要先创建一个索引库,用于存储数据。 以下是创建索引库的步骤: 1. 安装ElasticSearch:首先需要在本地机器上安装ElasticSearch。可以从官方网站下载最新版本的ElasticSearch,并按照安装指南进行安装。 2. 启动ElasticSearch:安装完成后,需要启动ElasticSearch。可以通过命令行或控制台启动ElasticSearch。启动成功后,可以通过浏览器在http://localhost:9200访问ElasticSearch。 3. 创建索引库:使用ElasticSearch的REST API创建一个索引库。可以使用curl或任何其他HTTP客户端来发送REST请求。下面是一个使用curl创建索引库的示例: ``` curl -X PUT "http://localhost:9200/my_index" ``` 这个命令会在ElasticSearch创建一个名为“my_index”的索引库。 4. 添加映射:在索引库中添加映射,以定义数据的结构。映射定义了数据类型、字段名称和字段属性。下面是一个使用curl添加映射的示例: ``` curl -X PUT "http://localhost:9200/my_index/_mapping/my_type" -H 'Content-Type: application/json' -d' { "properties": { "name": { "type": "text" }, "age": { "type": "integer" }, "email": { "type": "keyword" } } } ' ``` 这个命令会在“my_index”索引库中的“my_type”类型下添加一个映射。 5. 添加文档:使用ElasticSearch的REST API向索引库中添加文档。可以使用curl或任何其他HTTP客户端来发送REST请求。下面是一个使用curl添加文档的示例: ``` curl -X POST "http://localhost:9200/my_index/my_type" -H 'Content-Type: application/json' -d' { "name": "张三", "age": 30, "email": "zhangsan@example.com" } ' ``` 这个命令会向“my_index”索引库中的“my_type”类型添加一个名为“张三”的文档。 6. 搜索数据:使用ElasticSearch的REST API搜索索引库中的数据。可以使用curl或任何其他HTTP客户端来发送REST请求。下面是一个使用curl搜索数据的示例: ``` curl -X GET "http://localhost:9200/my_index/my_type/_search?q=name:张三" ``` 这个命令会从“my_index”索引库中的“my_type”类型中搜索名为“张三”的文档。 以上是创建索引库的基本步骤。在实际应用中,可能需要更复杂的操作,例如创建分片、备份和恢复等。ElasticSearch提供了强大的API来支持这些操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值