ES查询示例
1、查询值为空:
GET /indexName/_search
{"query":
{
"bool": {
"should": [
{
"term": {
"pick_fbq_code": ""
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "pick_fbq_code"
}
}
]
}
}
]
}
}
}
GET /indexName/_search
{"query":
{
"bool": {
"filter": {
"prefix": {
"pick_tm": "2019-09-29"
}
}
}
}
}
GET /indexName/_search
{"query":
{
"bool": {
"filter": {
"prefix": {
"pick_tm": "2019-09-29"
}
},
"must_not": [
{
"exists": {
"field": "pick_fbq_code"
}
}
]
}
}
}
GET /indexName/_search
{"query":
{
"bool": {
"filter": {
"term": {
"pick_fbq_code": ""
}
} ,"must": [
{
"prefix": {
"pick_tm": "2019-09-29"
}
}
]
}
}
}
2、多条件查询Must
GET /risp_abc/_search
{"query":
{
"bool": {
"must": [
{ "match": { "meterageWeightQty": ""}},
{ "match": { "consignedTime": "2019-09-30"}}
]
}
}
}
3、_delete_by_query条件删除
POST /indexName/typeName/_delete_by_query?conflicts=proceed&scroll_size=10000
{
"query": {
"range": {
"receive_date": {
"gte": "2019-10-01",
"lte": "2019-10-01"
}
}
}
}
4、查看模板
GET /*/_alias/indexName
GET /indexName/_alias/*
POST /_aliases
{
"actions": [
{
"add": {
"index": "indexName~20191029",
"alias": "indexName"
}
}
]
}
5、新增字段
PUT my_index/_mapping/my_type
{
"properties": {
"new_column": {
"type": "integer"
}
}
}
6、
GET /indexName/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{"prefix": {
"pickupDepotBatchDay": {
"value": "laterIgnore_"
}
}}
]
}},
"aggregations" : {
"pickupDay" : {
"terms" : {
"field" : "searchDay",
"size" : 100000
}}}
}
7、查询属性值长度不超过4的记录
GET /indexName/_search
{
"query": {
"bool": {
"must": {
"script": {
"script": {
"inline": "doc['deliveryDeptCode'].length<4",
"lang": "painless"
}
}
}
}
}
}
8、shell脚本别名及异常捕捉
#!/bin/sh
es_host="http://10.110.106.47:9200/"
dt=$1
dt1=$2
dt2=$3
index="oewm_receive_dept_stats_day"
echo "$dt, $dt1, $dt2"
ret=0
result=1
errorStr='error":'
echo "errorStr= $errorStr"
result1=$(curl -XPOST "$es_host"_aliases -H 'Content-Type: application/json' -d'
{
"actions": [
{
"add": {
"index": "'${index}'~'${dt}'",
"alias": "'${index}'"
}
}
]
}
')
if [[ $result1 =~ $errorStr ]]
then
ret=1
echo "result1= $result1"
fi
result2=$(curl -XPOST "$es_host"_aliases -H 'Content-Type: application/json' -d'
{
"actions": [
{
"remove": {
"index": "'${index}'~'${dt1}'",
"alias": "'${index}'"
}
}
]
}
')
if [[ $result2 =~ $errorStr ]]
then
ret=1
echo "result2= $result2"
fi
result3=$(curl -XDELETE ${es_host}${index}~$dt2 && curl -XGET "$es_host"_alias/${index})
if [[ $result3 =~ $errorStr ]]
then
ret=1
echo "result3= $result3"
fi
echo "ret= $ret"
exit $ret
9、模糊查询排名
GET /indexName/_search
{
"size": 0,
"query": {
"bool": {
"filter": [
{
"prefix": {
"consignment": "托寄物"
}
},
{
"prefix": {
"indexDay": "2020-09"
}
}
]
}
} ,
"aggregations" : {
"deliveryCityCode" : {
"terms" : {
"field" : "deliveryCityCode",
"size" : 100000
}}}
}
10、模糊搜索
GET /indexName/_search
{
"query": {
"wildcard": {
"consignment": {
"value": "*华为手机*"
}}
}}
11、模糊搜索+多条件查询
GET indexName/_search
{
"query":{
"bool": {
"filter": [
{
"term": {
"processDate": {
"value": "2020-12-16"
}
}
}, { "query_string": {
"default_field": "requireId",
"query" : "*20121617568893*"
}}
]
}
}
}