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

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

{
  "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:

{
      "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.

This will never be true as each document can only have a single id. Try to use an ids query clause instead.

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

{
  "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
            }
          }
        }
      ]
    }
  }
}

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