搜索数据语法示例 范围查询 (Range Query) 范围查询允许你搜索数值、日期或其他可比较字段的范围内的值。 GET /myindex/search { "query": { "range": { "price": { "gte": 100, "lte": 200 } } } } 这个查询会返回 price 字段值在 100 到 200 之间的文档。 多字段查询 多字段匹配 (MultiMatch Query) 多字段匹配查询用于在多个字段中搜索相同的关键词。 GET /myindex/search { "query": { "multimatch": { "query": "OpenSearch", "fields": ["title", "content"] } } } 这个查询会在 title 和 content 字段中搜索 "OpenSearch"。 字段存在查询 (Exists Query) 字段存在查询用于查找某个字段存在的文档。 GET /myindex/search { "query": { "exists": { "field": "user" } } } 这个查询会返回所有 user 字段存在的文档。 排序与分页 排序 (Sort) 你可以根据一个或多个字段对搜索结果进行排序。 GET /myindex/search { "sort": [ { "date": { "order": "desc" } }, { "price": { "order": "asc" } } ], "query": { "matchall": {} } } 此查询会先按 date 字段降序排序,再按 price 字段升序排序。 分页 (Pagination) 分页可以通过 from 和 size 参数来实现。 GET /myindex/search { "from": 10, "size": 5, "query": { "matchall": {} } } 这个查询会跳过前 10 条记录,并返回接下来的 5 条记录。 高级搜索 嵌套查询 (Nested Query) 如果文档中包含嵌套对象或数组,nested 查询可以用于对嵌套字段进行搜索。 GET /myindex/search { "query": { "nested": { "path": "comments", "query": { "bool": { "must": [ { "match": { "comments.author": "John" } }, { "range": { "comments.date": { "gte": "20240101" } } } ] } } } } } 此查询会返回 comments 数组中包含 author 为 John 且 date 在 "20240101" 之后的文档。