Find documents with empty string value on elasticsearch

前端 未结 12 2121
不知归路
不知归路 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:24

    For those of you using elastic search 5.2 or above, and still stuck. Easiest way is to reindex your data correctly with the keyword type. Then all the searches for empty values worked. Like this:

    "query": {
        "term": {"MY_FIELD_TO_SEARCH": ""}
    }
    

    Actually, when I reindex my database and rerun the query. It worked =)

    The problem was that my field was type: text and NOT a keyword. Changed the index to keyword and reindexed:

    curl -X PUT https://username:password@host.io:9200/mycoolindex
    
    curl -X PUT https://user:pass@host.io:9200/mycoolindex/_mapping/mycooltype -d '{
      "properties": {
                "MY_FIELD_TO_SEARCH": {
                        "type": "keyword"
                    },
    }'
    
    curl -X PUT https://username:password@host.io:9200/_reindex -d '{
     "source": {
       "index": "oldindex"
     },
     "dest": {
        "index": "mycoolindex"
     }
    }'
    

    I hope this helps someone who was as stuck as I was finding those empty values.

提交回复
热议问题