在Elasticsearch 的使用和不斷深入中,我們常常會遇到各種各樣的問題;
在此,記錄下Elasticsearch 字段為空記錄查詢;
1.查詢為空的字段
我們查詢某個字段為空的數據時,在mysql中:
select eid,ent_name from ent_search where enttype_code is NULL;
在elasticsearch中,我們使用的api為exists,這個查詢是:查詢這個字段為空的或者沒有這個字段的:
GET ent_search/_search
{
"_source": ["eid","ent_name"],
"query": {
"bool": {
"must_not": {
"exists": {
"field": "enttype_code"
}
}
}
}}
2.查詢某個不為空的字段
我們查詢某個字段不為空的數據時,在mysql中:
select eid,ent_name from ent_search where enttype_code is NOT NULL;
在elasticsearch中,我們使用的api為exists,這個查詢是:
GET ent_search/_search
{
"_source": ["eid","ent_name","enttype_code"],
"query": {
"constant_score": {
"filter": {
"exists": {
"field": "enttype_code"
}
}
}
}}
### 查詢例子
sql_example: select msg,level from jsonlog3-2019.06.26 where msg is NULL; es_explain: GET /jsonlog3-2019.06.26/_search { "_source": ["msg","level"], "query": { "bool": { "must": [ {"bool": {"must_not": [ {"exists": {"field": "msg"}} ]}} ] } }} sql_example: select msg,level from jsonlog3-2019.06.26 where msg is not NULL; es_explain: GET /jsonlog3-2019.06.26/_search { "_source": ["msg","level"], "query": { "bool": { "must": [ {"bool": {"must": [ {"exists": {"field": "msg"}} ]}} ] } }} OR GET /qdtech-jsonlog3-2019.06.26/_search { "_source": ["msg","level"], "query": { "constant_score": { "filter": { "exists": { "field": "msg" } } } } } 注:es中字段不存在 和 字段為null 是同一個概念;
