zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

ElasticSearch的常用api

2023-09-27 14:24:16 时间

以下操作之前,先看下服务启动情况
http://127.0.0.1:9200/

版本:6.3.1

1、索引操作

操作方式接口参数
集群健康GET/_cat/health?v-
节点列表GET/_cat/nodes?v-
索引列表GET/_cat/indices?v-
创建索引PUT/website-
查看设置GET/website/_settings-
设置索引PUT/website/_settings{"number_of_replicas": "2"}
复制索引POST/_reindex{"source":{"index":"website"}, "dest":{"index":"test002"}}
关闭索引POST/website/_close-
打开索引POST/website/_open-
删除索引DELETE/website-

2、文档操作

操作方式接口参数
添加文档PUT/website/blog/001{"name": "tom"}
获取文档GET/website/blog/001-
检查文档是否存在HEAD/website/blog/001-
一次取多个POST/website/blog/_mget{"docs": [{"_id": "001"}, {"_id": "002"}] }
搜索多个POST/website/_search{"query":{"terms": {"_id":["001", "002"]}}}
更新文档(doc方式)POST/website/blog/001/_update{"doc":{"name":"Jack" }}
更新文档(脚本方式)POST/website/blog/001/_update{ "script":{"inline": "ctx._source.star += params.star", "lang":"painless","params":{ "star":100} }}
查询更新POST/website/_update_by_query{"script":{"inline":"ctx._source.age = 23", "lang":"painless"}, "query":{"term":{"name":"Tom"} } }
删除文档(指定ID)DELETE/website/blog/001-
删除文档(带查询条件)DELETE/website{"query":{"term":{"name":"Tom"} } }
批量操作(多行格式)POST/_bulk{"index":{ "_index": "books", "_type": "IT", "_id": "1" }}{"name": "Tom"}

3、搜索操作

搜索查询
POST | /website/_search

操作参数
查看所有{"query":{"match_all":{} } }
精确匹配{"query":{"constant_score":{"filter":{"term":{"price":549} } } } }
词项查询(term query){"query":{"term":{"title":"java"} } }
分词查询(match query){"query":{"match":{"title":"Core Java"} } }
分词查询(全部匹配){"query":{"match":{"title":{"query":"Core Java", "operator":"and"} } } }
顺序匹配{"query":{"match_phrase":{"title":"Core Java"} } }
前缀匹配{"query":{"match_phrase_prefix":{"title":"Core J"} } }
多字段搜索{"query":{"multi_match":{"query":"1986 deep", "fields":["title", "description"] } } }
多词查询{"query":{"terms":{"title":["deep", "core"] } } }
范围查询{"query":{"range":{"publish_time":{"gte":"2016-01-01", "lte":"2016-12-31", "format":"yyyy-MM-dd"} } } }
存在字段查询{"query":{"exists":{"field":"author"} } }
前缀查询{"query":{"prefix":{"title":"cor"} } }
通配符查询("?“一个,”*"多个){"query":{"wildcard":{"title":"cor?"} } }
正则表达式{"query":{"regexp":{"description":"[0-9]{4}"} } }
模糊查询{"query":{"fuzzy":{"description":"1987"} } }
复合查询(must, should, must_not, filter){"query":{"bool":{"must":{"term":{"title":"java"} }, "must_not":[{"term":{"title":"core"}} ] } } }
脚本查询{"query":{"bool":{"must":{"script":{"script":{"inline":"doc['price'].value>500", "lang":"painless"} } } } } }
指定排序字段{"query":{"term":{"title":"java"} }, "sort":[{"price":{"order":"desc"}} ] }

4、其他操作

操作方式接口参数
分词效果POST/website/_analyze{"field":"title", "text":"Core Java", "analyzer": "ik_smart"}
设置分词器PUT/blog{"settings" : {"index" : {"analysis.analyzer.default.type": "ik_max_word"} } }
分页GET/website/_search?size=10&from=0-
高亮POST/website/_search{"query" : {"match_phrase" : {"about" : "rock climbing"} }, "highlight": {"fields" : {"about" : {} } } }
数量GET/website/_count-

参考

  1. Elasticsearch集群和索引常用命令
  2. elasticsearch实战三部曲之一:索引操作
  3. elasticsearch实战三部曲之二:文档操作
  4. elasticsearch实战三部曲之三:搜索操作
  5. 修改ES默认分词设置