# Find documents which is older then specified date range based on list of ids

**URL:** https://discuss.elastic.co/t/find-documents-which-is-older-then-specified-date-range-based-on-list-of-ids/255843
**Category:** Elasticsearch
**Created:** [November 18, 2020, 12:52pm UTC](https://discuss.elastic.co/t/find-documents-which-is-older-then-specified-date-range-based-on-list-of-ids/255843 "2020-11-18T12:52:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sibasish.palo](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@sibasish.palo](https://discuss.elastic.co/u/sibasish.palo)
#### Post date: [November 18, 2020, 12:52pm UTC](https://discuss.elastic.co/t/find-documents-which-is-older-then-specified-date-range-based-on-list-of-ids/255843/1 "2020-11-18T12:52:41Z")

</div>

After searching for sometime and seeing some answer not able to quite figure out the query for my requirement

My requirement is i have a list of document ids, what i need to find is the documents which are older than a specified range.

Scenario what i am trying:  
total document present 10 documents with id ranging from 1 to 10.  
trying to fetch 1,2,3 document if its 7 days older.  
if only document 1,2 is 7 days older than it should only return 1 and 2 document and ignore the document 3 (if other documents are there which are 7 days older apart from document with id 1,2,3 it should not return in the result as i am passing the ids in the query).

Documents in index

```auto
{
  "took": 391,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 4,
      "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
      {
        "_index": "user",
        "_type": "test",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "title": "string1",
          "publishedDate": "2020-11-13T19:11:13.654Z"
        }
      },
      {
        "_index": "user",
        "_type": "test",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "title": "string2",
          "publishedDate": "2020-08-13T19:11:13.654Z"
        }
      },
      {
        "_index": "user",
        "_type": "test",
        "_id": "3",
        "_score": 1.0,
        "_source": {
          "title": "string3",
          "publishedDate": "2020-11-09T19:11:13.654Z"
        }
      },
      {
        "_index": "user",
        "_type": "test",
        "_id": "4",
        "_score": 1.0,
        "_source": {
          "title": "string4",
          "publishedDate": "2020-11-02T19:11:13.654Z"
        }
      }
    ]
  }
}

```

Below is the query i am trying:

```auto
{
      "query": {
      "bool" : {
        "must" : [
          {"term" : {"_id" : {"value" : "1"}}},
          {"term" : {"_id" : {"value" : "2"}}},
          {"term" : {"_id" : {"value" : "3"}}}
        ],
        "filter" : [
          {"range" : {"publishedDate" : {"from" : "now-7d","to" : "now",
                      "include_lower" : true,"include_upper" : true,"boost" : 1.0
                     }
            }
          }
        ],
        "adjust_pure_negative" : true,
        "boost" : 1.0
      }
    }
    }

```

Ideally it should return document 1 and 2 as only those two documents match with the query but above query doesn't return any result.  
i think i am doing something wrong in the query.  
can someone please help me with this.

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [November 20, 2020, 8:01am UTC](https://discuss.elastic.co/t/find-documents-which-is-older-then-specified-date-range-based-on-list-of-ids/255843/2 "2020-11-20T08:01:11Z")

</div>

> [@sibasish.palo](#):
>
> ```auto
> "must" : [
> {"term" : {"_id" : {"value" : "1"}}},
> {"term" : {"_id" : {"value" : "2"}}},
> {"term" : {"_id" : {"value" : "3"}}}
> ],
> 
> ```

This will never be true as each document can only have a single id. Try to use an [ids query clause](https://www.elastic.co/guide/en/elasticsearch/reference/7.9/query-dsl-ids-query.html) instead.

---

<div class="post-metadata">

### Author: ![sibasish.palo](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@sibasish.palo](https://discuss.elastic.co/u/sibasish.palo)
#### Post date: [November 20, 2020, 8:23am UTC](https://discuss.elastic.co/t/find-documents-which-is-older-then-specified-date-range-based-on-list-of-ids/255843/3 "2020-11-20T08:23:13Z")

</div>

Hi @Christian_Dahlqvist  
Thank you for the response. i changed my query as below and it works as expected

```auto
{
  "query": {
    "bool": {
      "must": [{"terms": { "_id": [ "1", "2", "3"]}}],
      "filter": [
        {
          "range": {
            "publishedDate": {
              "from": "now-7d",
              "to": "now",
              "include_lower": true,
              "include_upper": true,
              "boost": 1.0
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [December 18, 2020, 8:23am UTC](https://discuss.elastic.co/t/find-documents-which-is-older-then-specified-date-range-based-on-list-of-ids/255843/4 "2020-12-18T08:23:27Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
