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

后端 未结 12 978
陌清茗
陌清茗 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 01:55

    A null value and an empty string both result in no value being indexed, in which case you can use the exists filter

    curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '
    {
       "query" : {
          "constant_score" : {
             "filter" : {
                "exists" : {
                   "field" : "myfield"
                }
             }
          }
       }
    }
    '
    

    Or in combination with (eg) a full text search on the title field:

    curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '
    {
       "query" : {
          "filtered" : {
             "filter" : {
                "exists" : {
                   "field" : "myfield"
                }
             },
             "query" : {
                "match" : {
                   "title" : "search keywords"
                }
             }
          }
       }
    }
    '
    
    0 讨论(0)
  • 2020-12-13 01:56

    As @luqmaan pointed out in the comments, the documentation says that the filter exists doesn't filter out empty strings as they are considered non-null values.

    So adding to @DrTech's answer, to effectively filter null and empty string values out, you should use something like this:

    {
        "query" : {
            "constant_score" : {
                "filter" : {
                    "bool": {
                        "must": {"exists": {"field": "<your_field_name_here>"}},
                        "must_not": {"term": {"<your_field_name_here>": ""}}
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-13 01:57

    You can use not filter on top of missing.

    "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "not": {
               "filter": {
                  "missing": {
                     "field": "searchField"
                  }
               }
            }
         }
      }
    }
    
    0 讨论(0)
  • 2020-12-13 02:04

    We are using Elasticsearch version 1.6 and I used this query from a co-worker to cover not null and not empty for a field:

    {
      "query": {
        "filtered": {
          "query": {
            "match_all": {}
          },
          "filter": {
            "bool": {
              "must": [
                {
                  "exists": {
                    "field": "myfieldName"
                  }
                },
                {
                  "not": {
                    "filter": {
                      "term": {
                        "myfieldName": ""
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-13 02:07

    The only solution here that worked for me in 5.6.5 was bigstone1998's regex answer. I'd prefer not to use a regex search though for performance reasons. I believe the reason the other solutions don't work is because a standard field will be analyzed and as a result have no empty string token to negate against. The exists query won't help on it's own either since an empty string is considered non-null.

    If you can't change the index the regex approach may be your only option, but if you can change the index then adding a keyword subfield will solve the problem.

    In the mappings for the index:

    "myfield": {
        "type": "text",
        "fields": {
            "keyword": {
                "ignore_above": 256,
                "type": "keyword"
            }
        }
    }
    

    Then you can simply use the query:

    {
      "query": {
        "bool": {
          "must": {
            "exists": {
              "field": "myfield"
            }
          },
          "must_not": {
            "term": {
              "myfield.keyword": ""
            }
          }
        }
      }
    }
    

    Note the .keyword in the must_not component.

    0 讨论(0)
  • 2020-12-13 02:13
    Elastic search Get all record where condition not empty.
    
    const searchQuery = {
          body: {
            query: {
              query_string: {
                default_field: '*.*',
                query: 'feildName: ?*',
              },
            },
          },
          index: 'IndexName'
        };
    
    0 讨论(0)
提交回复
热议问题