社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Elasticsearch

【ES】Elasticsearch的基本操作及相关概念-03

锥栗 • 3 年前 • 342 次点击  
阅读 143

【ES】Elasticsearch的基本操作及相关概念-03

前言

所有操作基于kibana的 devs tools,相关的安装可以看:

【ES学习】ElasticSearch MacOS版 安装与使用(图文教程)

【ES学习】ElasticSearch在Kibana的使用 Kibana安装(MacOS版)

本文承接【ES】Elasticsearch的基本操作及相关概念-01

本文承接【ES】Elasticsearch的基本操作及相关概念-02

先导入更多的数据:

PUT /megacorp/employee/5
{
    "first_name" :  "Hone",
    "last_name" :   "Xiao",
    "age" :         40,
    "about" :       "I graduated from the Cambridge",
    "interests":  [ "photography", "hiking"]
}

PUT /megacorp/employee/6
{
    "first_name" :  "Qiang",
    "last_name" :   "Xiao",
    "age" :         29,
    "about" :       "I have research on computers",
    "interests":  [ "hiking", "music"]
}

PUT /megacorp/employee/7
{
    "first_name" :  "Bei",
    "last_name" :   "Bei",
    "age" :         27,
    "about" :       "I am good at playing piano",
    "interests":  [ "photography", "sports"]
}

PUT /megacorp/employee/8
{
    "first_name" :  "Ge",
    "last_name" :   "Feng",
    "age" :         38,
    "about" :       "I am an investor in MCN",
    "interests":  [ "diving", "alpinism"]
}

PUT /megacorp/employee/9
{
    "first_name" :  "Jie",
    "last_name" :   "G",
    "age" :         38,
    "about" :       "I am a part-time model and anchor",
    "interests":  [ "diving", "reading"]
}

PUT /megacorp/employee/10
{
    "first_name" :  "Liya",
    "last_name" :   "Jie",
    "age" :         27,
    "about" :       "I like dancing and music",
    "interests":  [ "music", "dancing"]
}
复制代码

from-size 分页查询

这就像极了MySQL中的LIMIT a, b,ES中也可以通过规定起始位置和展示数量,来达到分页搜索的效果。

我们可以输入:

=========== 表达式 =========
GET /索引index/类型types/_search
{
  "from": 起始位置,
  "size": 页面size
}
==========================
GET /megacorp/employee/_search
{
  "from": 0,
  "size": 5
}
复制代码

结果会显示5条文档,并且从第0个开始。

我们再输入:

GET /megacorp/employee/_search
{
  "from": 5,
  "size": 5
}
复制代码

也会显示5条文档,但是是从第5条文档开始。

如果from+size超过index.max_result_window(默认10000),会报错

在这里插入图片描述 数据分片存储下,如果"from":"400","size":"10",那就需要在每一个分片上,处理400+10=410条数据,再聚合各个分片上的410条结果取前10条数据再返回。所以,页数越深,处理的文档就越多,占用的堆内存也就越大,耗时越长,效率越低。

scroll 分页查询

scroll通过生成快照的方式避免深度分页问题。

我们需要先scroll search:

GET /megacorp/employee/_search?scroll=10m
{
  "size" : 5,
  "query": {
    "fuzzy": {
      "about": "to"
    }
  }
}
复制代码

记住scroll_id,我们输入:

POST _search/scroll
{
  "scroll" : "10m",
  "scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFjl6OVRkQkI1UklTNElVU1VDVjJnOVEAAAAAAAAHtBZjX1o2MklfOVJWcWRhR2F1QXpjUnB3"
}
复制代码

在这里插入图片描述

可以看出scroll并不是实时性的。

search_after 向下翻页

search_after可以做实时搜索。需要先进行正常的搜索,要指定sort值,并且值要唯一。然后使用之前获取到的sort值进行search_after:

我们先输入sort语句查询,按照"age"降序(sort可以多条件,我们只给单条件":

GET /megacorp/employee/_search
{
  "size" : 3,
  "query": {
    "fuzzy": {
      "about": "to"
    }
  },
  "sort" : [
    {
      "age" : "desc"
    }
  ]
}
复制代码

得到:




    
{
  "took" : 34,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "first_name" : "Douglas",
          "last_name" : "Fir",
          "age" : 35,
          "about" : "I like to build cabinets",
          "interests" : [
            "forestry"
          ]
        },
        "sort" : [
          35
        ]
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "first_name" : "Jane",
          "last_name" : "Smith",
          "age" : 32,
          "about" : "I like to collect rock albums",
          "interests" : [
            "music"
          ]
        },
        "sort" : [
          32
        ]
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "first_name" : "John",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        },
        "sort" : [
          25
        ]
      }
    ]
  }
复制代码

然后我们输入:

GET /megacorp/employee/_search
{
  "size" : 3,
  "search_after": [35],
  "sort" : [
    {
      "age" : "desc"
    }
  ]
}
复制代码

得到:

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "first_name" : "Jane",
          "last_name" : "Smith",
          "age" : 32,
          "about" : "I like to collect rock albums",
          "interests" : [
            "music"
          ]
        },
        "sort" : [
          32
        ]
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "6",
        "_score" : null,
        "_source" : {
          "first_name" : "Qiang",
          "last_name" : "Xiao",
          "age" : 29,
          "about" : "I have research on computers",
          "interests" : [
            "hiking",
            "music"
          ]
        },
        "sort" : [
          29
        ]
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "7",
        "_score" : null,
        "_source" : {
          "first_name" : "Bei",
          "last_name" : "Bei",
          "age" : 27,
          "about" : "I am good at playing piano",
          "interests" : [
            "photography",
            "sports"
          ]
        },
        "sort" : [
          27
        ]
      }
    ]
  }
}
复制代码

