es http Restful操作笔记

启动es

docker run --name es6 --net host -e "discovery.type=single-node" docker.io/elasticsearch:6.5.4

es操作:

参考:
ElasticSearch restful实操

查询数据

curl -X GET "http://x.x.x.x:9200/_cat"
curl -X GET http://x.x.x.x:9200/_cat/indices?v
curl -X GET "http://x.x.x.x:9200/index01/_search?pretty"

操作es数据demo

创建索引:

curl -X PUT http://x.x.x.x:9200/index01     同: put /index01

删除索引:

curl -XDELETE http://x.x.x.x:9200/index01

批量删除索引:

curl -XDELETE http://x.x.x.x:9200/index_* #删除索引 index_01/index_02

插入数据:

curl -XPOST http://x.x.x.x:9200/index01/type01 -H "Content-Type: application/json" \
-d '{"name" : "john","age" : 18,"id" : 2}'
同: POST /index01/type01
{"name" : "john"} 

查询数据:

curl -XGET http://x.x.x.x:9200/index01/type01/_search?pretty
curl -XGET http://x.x.x.x:9200/index01/_search?pretty \
-d '{
    "query": {
        "match_all": {}
        }
    }'

更新部分数据:

curl -XPOST http://x.x.x.x:9200/index01/type01/1/_update?pretty -H "Content-Type: application/json" \
-d '{"doc":{"name": "zhangsan"}}'

同: GET /index01/type01/_search?pretty
    {
    "query": {
        "match_all": {}
        }
    }

查询表达式:

示例1:
GET /index01/type01/_search?pretty
    {
    "query":{
        "bool": {
            "filter": {"range" : {"age": {"gt": 18}}}
            }
        }
    }
示例2:
    {
    "bool": {
        "match_all": {}
        }
    }

查询数据

curl -XGET http://x.x.x.x:9200/business_waybill/factoring_waybill/_search?pretty
匹配日期范围

curl -XGET http://x.x.x.x:9200/business_bill/factoring_bill/_search?pretty -H "Content-Type: application/json" \
-d '{
        "query": {
            "bool": {
                "must": [
                    {
                        "range": {
                            "settle_tm": {
                                "gte": "2022-01-01",
                                "lte": "2022-01-01"
                            }
                        }
                    }
                ]
            }
        }
    }'

匹配字段

  curl -XGET http://x.x.x.x:9200/business_bill/factoring_bill/_search?pretty -H "Content-Type: application/json" \
    -d '{
            "query": {
                "bool": {
                    "must": [
                        {
                        "match": {
                            "carrier_company_no":"123"
                        }
                        }
                    ]
                }
            }
        }'

匹配日期范围和字段(写法一)

curl -XGET http://x.x.x.x:9200/business_bill/factoring_bill/_search?pretty -H "Content-Type: application/json" \
-d '{
        "query": {
            "bool": {
                "must": [
                    {
                    "match": {
                        "carrier_company_no":"123"
                    }
                    },
                    {
                        "range": {
                            "settle_tm": {
                                "gte": "2022-01-01",
                                "lte": "2022-01-01"
                            }
                        }
                    }
                ]
            }
        }
        
    }'

匹配日期范围和字段(写法二)

curl -XGET http://x.x.x.x:9200/business_bill/factoring_bill/_search?pretty -H "Content-Type: application/json" \
-d '{
        "query": {
            "bool": {
                "must": [
                    {
                    "match": {
                        "carrier_company_no":"123"
                    }
                    }
                ],
                "filter":{
                        "range": {
                            "settle_tm": {
                                "gte": "2022-01-01",
                                "lte": "2022-01-01"
                            }
                        }
                }
            }
        }
    }'

mapping操作

查看mapping

  curl -XGET http://x.x.x.x:9200/business_bill?pretty

创建mapping,添加mapping字段和新加mapping一样的操作

curl -XPOST 'http://x.x.x.x:9200/business_bill/factoring_bill/_mapping?pretty' -H "Content-Type: application/json"  \
-d '
{
	"factoring_bill": {
		"properties": {
			"settle_tm": {
				"type": "date",
				"format": "yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
			},
			"publish_time": {
				"type": "keyword",
				"fields": {
					"text": {
						"type": "keyword"
					},
					"date": {
						"type": "date",
						"format": "yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
						"ignore_malformed": "true"
					}
				}
			},
			"settle_no": {
				"type": "text"
			},
			"amount_sum": {
				"type": "double"
			},
			"carrier_company_no": {
				"type": "text"
			},
			"carrier_company_name": {
				"type": "keyword",
				"ignore_above": 256
			},
			"settle_status": {
				"type": "integer"
			},
			"waybill_no": {
				"type": "text"
			},
			"business_dt": {
				"type": "text"
			},
			"trans_job_item_code": {
				"type": "text"
			},
			"charging_tm": {
				"type": "text"
			},
			"volume": {
				"type": "double"
			},
			"weight": {
				"type": "double"
			},
			"count": {
				"type": "double"
			},
			"amount": {
				"type": "double"
			}
		}
	}
}
'

date类型解析说明:

  • format。通过format设置日期格式,常见的可以设置成年月日时分秒、年月日及毫秒值三种格式。

  • ignore_malformed。默认值false。如果为true,则忽略格式错误的数字。如果为false(默认值),则格式错误的数字将引发异常并拒绝整个文档。

  • null_value。默认是null。接受采用配置格式之一的日期值作为该字段,以替换任何显式的空值。默认为null,这意味着该字段被视为丢失。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值