zoukankan      html  css  js  c++  java
  • Elasticsearch 字段为空(null)记录查询

    在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 是同一个概念;
     
  • 相关阅读:
    IOS-多线程知识
    《shop》 --- 自定义工具类 分页功能
    navicat for mysql 显示中文乱码解决办法
    Linux -- 搭建php服务器环境小记
    在自己主机搭建svn服务器,在远程地址里搭建svn服务器
    $ThinkPhp学习,shop项目 小记
    php 个人博客 实战小记
    php:require 和 include 的区别 5.9日
    mysql的入门命令 ==留言本的思路==
    Access denied for user 'root'@'localhost' (using password: YES) 问题解决小记
  • 原文地址:https://www.cnblogs.com/illusioned/p/11915080.html
Copyright © 2011-2022 走看看