ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 概述 **search_type 表示搜索类型** - matchall 返回索引的所有文档。不受 `query`的限制 - match 匹配查询可以选择执行模糊匹配。如果模糊度参数被设置为非零整数,则所分析的文本将与指定的模糊度相匹配。 此外,prefix_length参数可用于要求字词也具有指定长度的相同前缀。 - matchphrase 匹配短语查询类似于短语查询,但它会分析输入文本,并使用分析结果生成短语查询。 - term 对单词的精度匹配 - querystring 查询语言查询允许人类使用简单的语法来描述复杂的查询。 - prefix - wildcard - fuzzy 模糊匹配 - daterange **_source 返回字段** 如果为空则返回全部,填相关字符串组则表示返回指定字段` "Field1", "Field2" ` **query 表示查询条件** - term - field - start_time 开始时间 - end_time 结束时间 ## 基础搜索 格式 ``` POST /api/:target/_search ``` 示例 ``` POST http://localhost:4080/api/hello2/_search ``` 请求体 ``` { "search_type": "match", "query": { "term": "Prabhat", "field": "_all", "start_time": "2021-12-25T15:08:48.777Z" }, "sort_fields": ["-@timestamp"], "from": 0, "max_results": 20, "_source": [ ] } ``` _source ## 聚合查询 ``` POST http://localhost:4080/api/olympics/_search ``` 请求体 ``` { "search_type": "match", "query": { "term": "Ice Hockey" }, "sort_fields": ["-@timestamp"], "from": 0, "max_results": 20, "aggs": { "Medal": { "agg_type": "term", "field": "Medal", "size": 10 }, "Year": { "agg_type": "range", "field": "Year", "size": 10, "ranges": [ {"from": 1900, "to": 1920}, {"from": 1921, "to": 1950}, {"from": 1951, "to": 2000}, {"from": 2000, "to": 2021} ] }, "@timestamp": { "agg_type": "date_range", "field": "@timestamp", "size": 10, "date_ranges": [ { "from": "2020-01-21T09:22:50.604Z", "to": "2021-01-21T09:22:50.604Z" }, { "from": "2021-01-22T09:22:50.604Z", "to": "2023-01-21T09:22:50.604Z" } ] }, "max_Year": { "agg_type": "max", "field": "Year" }, "min_Year": { "agg_type": "min", "field": "Year" }, "avg_Year": { "agg_type": "avg", "field": "Year" }, "weighted_avg_Year": { "agg_type": "weighted_avg", "field": "Year", "weight_field": "Year" }, "sum_Year": { "agg_type": "sum", "field": "Year" }, "count_Sport": { "agg_type": "count", "field": "Sport" } } } ``` 请求 返回 ``` { "took": 250, "timed_out": false, "hits": { "total": { "value": 3051 }, "max_score": 17.82873837347811, "hits": null }, "aggregations": { "@timestamp": { "buckets": [ { "key": "[2020-01-21T09:22:50Z,2021-01-21T09:22:50Z)", "doc_count": 0 }, { "key": "[2021-01-22T09:22:50Z,2023-01-21T09:22:50Z)", "doc_count": 6102 } ] }, "Medal": { "buckets": [ { "key": "gold", "doc_count": 1026 }, { "key": "silver", "doc_count": 1024 }, { "key": "bronze", "doc_count": 1001 } ] }, "Year": { "buckets": [ { "key": "[1900.000000,1920.000000)", "doc_count": 132 }, { "key": "[1921.000000,1950.000000)", "doc_count": 2292 }, { "key": "[1951.000000,2000.000000)", "doc_count": 9822 }, { "key": "[2000.000000,2021.000000)", "doc_count": 5646 } ] }, "avg_Year": { "value": 1980.4818092428711 }, "count_Sport": { "value": 3051 }, "max_Year": { "value": 2014 }, "min_Year": { "value": 1908 }, "sum_Year": { "value": 36254700 }, "weighted_avg_Year": { "value": 1980.4818092428711 } }, "error": "" } ```