ElasticSearch入门

以下都是基于elasticsearch7.1版本。

GET /_cat/health?v:检查当前整个es服务器的状态

GET /_cat/indecis?v:检查当前整个es服务器索引的分布情况

关于主分片和副本分片的情况,不仅仅是主和副不能在同一个node,不同的副本也不能在同一个node!

// 索引构建

PUT /index_name:创建索引

下图是加了test_index和test_index2索引后单机ES的情况,从health看出,此时的unassign为2,未分配数据分片有两个,他们分别是前两个索引的副本分片(默认是主和副1:1)shards代表已分配的分片数:5,主分片:5,所以为yellow状态

创建文档:

创建完成后,shards里显示total为2,说明当前索引所拥有的分片数为2,successful为1,正常情况主和副都应该添加进去,但是此时我们的node为1,副本分片不允许分配数据,所以successful为1

获取文档:

GET /test_index/_doc/1

更新文档:

PUT /test_index/_doc/1

{}

put操作是替换,要想更新某一个字段:

POST test_index/_update/1
{
"doc":{
"field":"哈哈"
}
}

就可以更新某一个field了

查询:

{
"took" : 6, # 花费6毫秒
"timed_out" : false,
"_shards" : {
"total" : 1, # 为了搜集满足条件的数据,需要打到1个分片(主或者副)上面
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "哈哈",
"age" : 15
}
}
]
}
}

排序加分页:

GET /test_index/_search
{
"query": {
"match": {
"name": "张三"
}
}
, "sort": [
{
"age":"asc"
}
],
"from": 0,
"size": 1
}

指定字段查询:

GET /test_index/_search
{
"query": {
"match": {
"name": "张三"
}
}
, "sort": [
{
"age":"asc"
}
],
"from": 0,
"size": 1,
"_source": ["name"]# 指定name为返回字段
}

多个查询条件和过滤条件:

POST /test_index/_search
{
"query": {
"bool": {
"must": {
"match": {
"name":"张三"
}
},
"filter": {
"range": {
"age": {
"gte": 20,
"lte": 30
}
}
}
}
}
}

全文检索:

POST /test_index/_search
{
"query": {
"match_phrase": {
"name": "good news"
}
}
}# 上面的phrase,要求name字段的分词里必须要要有"good news",但是:

POST /test_index/_search
{
"query": {
"match": {
"name": "good news"
}
}# 这个是全文检索,good news被分词为good、news、good news三个,所以满足三个其中一个即可被选中

原文地址:https://www.cnblogs.com/Booker808-java/p/12595684.html