查询所有

{
  "query": { "match_all": {} },
  "sort": [
    { "字段名": "asc" }
  ]
}

分页查询(from+size)

{
  "query": { "match_all": {} },
  "sort": [
    { "字段名": "asc" }
  ],
  "from": 10,
  "size": 10
}

指定字段查询:match

{
  "query": { "match": { "字段名": "值" } }
}

查询段落匹配:match_phrase模糊匹配类似 like "%值%"

{
  "query": { "match_phrase": { "字段名": "值" } }
}

多条件查询: bool

如果要构造更复杂的查询,可以使用bool查询来组合多个查询条件。

例如,以下请求搜索满足字段1的值但不满足字段2

{
  "query": {
    "bool": {
      "must": [
        { "match": { "字段名1": "值" } }
      ],
      "must_not": [
        { "match": { "字段名2": "值" } }
      ]
    }
  }
}

 在bool查询的子句中同时具备query/must 和 filter

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "字段1": "值"
          }
        }
      ],
      "filter": [
        {
          "term": {
            "字段2": "值"
          }
        },
        {
          "range": {
            "字段3": {
              "gte": 大于值,
              "lte": 小于值
            }
          }
        }
      ]
    }
  }
}

must+should+filter+sort

[
        {
            "from": 0,
            "size": 10,
            "query": {
                "bool": {
                    "must": [
                        {
                            "regexp": {
                                "title": ".*中国.*"
                            }
                        }
                    ],
                    "should": [
                        {
                            "match_phrase": {
                                "category_id": ",1,"
                            }
                        },
                        {
                            "match_phrase": {
                                "category_id": ",2,"
                            }
                        }
                    ],
                    "minimum_should_match": 1,
                    "filter": [
                        {
                            "terms": {
                                "table_name": [
                                    "ZBXX"
                                ]
                            }
                        },
                        {
                            "terms": {
                                "area_id": [
                                    "1",
                                    "2"
                                ]
                            }
                        },
                        {
                            "range": {
                                "publish_date": {
                                    "gt": "2023-09-12 00:00:00",
                                    "lt": "2023-09-19 00:00:00",
                                    "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                                }
                            }
                        }
                    ]
                }
            },
            "sort": [
                {
                    "publish_date": {
                        "order": "desc"
                    }
                }
            ]
        }
    ]