使用elasticsearch7.6.0版本。
索引就相当于数据库中的一张表,索引中的文档就相当于表中的信息。
put http://localhost:9200/movice -d
{
"mappings" : {
"properties" : {
"name" : {
"type" : "keyword"
}
}
},
"settings" : {
"index" : {
"number_of_shards" : 1,
"number_of_replicas" : 2
}
}
}
解释:
properties里的name就是相当于表中的字段信息。
参考:https://www.elastic.co/guide/en/elasticsearch/reference/7.6/mapping-types.html
如下图:
http://localhost:9200/_cat/indices?v
如下图:
get http://localhost:9200/movice
如下图:
比如在我的elasticsearch中。 我的索引student是不存在的。
post http://localhost:9200/student/_doc/8
如下图:
响应结果说明:
指定id的话就会使用指定的id,如果不指定id的话就会使用随机字符串
post http://localhost:9200/student/_doc
如下图:
get http://localhost:9200/student/_search
如下图:
get http://localhost:9200/student/_search -d
{
"query":{
"match":{
"name":"我"
}
}
}
如下图:
返回值:
{
"took": 53,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 0.8712301,
"hits": [
{
"_index": "student",
"_type": "_doc",
"_id": "7",
"_score": 0.8712301,
"_source": {
"name": "我来了"
}
},
{
"_index": "student",
"_type": "_doc",
"_id": "8",
"_score": 0.77391195,
"_source": {
"name": "我是8号"
}
},
{
"_index": "student",
"_type": "_doc",
"_id": "neypDHIBujABphC4Wrfw",
"_score": 0.49657142,
"_source": {
"name": "我是九号,没有指定id"
}
}
]
}
}
get http://localhost:9200/student/_search -d
{
"query":{
"match_all":{}
},
"from":0,
"size":2
}
解释:from:从第1页开始显示,只显示2个文档信息。
如下图:
返回值:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "student",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "张三"
}
},
{
"_index": "student",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"name": "李四"
}
}
]
}
}
get http://localhost:9200/student/_search
{
"query": { "match_all": {} },
"_source": ["name"]
}
如下图:
原文:https://www.cnblogs.com/zhenzi0322/p/12882003.html