Create Elasticsearch curl query for not null and not empty(“”)

后端 未结 12 979
陌清茗
陌清茗 2020-12-13 01:21

How can i create Elasticsearch curl query to get the field value which are not null and not empty(\"\"),

Here is the mysql query:

select field1 from          


        
相关标签:
12条回答
  • 2020-12-13 02:14

    Wrap a Missing Filter in the Must-Not section of a Bool Filter. It will only return documents where the field exists, and if you set the "null_value" property to true, values that are explicitly not null.

    {
      "query":{
         "filtered":{
            "query":{
               "match_all":{}
            },
            "filter":{
                "bool":{
                  "must":{},
                  "should":{},
                  "must_not":{
                     "missing":{
                        "field":"field1",
                        "existence":true,
                        "null_value":true
                     }
                  }
               }
            }
         }
      }
    }
    
    0 讨论(0)
  • 2020-12-13 02:14

    Here's the query example to check the existence of multiple fields:

    {
      "query": {
        "bool": {
          "filter": [
            {
              "exists": {
                "field": "field_1"
              }
            },
            {
              "exists": {
                "field": "field_2"
              }
            },
            {
              "exists": {
                "field": "field_n"
              }
            }
          ]
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-13 02:17

    You can do that with bool query and combination of must and must_not like this:

    GET index/_search
    {
        "query": {
            "bool": {
                "must": [
                    {"exists": {"field": "field1"}}
                ],
                "must_not": [
                    {"term": {"field1": ""}}
                ]
            }
        }
    }
    

    I tested this with Elasticsearch 5.6.5 in Kibana.

    0 讨论(0)
  • 2020-12-13 02:17

    You need to use bool query with must/must_not and exists

    To get where place is null

    {
      "query": {
        "bool": {
          "must_not": {
            "exists": {
              "field": "place"
            }
          }
        }
      }
    }
    

    To get where place is not null

    {
      "query": {
        "bool": {
          "must": {
            "exists": {
              "field": "place"
            }
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-13 02:21

    On elasticsearch 5.6, I have to use command below to filter out empty string:

        GET /_search
        {
            "query" : {
                "regexp":{
                    "<your_field_name_here>": ".+"
                }
            }
        }  
    
    0 讨论(0)
  • 2020-12-13 02:21

    You can use a bool combination query with must/must_not which gives great performance and returns all records where the field is not null and not empty.

    bool must_not is like "NOT AND" which means field!="", bool must exist means its !=null.

    so effectively enabling: where field1!=null and field1!=""

    GET  IndexName/IndexType/_search
    {
        "query": {
            "bool": {
                "must": [{
                    "bool": {
                        "must_not": [{
                            "term": { "YourFieldName": ""}
                        }]
                    }
                }, {
                    "bool": {
                        "must": [{
                          "exists" : { "field" : "YourFieldName" }
                        }]
                    }
                }]
            }   
        }
    }
    

    ElasticSearch Version:

      "version": {
        "number": "5.6.10",
        "lucene_version": "6.6.1"
      }
    
    0 讨论(0)
提交回复
热议问题