Find empty strings in elasticsearch

前端 未结 6 1298
盖世英雄少女心
盖世英雄少女心 2021-01-18 14:58

I\'m trying to _search documents that has some specific value in the field.

{
  \"query\": {
      \"bool\": {
        \"must\": [
         {\"f         


        
相关标签:
6条回答
  • 2021-01-18 15:22

    Or another way to do the same thing more efficiently is to use the exists filter:

    "exists" : {
      "field" : "advs.status"
    }
    

    Both are valid, but this one is better :)

    0 讨论(0)
  • 2021-01-18 15:27

    You can try this temporary solution which works but isn't optimal - https://github.com/elastic/elasticsearch/issues/7515

    PUT t/t/1
    {
    "textContent": ""
    }
    
    PUT t/t/2
    {
     "textContent": "foo"
    }
    
    GET t/t/_search
    {
     "query": {
      "bool": {
       "must": [
        {
          "exists": {
            "field": "textContent"
          }
        }
      ],
      "must_not": [
        {
          "wildcard": {
            "textContent": "*"
          }
         }
       ]
      }
     }
    }
    
    0 讨论(0)
  • 2021-01-18 15:29

    If tou want to search for fields containing an empty string, either you change your mapping to set not_analyzed to this particular field or you can use a script filter:

    "filter": {
      "script": {
        "script": "_source.advs.status.length() == 0"
      }
    }
    
    0 讨论(0)
  • 2021-01-18 15:29

    the "missing" does work only for null values or not being there at all. Matching empty string was already answered here: https://stackoverflow.com/a/25562877/155708

    0 讨论(0)
  • 2021-01-18 15:32

    I generally use a filter if the field is not analyzed. Here is snippet:

    {
      "filtered": {
        "filter": {
          "term": {
            "field": ""
          }
        }
      }
    },
    
    0 讨论(0)
  • 2021-01-18 15:33

    Try using must_not with missing in your bool:

    "must_not":{
      "missing":{
        "field":"advs.status",
        "existence":true,
        "null_value":true
      }
    }
    
    0 讨论(0)
提交回复
热议问题