Find documents with empty string value on elasticsearch

前端 未结 12 2127
不知归路
不知归路 2020-12-03 09:46

I\'ve been trying to filter with elasticsearch only those documents that contains an empty string in its body. So far I\'m having no luck.

Before I go on, I should

12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 10:28

    I didn't manage to search for empty strings in a text field. However it seems to work with a field of type keyword. So I suggest the following:

        delete /test_idx
    
        put test_idx
        {
          "mappings" : {
            "testMapping": {
              "properties" : {
                "tag" : {"type":"text"},
                "content" : {"type":"text",
                             "fields" : {
                               "x" : {"type" : "keyword"}
                             }
                }
              }
            }
          }
        }
    
    put /test_idx/testMapping/1
    {
      "tag": "null"
    }
    
    put /test_idx/testMapping/2
    {
      "tag": "empty",
      "content": ""
    }
    
    GET /test_idx/testMapping/_search
    {
       "query" : {
         "match" : {"content.x" : ""}}}
                 }
    }
    

提交回复
热议问题