天天看點

ES 多個terms布爾查詢和range過濾的組合

GET index1/doc/_search
{
  "query": {
    "bool": {
      "must": {
        "terms": {
          "title": [
            "日本","核洩漏"
          ]
        }
      },
      "filter": {
        "range": {
          "publish_at": {
            "gte": "2021-04-13T00:00:00.000Z"
          }
        }
      }
    }
  },
  "sort": {
    "publish_at": {
      "order": "desc"
    }
  },
  "size": 10
}      
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "title": [
              "中國"
            ]
          }
        },
        {
          "terms": {
            "content": [
              "中國"
            ]
          }
        }
      ],
      "must_not": [
        {
          "terms": {
            "title": [
              "美國",
              "日本"
            ]
          }
        },
        {
          "terms": {
            "content": [
              "美國",
              "日本"
            ]
          }
        }
      ],
      "filter": {
        "range": {
          "publish_at": {
            "gte": "2021-01-01T00:00:00.000Z"
          }
        }
      },
      "minimum_should_match":1
    }
  },
  "sort": {
    "publish_at": {
      "order": "desc"
    }
  },
  "size": 10
}      

參考