根据ID列表查找比指定日期范围更早的文档

uqzxnwby  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(422)

在寻找了一段时间,看到一些答案,不能很好地理解我的要求查询
我的要求是我有一个文件ID列表,我需要找到的是超过指定范围的文件。
我正在尝试的场景:total document提供10个id从1到10的文档。尝试获取1,2,3文档(如果它早于7天)。如果只有文档1,2比它早7天,那么它应该只返回1和2文档,而忽略文档3(如果除了id为1,2,3的文档之外还有其他文档早7天,那么当我在查询中传递id时,它不应该返回结果)。
索引中的文档

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

下面是我正在尝试的问题:

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

理想情况下,它应该返回文档1和2,因为只有这两个文档与查询匹配,但上面的查询不返回任何结果。我想我在查询中做错了什么。有人能帮我一下吗。
提前谢谢

vlju58qv

vlju58qv1#

如果要检索比当前日期最多早7天的文档,则应仅返回文档 1 ,作为文档 2 已超过7天。
添加带有搜索查询和搜索结果的工作示例
搜索查询:

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

搜索结果:

"hits": [
      {
        "_index": "64906019",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "title": "string1",
          "publishedDate": "2020-11-13T19:11:13.654Z"
        }
      }
    ]

更新1:
如果只替换 must 带有 should 条款

{
  "query": {
    "bool": {
      "should": [                 <-- note this
        {
          "term": {
            "_id": "1"
          }
        },
        {
          "term": {
            "_id": "2"
          }
        },
        {
          "term": {
            "_id": "3"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "publishedDate": {
              "from": "now-7d",
              "to": "now",
              "include_lower": true,
              "include_upper": true,
              "boost": 1.0
            }
          }
        }
      ]
    }
  }
}

相关问题