Elasticsearch7.10.2接口Demo记录(持续更新中···)
版本:7.10.2
分词插件:analysis-ik
索引
创建索引
put localhost:9200/索引名称
//响应
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "schools"
}
映射
get localhost:9200/索引名称/_mapping
{
"properties": {
"id": {
"type": "integer", //字段类型
"index": true //是否索引。true:可以被搜索,false:不能被搜索
}, //字段值
"content": {
"type": "text",
"index": true,
"analyzer": "ik_max_word" //指定分词器,这里使用IK分词器的最细分词模式
}
}
}
//响应
{
"acknowledged": true
}
查看某个索引信息
get localhost:9200/索引名称
//响应
{
"news": {
"aliases": {},
"mappings": {
"properties": {
"content": {
"type": "text"
},
"id": {
"type": "integer"
}
}
}, //映射信息
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "news",
"creation_date": "1656060844110",
"number_of_replicas": "1",
"uuid": "n9pBE7LoSkuWHGfkTWW2Bg",
"version": {
"created": "7100299"
}
}
}
}
}
删除索引
delete localhost:9200/索引名称
//响应
{
"acknowledged": true
}
文档
创建文档
post localhost:9200/索引名称/_doc
{
"id": 10002,
"content": "阿凡达2预计今年上映"
}
//响应
{
"_index": "news",
"_type": "_doc",
"_id": "XqjxlIEBA0SYXO9Jq6zp",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
创建文档(携带主键)
post localhost:9200/索引名称/_doc/主键
{
"id": 10003,
"content": "今年粮食大丰收"
}
//响应
{
"_index": "news",
"_type": "_doc",
"_id": "id-10003", //主键
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
查看单个文档(主键查询)
get localhost:9200/索引名称/_doc/主键
//响应
{
"_index": "news",
"_type": "_doc",
"_id": "id-10003",
"_version": 1,
"_seq_no": 3,
"_primary_term": 1,
"found": true,
"_source": {
"id": 10003,
"content": "今年粮食大丰收"
}
}
查看全部文档
get localhost:9200/索引名称/_search
//响应
{
"took": 59,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "news",
"_type": "_doc",
"_id": "XKjvlIEBA0SYXO9Jb6zJ",
"_score": 1.0,
"_source": {
"id": 10000,
"content": "今年是中国成立70周年,天安门广场举行了盛大的阅兵仪式,现场人山人海,CCTV全程直播"
}
},
{
"_index": "news",
"_type": "_doc",
"_id": "XajwlIEBA0SYXO9JoaxW",
"_score": 1.0,
"_source": {
"id": 10001,
"content": "美国从阿富汗撤兵,结束了长达20年的阿富汗战争,大量装备被遗弃在阿富汗"
}
},
{
"_index": "news",
"_type": "_doc",
"_id": "XqjxlIEBA0SYXO9Jq6zp",
"_score": 1.0,
"_source": {
"id": 10002,
"content": "阿凡达2预计今年上映,导演是卡梅隆,将由阿凡达1原班人马出演,具体上映时间待定"
}
},
{
"_index": "news",
"_type": "_doc",
"_id": "id-10003",
"_score": 1.0,
"_source": {
"id": 10003,
"content": "今年粮食大丰收"
}
}
]
}
}
根据条件查询文档
get localhost:9200/索引名称/_search
//匹配类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是 or 的关系。
{
"query": {
"match": {
"字段1": "中国"
}
}
}
//把多个词条的关系变为and
{
"query": {
"match": {
"字段1": {
"query": "美国",
"operator": "and"
}
}
}
}
分页+排序查询
{
"query": {
"match_all": {}
},
"from": 0, //起始位置,同MySQL中的offset,计算公式:(页码-1)*size
"size": 2, //显示数量,同MySQL中的limit
"_source": [
"字段1",
"字段2"
], //显示的字段
"sort": {
"字段1": {
"order": "desc" //asc:升序排列
} //通过字段id倒序排列
}
}
多条件查询
bool:把各种其他查询通过must(必须and)、nust_not(必须不)、should(应该or)的方式进行组合
and
{
"query": {
"bool": {
"must": [
{
"match": {
"字段1": "中国"
}
},
{
"match": {
"字段2": 100
}
}
]
}
}
}
or
{
"query": {
"bool": {
"should": [
{
"match": {
"字段1": "中国"
}
},
{
"match": {
"字段2": 100
}
}
]
}
}
}
本作品采用《CC 协议》,转载必须注明作者和本文链接