启动es
docker run --name es6 --net host -e "discovery.type=single-node" docker.io/elasticsearch:6.5.4
es操作:
查询数据
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,这意味着该字段被视为丢失。