1、基本操作
查看索引的settings
GET /index_name/_settings
查看索引的mappings
GET /index_name/_mapping
新建索引mappings
PUT /index_name/_mapping { "properties": { "content": { "type": "text", "term_vector": "with_positions_offsets", "similarity": "BM25", "analyzer": "english", "search_analyzer": "standard", "fielddata": true }, "country": { "type": "keyword" }, "emotion": { "type": "byte" }, "time": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss" } } }
新索引settings和mappings
PUT /index_name { "settings":{ "number_of_shards":3, "number_of_replicas":2 }, "mappings":{ "properties":{ "id":{"type":"long"}, "name":{"type":"text","analyzer":"ik_smart"}, "text":{"type":"text","analyzer":"ik_max_word"} } } }
GET /_cluster/settings PUT /_cluster/settings {"persistent": {"search.max_buckets": 1000000}}
2、设置动态模板
设置动态模板
PUT index_name { "mappings": { "dynamic_templates": [ { "content": { "match": "content", "mapping": { "type": "text", "analyzer": "standard", "search_analyzer": "english" } } }, { "title": { "match": "tit*", "mapping": { "type": "text", "analyzer": "english" } } } ], "properties": { "content": { "type": "text", "term_vector": "with_positions_offsets", "similarity": "BM25", "analyzer": "standard", "fielddata": true }, "summary": { "type": "text", "term_vector": "with_positions_offsets", "similarity": "BM25", "analyzer": "standard", "fielddata": true }, "title": { "type": "text", "term_vector": "with_positions_offsets", "similarity": "BM25", "analyzer": "standard" } } } }
3、scroll滚动查询
I、滚动scroll查询
# 查询media为“新闻报”的内容,查询200条,时间为1000ms **GET index_name/_search?scroll=1000ms { "query": { "match": { "media": "新闻报" } }, "size": 200 }**
II、continue继续scroll查询(scorll查询后会返回一个scroll_id,可以通过scroll_id继续查询)
GET _search/scroll { "scroll": "1000ms", "scroll_id":"FGluY2x1ZGVfY29udGV4dF91dWlkDnF1ZXJ5VGhlbkZldGNoBRY0bWVKNHJoelJLU3h4bE5FTFFEU3BnAAAAAAAOXp8WUHdKQnB6WGpSRzZndWZVaEppVFNnQRY0bWVKNHJoelJLU3h4bE5FTFFEU3BnAAAAAAAOXqEWUHdKQnB6WGpSRzZndWZVaEppVFNnQRY0bWVKNHJoelJLU3h4bE5FTFFEU3BnAAAAAAAOXqMWUHdKQnB6WGpSRzZndWZVaEppVFNnQRY0bWVKNHJoelJLU3h4bE5FTFFEU3BnAAAAAAAOXqAWUHdKQnB6WGpSRzZndWZVaEppVFNnQRY0bWVKNHJoelJLU3h4bE5FTFFEU3BnAAAAAAAOXqIWUHdKQnB6WGpSRzZndWZVaEppVFNnQQ=" }
4、分词查询
查看分词器解析效果
GET /index_name/_analyze { "field": "title", "text": "The quick Brown Foxes." }
5、聚合查询
分组查询
# index_name是查询的索引,emotionGroup是分组名称(可自行设置),field后是分组的字段 GET index_name/_search { "aggs":{ "emotionGroup":{ "terms":{ "field":"emotion" } } }, "size": 0 #查询显示的文档条目数 }
分组后求和
GET index_name/_search { "aggs":{ "aggGroup":{ # 自己设置名称 "terms":{ "field":"emotion" }, "aggs":{ "emotionSum":{ # 自己设置名称 "sum": { "field": "emotion" } } } } }, "size": 0 }
求平均值
GET index_name/_search { "aggs":{ "emotionAvg":{ # 自己设置名称 "avg":{ "field":"emotion" } } }, "size": 0 }
获取前3名操作
GET index_name/_search { "aggs":{ "top3":{ # 自己设置名称 "top_hit":{ "size":3 } } }, "size": 0 }
先排序,再获取前3名操作
GET index_name/_search { "aggs":{ "top3":{ "top_hits":{ "sort": [{ "emotion":{ "order": "desc" } }], "size":3 } } }, "size": 0 }