# 分词器ik_max_word 最细粒度的拆分 POST _analyze { "analyzer": "ik_max_word", "text": "南京市长江大桥" }
# ik_smart 最粗粒度的拆分 POST _analyze { "analyzer": "ik_smart", "text": "南京市长江大桥" }
# 创建索引 PUT /zfzn
# 判断索引是否存在 HEAD /zfzn
# 查看索引,多个用逗号拼接 GET /zfzn
# 查看所有索引 GET _all
GET /_cat/indices?v
# 打开索引 POST /zfzn/_open
# 关闭索引 POST /zfzn/_close
# 删除索引 DELETE /zfzn
# 创建映射(表字段) PUT /zfzn PUT /zfzn/_mapping/ { "properties":{ "name":{ "type":"text", "analyzer":"ik_max_word" }, "age":{ "type":"integer" }, "remark":{ "type":"text" } } }
PUT /zfzn1 { "mappings": { "properties": { "name": { "type": "text", "analyzer": "ik_max_word" }, "age": { "type": "integer" }, "remark": { "type": "text" } } } }
# 查看映射(表字段) GET /zfzn/_mapping
# 查看所有映射 GET /_all/_mapping
# 修改映射(只能增加字段) PUT /zfzn/_mapping/ { "properties":{ "name":{ "type":"text", "analyzer":"ik_max_word" }, "age":{ "type":"integer" }, "remark":{ "type":"text" }, "sex":{ "type":"text" } } }
# 新增文档(表数据) POST /zfzn/_doc/1 { "name":"小冰", "age":"19", "remark":"我是一个演员", "sex":"女" }
# 查看单个文档 GET /zfzn/_doc/1
# 查看所有文档 GET /zfzn/_search { "query": { "match_all": {} } }
# 查询指定字段 GET /zfzn/_doc/1?_source=name,age
# 更新文档(id存在则更新,不存在则新增) PUT /zfzn/_doc/1 { "name":"小冰改", "age":"19", "remark":"我是一个演员", "sex":"女" }
# 更新指定字段 POST /zfzn/_update/1 { "doc": { "remark":"不当演员,我要回家种田" } }
# 删除文档(根据ID) DELETE /zfzn/_doc/-jRdFYIBm3HYdyw3Px1X
# 根据条件删除 (注意name使用了分词器) POST /zfzn/_delete_by_query { "query":{ "match":{ "name" : "小莫" } } }
# 删除所有文档 POST /zfzn/_delete_by_query { "query": { "match_all": {} } }
# 新增测试数据 POST /zfzn/_doc { "name":"林动", "age":"29", "remark":"我是一个辣鸡", "sex":"男" }
POST /zfzn/_doc { "name":"林子", "age":"3", "remark":"我是一个零", "sex":"男" }
POST /zfzn/_doc { "name":"萧炎", "age":"25", "remark":"我是一个动漫", "sex":"男" }
POST /zfzn/_doc { "name":"鳄鱼", "age":"39", "remark":"我是一个鱼", "sex":"男" }
POST /zfzn/_doc { "name":"花生", "age":"39", "remark":"我是一个花", "sex":"男" }
POST /zfzn/_doc { "name":"电脑", "age":"15", "remark":"我是一个电", "sex":"男" }
POST /zfzn/_doc { "name":"无天", "age":"66", "remark":"我是一个天", "sex":"男" }
POST /zfzn/_doc { "name":"无花电脑001", "age":"66", "remark":"我是一个天", "sex":"男" }
POST /zfzn/_doc { "name":"无花001电脑", "age":"66", "remark":"我是一个天", "sex":"男" }
POST /zfzn/_doc { "name":"无花看起来真不错啊001有什么好吃的电脑", "age":"66", "remark":"我是一个天", "sex":"男" }
# 多词条查询(默认or条件) POST /zfzn/_search { "query": { "match": { "name": "无花电脑" } } }
POST /zfzn/_search { "query": { "match": { "name":{ "query": "无花电脑","operator": "and" } } } }
# 短语搜索 GET /zfzn/_search { "query": { "match_phrase": { "name": "无花 001" } } }
GET /zfzn/_search { "query": { "match_phrase": { "name": { "query": "无花 001", "slop": 2 } } } }
# 高级查询,无需指定字段匹配 GET /zfzn/_search { "query": { "query_string": { "query": "天" } } }
GET /zfzn/_search { "query": { "query_string": { "query": "天", "default_field": "name" } } }
GET /zfzn/_search { "query": { "query_string": { "query": "天", "fields": ["name","remark"] } } }
# 词条搜索,不分析搜索词
PUT /zfzn PUT /zfzn/_mapping/ { "properties":{ "name":{ "type":"keyword" }, "age":{ "type":"integer" }, "remark":{ "type":"text" } } }
POST /zfzn/_search { "query": { "term": { "name": "天" } } }
# 多词条 GET /zfzn/_search { "query": { "terms": { "name": [ "天", "萧" ] } } }
# 范围搜索
GET /zfzn/_search { "query": { "range": { "age": { "gte": 10, "lte": 30 } } } }
# 非空搜索
POST /zfzn/_doc { "name":"", "age":"66", "remark":"我是空的", "sex":"男" }
GET /zfzn/_search { "query": { "exists": { "field": "name" } } }
# 前缀搜索 GET /zfzn/_search { "query": { "prefix": { "name": "无" } } }
# 通配符搜索 GET /zfzn/_search { "query": { "wildcard": { "name": "001*" } } }
# 正则搜索 GET /zfzn/_search { "query": { "regexp": { "name": "无*" } } }
# 模糊搜索 GET /zfzn/_search { "query": { "fuzzy": { "name": "萧" } } }
# ids搜索
GET /zfzn/_search { "query": { "ids": { "type": "_doc", "values": [ "QDSIFYIBm3HYdyw3Dx_k", "QTSIFYIBm3HYdyw3ER8Q" ] } } }
# 布尔搜索
POST /zfzn/_search { "query": { "bool": { "must": { "match": { "name": "天" } }, "filter": { "term": { "name": "无" } }, "must_not": { "range": { "age": { "gte": 60, "lte": 90 } } }, "boost": 1 } } }
# 排序 POST /zfzn/_search { "query": { "match_all": {} }, "sort": [ { "age": { "order": "desc" } } ] }
# 分页 POST /zfzn/_search { "query": { "match_all": {} }, "size": 2, "from": 0 }
# 函数使用 POST /zfzn/_search { "size": 0, "aggs": { "max_age": { "max": { "field": "age" } } } }
POST /zfzn/_search { "size": 0, "aggs": { "min_age": { "min": { "field": "age" } } } }
POST /zfzn/_search { "size": 0, "aggs": { "sum_age": { "sum": { "field": "age" } } } }
# 计数 POST /zfzn/_count { "query": { "range": { "age": { "gt": 10 } } } }
# 某字段有值的文档数 POST /zfzn/_search?size=0 { "aggs": { "age_count": { "value_count": { "field": "age" } } } }
# 去重计数 POST /zfzn/_search?size=0 { "aggs": { "age_count": { "cardinality": { "field": "age" } } } }
# 统计多个函数值 POST /zfzn/_search?size=0 { "aggs": { "price_stats": { "stats": { "field": "age" } } } }
# 分组 带having POST /zfzn/_search { "size": 0, "aggs": { "group_by_age": { "range": { "field": "age", "ranges": [ { "from": 0, "to": 20 }, { "from": 20, "to": 40 }, { "from": 40, "to": 100 } ] }, "aggs": { "average_age": { "avg": { "field": "age" } }, "having": { "bucket_selector": { "buckets_path": { "avg_age": "average_age" }, "script": { "source": "params.avg_age >= 50 " } } } } } } }
|