count 命中数量

count可以计算搜索到结果的数量。

我们输入:

GET /megacorp/employee/_count
{
  "query": {
    "fuzzy": {
      "about": "to"
    }
  }
}
复制代码

可以得到:

{
  "count" : 3,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  }
}
复制代码

还可以多个索引index查找

GET megacorp,blogs/_count
{
  "query": {
    "fuzzy": {
      "about": "to"
    }
  }
}
复制代码

HEAD 判断存在

我们可以使用HEAD命令来判断索引是否存在:

HEAD /megacorp
复制代码

返回200是存在。 返回404是不存在。

_close / _open 关闭/开启索引

索引关闭会消耗大量的磁盘空间。

我们输入:

POST /megacorp/_close
复制代码

可以将索引关闭,如下:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "indices" : {
    "megacorp" : {
      "closed" : true
    }
  }
}
复制代码

此时索引搜东西是搜不了的。

我们再通过_open开启索引:

POST /megacorp/_open
复制代码

_rollover 滚动索引

1)滚动索引是设置索引,而非创建索引,且设置一次结果返回 “rolled_over” : true,则会按照设定规则创建新索引,名字递增,而非一次设置永久有效。

2)注意点2:设置滚动索引会出现两个别名,一个读取别名(在模板中定义),一个写入别名(在创建index中指定)

滚动索引的作用防止索引过大或过旧时,滚动索引API会将别名滚动到新的索引。别名指向到新的索引后,旧的索引就无法查询到。

我们输入:

PUT /logs-000001 
{
  "aliases": {
    "logs_write": {}
  }
}
复制代码

用_rollover并指定条件:创建时间大于7天或者文档数大于1000,将发生滚动操作,并生成新的索引logs-000002而且别名也指向新的索引。我们输入:

POST /logs_write/_rollover 
{
  "conditions": {
    "max_age":   "7d",
    "max_docs":  1000
  }
}
复制代码

_mapping 索引结构查询

Mapping就是索引结构,可以看做是数据库中的表结构,包括字段名、字段类型、倒排索引相关设置。

我们输入:

GET /_mapping
复制代码

在这里插入图片描述 会显示我们曾经创建过的所有索引的结构。

我们输入:

GET /megacorp/_mapping
复制代码

可以得到megacorp索引的结构:

在这里插入图片描述

_aliases 索引指定别名

我们创建一个新索引:

PUT /source_name
复制代码

然后输入:

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "索引名字", "alias" : "索引别名" } }
    ]
}
============================================
POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "source_name", "alias" : "other_name" } }
    ]
}
复制代码

这样我们GET 这两个索引名字,指向的都是同一个:

在这里插入图片描述

注意:这里的"actions"是原子操作。

_stats 索引统计信息

查看索引统计状态,我们输入:

GET /megacorp/_stats
复制代码

我的得到:

{
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_all" : {
    "primaries" : {
      "docs" : {
        "count" : 10,
        "deleted" : 1
      },
      "store" : {
        .......
      },
      "indexing" : {
        .......
      },
      "get" : {
        .......
      },
      "search" : {
        .......
      },
      "merges" : {
        .......
      },
      "refresh" : {
        .......
      },
      "flush" : {
        .......
      },
      "warmer" : {
        .......
      },
      "query_cache" : {
        .......
      },
      "fielddata" : {
        .......
      },
      "completion" : {
        "size_in_bytes" : 0
      },
      "segments" : {
        ........
      },
      "translog" : {
        ........
      },
      "request_cache" : {
        ........
      },
      "recovery" : {
        .........
      }
    },
    .......
复制代码

primaries:主分区的统计结果。

total:主分区和副本统计结果。

各个字段的含义: docs:所有文档的个数和删除的文档个数。

store:占用的存储的字节数.。

indexing:文档级别的统计。

search :query统计。

segments :内存分片。

completion : fielddata:字段空间统计。 flush :刷新到磁盘统计。 merge :合并统计。 request_cache:缓存命中统计。 refresh :刷新到可以被查询到统计(有可能还有flush到磁盘)。 translog :translog 统计。 segments:获取segments信息,索引信息在segments层级的情况 indices recovery:获取索引恢复的情况。

_settings 索引设置信息

我们输入:

GET /megacorp/_settings
复制代码

可以得到:

{
  "megacorp" : {
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "megacorp",
        "creation_date" : "1629699262885",
        "number_of_replicas" : "1",
        "uuid" : "5skqj50KQf-eUSH0JUwIUA",
        "version" : {
          "created" : "7140099"
        }
      }
    }
  }
}
复制代码

_cache-clear 清除缓存

我们输入:

POST /megacorp/_cache/clear
复制代码

可以清除缓存

_flush 刷新数据到磁盘

_flush可以刷新数据到磁盘上,释放内存空间:

POST /megacorp/_flush
复制代码

参考

love1024.blog.csdn.net/article/det…

blog.csdn.net/qq_18218071…

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/119333
 
342 次点击