Home > Software design >  Find all docs where field of type double is equal to empty string
Find all docs where field of type double is equal to empty string

Time:09-30

Problem

I'm trying to find all documents that have a particular field set to the empty string.

But this field is of type double and that complicates things...

I can't use term or match query because the field is of type double. I get a number_format_exception.

I'm currently using the following:

{
    "query": {
        "bool": {
            "must_not": [
                {
                   "exists": {
                        "field": "my_double_field"
                    }
                }
            ]
            
        }
    },
    "size": 100
}

This returns all docs where my_double_field is equal to "" AND the docs where my_double_field does not exist at all.

Question

Is there a query to return docs if and only if this field is set to ""?

CodePudding user response:

If the goal is to remove the field, then you can simply hit the _update_by_query endpoint and iterate over all your documents and remove the field if it matches an empty string. There's no query that will allow you to only select those documents, but at least you can iterate over all documents and only update the ones that contain those values.

POST test/_update_by_query
{
  "script": {
    "source": """
    if (ctx._source.my_double_field == '') {
      ctx._source.remove('my_double_field');
    }
    """,
    "lang": "painless"
  }
}
  • Related