Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch Delete Query - Filter with term and range

I have the following query that I am trying to use to delete data from an ElasticSearch index.

{
    "filter": {
        "and": [
            {
                "range": {
                    "Time": {
                        "from": "20120101T000000",
                        "to": "20120331T000000"
                    }
                }
            },
            {
                "term": {
                    "Source": 1
                }
            }
        ]
    }
}

I have tried to delete documents based on this query. This query returns results fine when I run it against the index. But when I try to run a delete command against the index, nothing happens.

I am not sure if I am constructiing the query wrong or what else.

like image 299
Gabbar Avatar asked Sep 13 '12 05:09

Gabbar


1 Answers

You're using only a filter while the delete by query API probably needs a query. You can convert your filter to a query using a filtered query like this:

{
    "filtered" : {
        "query" : {
            "match_all" : {}
        },
        "filter": {
            "and": [
                {
                    "range": {
                        "Time": {
                            "from": "20120101T000000",
                            "to": "20120331T000000"
                        }
                    }
                },
                {
                    "term": {
                        "Source": 1
                    }
                }
            ]
        }
    }
}

Otherwise you could convert your filter to a query using a bool query with two must clauses, so that you don't need a filtered query anymore. Anyway, I guess the filter approach is better since filters are faster.

like image 114
javanna Avatar answered Oct 21 '22 13:10

javanna