-
Notifications
You must be signed in to change notification settings - Fork 1
/
2018051401.html
1 lines (1 loc) · 111 KB
/
2018051401.html
1
<!DOCTYPE html><html class="theme-next mist use-motion" lang="zh-Hans"><head><meta name="generator" content="Hexo 3.9.0"><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><meta name="theme-color" content="#222"><script src="/lib/pace/pace.min.js?v=1.0.2"></script><link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css"><link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css"><link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css"><link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-240x240-playpi.png?v=5.1.3"><link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-playpi.png?v=5.1.3"><link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-playpi.png?v=5.1.3"><link rel="mask-icon" href="/images/logo-playpi.svg?v=5.1.3" color="#222"><meta name="keywords" content="Elasticsearch,HTTP,RESTful"><link rel="alternate" href="/atom.xml" title="虾丸派" type="application/atom+xml"><meta name="description" content="本文记录工作中常用的关于 Elasticsearch 的 HTTP 接口,以作备用,读者也可以参考,会持续补充更新。开发环境基于 Elasticsearch v5.6.8、v1.7.5、v2.x。"><meta name="keywords" content="Elasticsearch,HTTP,RESTful"><meta property="og:type" content="article"><meta property="og:title" content="Elasticsearch 常用 HTTP 接口"><meta property="og:url" content="https://www.playpi.org/2018051401.html"><meta property="og:site_name" content="虾丸派"><meta property="og:description" content="本文记录工作中常用的关于 Elasticsearch 的 HTTP 接口,以作备用,读者也可以参考,会持续补充更新。开发环境基于 Elasticsearch v5.6.8、v1.7.5、v2.x。"><meta property="og:locale" content="zh-Hans"><meta property="og:updated_time" content="2020-01-14T18:16:56.000Z"><meta name="twitter:card" content="summary"><meta name="twitter:title" content="Elasticsearch 常用 HTTP 接口"><meta name="twitter:description" content="本文记录工作中常用的关于 Elasticsearch 的 HTTP 接口,以作备用,读者也可以参考,会持续补充更新。开发环境基于 Elasticsearch v5.6.8、v1.7.5、v2.x。"><script type="text/javascript" id="hexo.configurations">var NexT=window.NexT||{},CONFIG={root:"/",scheme:"Mist",version:"5.1.3",sidebar:{position:"left",display:"hide",offset:12,b2t:!1,scrollpercent:!0,onmobile:!1},fancybox:!0,tabs:!0,motion:{enable:!0,async:!1,transition:{post_block:"fadeIn",post_header:"slideDownIn",post_body:"slideDownIn",coll_header:"slideLeftIn",sidebar:"slideUpIn"}},duoshuo:{userId:"0",author:"博主"},algolia:{applicationID:"",apiKey:"",indexName:"",hits:{per_page:10},labels:{input_placeholder:"Search for Posts",hits_empty:"We didn't find any results for the search: ${query}",hits_stats:"${hits} results found in ${time} ms"}}}</script><link rel="canonical" href="https://www.playpi.org/2018051401.html"><title>Elasticsearch 常用 HTTP 接口 | 虾丸派</title></head><body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans"><div class="container sidebar-position-left page-post-detail"><div class="headband"></div><header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-wrapper"><div class="site-meta"><div class="custom-logo-site-title"><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title">虾丸派</span> <span class="logo-line-after"><i></i></span></a></div><h1 class="site-subtitle" itemprop="description">烂笔头</h1></div><div class="site-nav-toggle"><button><span class="btn-bar"></span> <span class="btn-bar"></span> <span class="btn-bar"></span></button></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i><br>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i><br>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i><br>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i><br>归档</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i><br>关于</a></li><li class="menu-item menu-item-books"><a href="/books/" rel="section"><i class="menu-item-icon fa fa-fw fa-book"></i><br>书籍</a></li><li class="menu-item menu-item-guide"><a href="/guide/" rel="section"><i class="menu-item-icon fa fa-fw fa-location-arrow"></i><br>指南</a></li><li class="menu-item menu-item-search"><a href="javascript:;" class="popup-trigger"><i class="menu-item-icon fa fa-search fa-fw"></i><br>搜索</a></li></ul><div class="site-search"><div class="popup search-popup local-search-popup"><div class="local-search-header clearfix"><span class="search-icon"><i class="fa fa-search"></i> </span><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span><div class="local-search-input-wrapper"><input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input"></div></div><div id="local-search-result"></div></div></div></nav></div></header><main id="main" class="main"><div class="main-inner"><div class="content-wrap"><div id="content" class="content"><div id="posts" class="posts-expand"><article class="post post-type-normal" itemscope itemtype="http://schema.org/Article"><div class="post-block"><link itemprop="mainEntityOfPage" href="https://www.playpi.org/2018051401.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="name" content="虾丸派"><meta itemprop="description" content="记录知识 | 分享技术"><meta itemprop="image" content="/images/favicon-1536x1536-playpi.png"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="虾丸派"></span><header class="post-header"><h2 class="post-title" itemprop="name headline">Elasticsearch 常用 HTTP 接口</h2><div class="post-meta"><span class="post-time"><span class="post-meta-item-text">发表于</span> <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-05-14T02:16:56+08:00">2018-05-14 </time></span><span class="post-category"><span class="post-meta-divider">|</span> <span class="post-meta-item-text">分类于</span> <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/big-data-technical-knowledge/" itemprop="url" rel="index"><span itemprop="name">大数据技术知识</span> </a></span></span><span id="busuanzi_container_page_pv" style="display:none"><span class="post-meta-divider">|</span> 阅读次数 <span id="busuanzi_value_page_pv"></span></span><div class="post-wordcount"><span class="post-meta-item-text">字数统计</span> <span title="字数统计">5,104字 </span><span class="post-meta-divider">|</span> <span class="post-meta-item-text">阅读时长 ≈</span> <span title="阅读时长">23分钟</span></div></div></header><div class="post-body" itemprop="articleBody"><p>本文记录工作中常用的关于 <code>Elasticsearch</code> 的 <code>HTTP</code> 接口,以作备用,读者也可以参考,会持续补充更新。开发环境基于 <code>Elasticsearch v5.6.8</code>、<code>v1.7.5</code>、<code>v2.x</code>。</p><a id="more"></a><h1 id="集群状态"><a href="# 集群状态" class="headerlink" title="集群状态"></a>集群状态</h1><h2 id="集群信息"><a href="# 集群信息" class="headerlink" title="集群信息"></a> 集群信息</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:9200/_cluster/stats?pretty</span><br><span class="line">http://localhost:9200/_cat/nodes</span><br><span class="line">http://localhost:9200/_cat/indices</span><br><span class="line">http://localhost:9200/_cluster/state</span><br><span class="line">http://localhost:9200/_cat/aliases</span><br><span class="line"></span><br><span class="line">GET _nodes/_all/stats/fs?pretty=true</span><br></pre></td></tr></table></figure><p>可以看到整个集群的索引数、分片数、文档数、内存使用等等信息。</p><h2 id="健康状况"><a href="# 健康状况" class="headerlink" title="健康状况"></a>健康状况</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:9200/_cat/health?v</span><br></pre></td></tr></table></figure><p>可以看到分片数量,状态【红、黄、绿】。</p><h2 id="空间使用"><a href="# 空间使用" class="headerlink" title="空间使用"></a>空间使用</h2><p>查询每个节点的空间使用情况,预估数据大小:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:9200/_cat/allocation?v</span><br></pre></td></tr></table></figure><h2 id="分片分布"><a href="# 分片分布" class="headerlink" title="分片分布"></a>分片分布</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:9200/_cat/shards</span><br></pre></td></tr></table></figure><h2 id="索引状态"><a href="# 索引状态" class="headerlink" title="索引状态"></a> 索引状态</h2><p>可以看到索引的数据条数、磁盘大小、分片个数【可以使用别名】。</p><p>各项指标解释说明参考:<a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.6/indices-stats.html" target="_blank" rel="noopener">indices-stats</a> 。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:9200/your_index/_stats</span><br></pre></td></tr></table></figure><h2 id="集群配置信息"><a href="# 集群配置信息" class="headerlink" title="集群配置信息"></a>集群配置信息</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:9200/_cluster/settings?pretty</span><br></pre></td></tr></table></figure><p>对于一些可以设置的参数,临时生效,对于集群的管理很有帮助。</p><p>例如节点黑名单:<code>cluster.routing.allocation.exclude._ip</code>,临时下线节点,类似于黑名单,分片不会往指定的主机移动,同时会把分片从指定的节点全部移除,最终可以下线该节点,可通过 <code>put transient</code> 设置临时生效。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">curl -XPUT 127.0.0.1:9200/_cluster/settings -d '{</span><br><span class="line"> "transient" :{</span><br><span class="line"> "cluster.routing.allocation.exclude._ip" : "192.168.0.1"</span><br><span class="line"> }</span><br><span class="line">}'</span><br></pre></td></tr></table></figure><p>例如临时关闭分片重分配【开启时设置值为 <code>all</code>】。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">curl -XPUT 127.0.0.1:9200/_cluster/settings -d '{</span><br><span class="line"> "transient": {</span><br><span class="line"> "cluster.routing.allocation.enable": "none"</span><br><span class="line"> }</span><br><span class="line">}'</span><br><span class="line"></span><br><span class="line">PUT /_cluster/settings/</span><br><span class="line">{</span><br><span class="line"> "transient": {</span><br><span class="line"> "cluster.routing.allocation.enable": "none"</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>设置整个集群每个节点可以分配的分片数,主要是为了数据分布均匀。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET _cluster/settings</span><br><span class="line"></span><br><span class="line">PUT /_cluster/settings/</span><br><span class="line">{</span><br><span class="line"> "transient": {</span><br><span class="line"> "cluster.routing.allocation.total_shards_per_node": "50"</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>设置慢索引阈值,指定索引进行操作,可以使用通配符:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">curl -XPUT 127.0.0.1:9200/your_index_*/_settings -d '{</span><br><span class="line"> "index.indexing.slowlog.threshold.index.info": "10s"</span><br><span class="line">}''</span><br></pre></td></tr></table></figure><p>设置慢查询阈值方式类似:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">curl -XPUT 127.0.0.1:9200/your_index_*/_settings -d '{</span><br><span class="line"> "index.indexing.slowlog.threshold.search.info": "10s"</span><br><span class="line">}'</span><br></pre></td></tr></table></figure><p>推迟索引分片的重新分配时间平【适用于 <code>Elasticsearch</code> 节点短时间离线再加入集群,提前设置好这个参数,避免从分片的复制移动,降低网络 <code>IO</code>】。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">PUT /your_index/_settings</span><br><span class="line">{</span><br><span class="line"> "settings": {</span><br><span class="line"> "index.unassigned.node_left.delayed_timeout": "5m"</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>可以使用索引别名、通配符设置,这样就可以一次性设置多个索引,甚至全部的索引。</p><h2 id="热点线程"><a href="# 热点线程" class="headerlink" title="热点线程"></a>热点线程</h2><p>查看热点线程,可以判断热点线程是 <code>search</code>,<code>bulk</code>,还是 <code>merge</code> 类型,从而进一步分析是查询还是写入导致 <code>CPU</code> 负载过高。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:9200/_nodes/node0/hot_threads</span><br><span class="line"></span><br><span class="line">http://localhost:9200/_nodes/hot_threads</span><br></pre></td></tr></table></figure><h2 id="请求队列"><a href="# 请求队列" class="headerlink" title="请求队列"></a>请求队列</h2><p>查看请求队列情况,可以看到每种类型请求的积压情况:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:9200/_cat/thread_pool?v</span><br><span class="line"></span><br><span class="line"># 添加参数可以查看各个指标 </span><br><span class="line">http://localhost:9200/_cat/thread_pool/search?v&h=node_name,ip,name,active,queue,rejected,completed,type,queue_size</span><br><span class="line"></span><br><span class="line"> 注意,size 指标【在节点启动时是 0,随着请求进来才会增加】有特殊含义,不代表配置文件中的 size 参数【fixed 类型】。</span><br><span class="line"></span><br><span class="line">size:会从 0 开始增长,表示已经开启的线程池大小,直到 max 值,即配置文件中配置的。</span><br><span class="line">min、max:含义根据线程池类型不同而不同。</span><br><span class="line"></span><br><span class="line"> 查询请求:/_tasks?detailed=true&actions=*search*</span><br><span class="line"> 取消单个请求:/_tasks/xx/_cancel</span><br><span class="line"> 取消整个节点:/_tasks/_cancel?nodes=xx&actions=*search*</span><br></pre></td></tr></table></figure><h2 id="节点配置信息"><a href="# 节点配置信息" class="headerlink" title="节点配置信息"></a>节点配置信息</h2><p>可以查看节点的 <code>JVM</code> 配置、插件信息、队列配置等等。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:9200/_nodes/node_id</span><br><span class="line">http://localhost:9200/_nodes?pretty=true</span><br><span class="line">http://localhost:9200/_nodes/stats/thread_pool?pretty=true</span><br></pre></td></tr></table></figure><p>注意,<code>thread_pool</code> 线程池相关参数自从 <code>v5.x</code> 以后不支持动态设置【即通过 <code>put</code> 接口】,只能通过更改节点的配置文件并重启节点来操作,这也说明了这个参数是对于节点生效,不同配置的节点可以设置不同的值。</p><h2 id="使用堆内存大小"><a href="# 使用堆内存大小" class="headerlink" title="使用堆内存大小"></a>使用堆内存大小</h2><p>使用</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:9200/_cat/fielddata</span><br></pre></td></tr></table></figure><p>查看当前集群中每个数据节点上被 <code>fielddata</code> 所使用的堆内存大小。</p><p>此外还可以指定字段</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:9200/_cat/fielddata?v&fields=uid&pretty</span><br><span class="line">http://localhost:9200/_cat/fielddata/uid?v&pretty</span><br></pre></td></tr></table></figure><p>按照节点、索引来查询:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"> 按照索引、分片 </span><br><span class="line">http://localhost:9200/_stats/fielddata?fields=*</span><br><span class="line"></span><br><span class="line"> 按照节点 </span><br><span class="line">http://localhost:9200/_nodes/stats/indices/fielddata?fields=*</span><br><span class="line"></span><br><span class="line"> 按照节点、索引分片 </span><br><span class="line">http://localhost:9200/_nodes/stats/indices/fielddata?level=indices&fields=*</span><br><span class="line">http://localhost:9200/_nodes/stats/indices/fielddata?level=indices&fields=_uid</span><br></pre></td></tr></table></figure><h2 id="清理缓存"><a href="# 清理缓存" class="headerlink" title="清理缓存"></a>清理缓存</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">curl localhost:9200/index/_cache/clear?pretty&filter=false&field_data=true&fields=_uid,site_name</span><br><span class="line"></span><br><span class="line"> 关于 `&bloom=false` 参数的问题,要看当前 `Elasticsearch` 版本是否支持,`v5.6.x` 是不支持了。</span><br></pre></td></tr></table></figure><h2 id="推迟索引分片的重新分配时间"><a href="# 推迟索引分片的重新分配时间" class="headerlink" title="推迟索引分片的重新分配时间"></a> 推迟索引分片的重新分配时间</h2><p>适用于节点短时间离线再加入集群,提前设置好,避免从分片的复制移动。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">PUT your_index/_settings</span><br><span class="line">{</span><br><span class="line"> "settings": {</span><br><span class="line"> "index.unassigned.node_left.delayed_timeout": "5m"</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="排除掉节点"><a href="# 排除掉节点" class="headerlink" title="排除掉节点"></a>排除掉节点</h2><p>不让索引的分片分配在上面,想取消设置为 <code>null</code> 即可。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"># 索引级别的 </span><br><span class="line">PUT your_index/_settings</span><br><span class="line">{</span><br><span class="line"> "index.routing.allocation.exclude._ip": "ip1,ip2"</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"># 集群级别的,等价于下线节点,滚动重启时需要 </span><br><span class="line">PUT /_cluster/settings/</span><br><span class="line">{</span><br><span class="line"> "transient": {</span><br><span class="line"> "cluster.routing.allocation.exclude._ip": "ip1,ip2"</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"># 集群均衡移动分片的并发数,不能太大 </span><br><span class="line">cluster.routing.allocation.cluster_concurrent_rebalance</span><br></pre></td></tr></table></figure><h2 id="基于负载的智能路由查询"><a href="# 基于负载的智能路由查询" class="headerlink" title="基于负载的智能路由查询"></a>基于负载的智能路由查询</h2><p><code>v6.2</code> 以及以上版本,<code>search</code> 智能路由设置,<code>v7.0</code> 以及以上版本默认开启。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">PUT /_cluster/settings</span><br><span class="line">{</span><br><span class="line"> "transient": {</span><br><span class="line"> "cluster.routing.use_adaptive_replica_selection": true</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="查询全局超时时间"><a href="# 查询全局超时时间" class="headerlink" title="查询全局超时时间"></a> 查询全局超时时间</h2><p><code>search</code> 全局超时时间,避免某些耗时的查询把集群拖垮。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">search.default_search_timeout</span><br><span class="line"></span><br><span class="line"> 示例:5m</span><br></pre></td></tr></table></figure><h2 id="查询时指定分片主机等"><a href="# 查询时指定分片主机等" class="headerlink" title="查询时指定分片主机等"></a> 查询时指定分片主机等</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">preference=_shards:8,12</span><br><span class="line">preference=_only_nodes:1</span><br><span class="line">preference=_primary</span><br><span class="line">preference=_replica</span><br><span class="line"> </span><br><span class="line">POST your_index/_search?preference=_shards:12</span><br><span class="line">{</span><br><span class="line"> "query": {</span><br><span class="line"> "match_phrase": {</span><br><span class="line"> "content": " 查证 & quot;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"># 注意,指定节点之后还要指定分片,否则查询时报错:找不到某个分片对应的节点。</span><br><span class="line"># 示例:</span><br><span class="line">POST your_index_name/_search?preference=_only_nodes:YKA3ZivmSj2K6qutW6KNQQ&preference=_shards:3</span><br><span class="line">{</span><br><span class="line"> "query": {</span><br><span class="line"> "match_all": {</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="分片迁移的并发数带宽流量大小等等"><a href="# 分片迁移的并发数带宽流量大小等等" class="headerlink" title="分片迁移的并发数带宽流量大小等等"></a> 分片迁移的并发数带宽流量大小等等</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"># 并发数 </span><br><span class="line">PUT _cluster/settings</span><br><span class="line">{</span><br><span class="line"> "transient": {</span><br><span class="line"> "cluster.routing.allocation.node_concurrent_outgoing_recoveries": "3",</span><br><span class="line"> "cluster.routing.allocation.node_concurrent_incoming_recoveries": "3",</span><br><span class="line"> "cluster.routing.allocation.node_concurrent_recoveries": 3</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"># 带宽 </span><br><span class="line">PUT _cluster/settings</span><br><span class="line">{</span><br><span class="line"> "transient": {</span><br><span class="line"> "indices.recovery.max_bytes_per_sec": "20mb" </span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="只读索引问题"><a href="# 只读索引问题" class="headerlink" title="只读索引问题"></a> 只读索引问题</h2><p>在集群机器的磁盘快用完之前,集群会自动设置这台机器上面的节点的索引为【只读模式】,不可写入,写入直接拒绝并抛出异常信息。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">20/10/10 19:02:44 ERROR ESBulkProcessor: {"index":"your_index_name","type":"post","id":"12aad31610551fb2e236367bbde01db7","cause":{"type":"exception","reason":"Elasticsearch exception [type=cluster_block_exception, reason=blocked by: [FORBIDDEN/12/index read-only /allow delete (api)];]"},"status":403}</span><br></pre></td></tr></table></figure><p>此时,除了清理数据,还需要手动设置。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">GET your_index_*/_settings</span><br><span class="line"> </span><br><span class="line">PUT your_index_*/_settings</span><br><span class="line">{</span><br><span class="line"> "index": {</span><br><span class="line"> "blocks": {</span><br><span class="line"> "read_only_allow_delete": "false"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"> </span><br><span class="line">PUT your_index_*/_settings</span><br><span class="line">{</span><br><span class="line"> "index": {</span><br><span class="line"> "blocks": {</span><br><span class="line"> "read_only_allow_delete": null</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>参考官方文档:<a href="cluster.routing.allocation.disk.watermark.flood_stage">cluster.routing.allocation.disk.watermark.flood_stage</a> 。</p><h1 id="分析器"><a href="# 分析器" class="headerlink" title="分析器"></a>分析器</h1><p>可以查看不同分析器的分词结果,或者基于某个索引的某个字段查看分词结果。下面列举一些例子,其它更多的内容请读者参考另外一篇博客:<a href="https://www.playpi.org/2017082001.html">Elasticsearch 分析器使用入门指南 </a>。</p><p>查看集群安装的各种分词器效果,指定文本内容、分词器即可:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">POST _analyze</span><br><span class="line">{</span><br><span class="line"> "text":" 行完成,是否成功请查看 ccc",</span><br><span class="line"> "analyzer":"wordsEN"</span><br><span class="line">}</span><br><span class="line"> </span><br><span class="line">POST _analyze</span><br><span class="line">{</span><br><span class="line"> "text":" 行完成,是否成功请查看 ccc",</span><br><span class="line"> "analyzer":"standard"</span><br><span class="line">}</span><br><span class="line"> </span><br><span class="line">POST _analyze</span><br><span class="line">{</span><br><span class="line"> "text":" 行完成,是否成功请查看 ccc",</span><br><span class="line"> "analyzer":"english"</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>查看某个索引的某个字段的分词器效果【索引已经指定分词器,可以通过 <code>mapping</code> 查看】,指定索引名称、文本内容、字段名称,不要指定索引的 <code>type</code>,否则请求变为了新建文档:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">POST my-index-post/_analyze</span><br><span class="line">{</span><br><span class="line"> "text":" 行完成,是否成功请查看 ccc",</span><br><span class="line"> "field":"content"</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>查询时也可以指定分词器【不同分词器会影响返回的结果,例如 <code>standard</code> 分词器会过滤掉标点符号,所以查不到数据】,特别指定分词器即可。另外只能使用 <code>match</code>,不能使用 <code>match_phrase</code>。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">POST my-index-post/post/_search</span><br><span class="line">{</span><br><span class="line"> "query": {</span><br><span class="line"> "match": {</span><br><span class="line"> "content":{</span><br><span class="line"> "query": ",",</span><br><span class="line"> "analyzer": "standard"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="创建索引"><a href="# 创建索引" class="headerlink" title="创建索引"></a>创建索引</h1><p>创建带 <code>mapping</code> 的索引:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">PUT /my-index-post/</span><br><span class="line">{</span><br><span class="line"> "settings": {</span><br><span class="line"> "index.number_of_shards": 3,</span><br><span class="line"> "index.number_of_replicas": 1,</span><br><span class="line"> "index.refresh_interval": "30s",</span><br><span class="line"> "index.routing.allocation.total_shards_per_node": 3</span><br><span class="line"> },</span><br><span class="line"> "mappings": {</span><br><span class="line"> "post": {</span><br><span class="line"> "_all": {</span><br><span class="line"> "enabled": false</span><br><span class="line"> },</span><br><span class="line"> "dynamic_templates": [</span><br><span class="line"> {</span><br><span class="line"> "title1": {</span><br><span class="line"> "match": "title",</span><br><span class="line"> "match_mapping_type": "*",</span><br><span class="line"> "mapping": {</span><br><span class="line"> "type": "text",</span><br><span class="line"> "analyzer": "wordsEN"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>创建带 <code>mapping</code> 的 <code>type</code>【在索引已经存在的情况下】:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">PUT /my-index-post/_mapping/post/</span><br><span class="line">{</span><br><span class="line"> "_all": {</span><br><span class="line"> "enabled": false</span><br><span class="line"> },</span><br><span class="line"> "dynamic_templates": [</span><br><span class="line"> {</span><br><span class="line"> "title1": {</span><br><span class="line"> "mapping": {</span><br><span class="line"> "analyzer": "wordsEN",</span><br><span class="line"> "type": "text"</span><br><span class="line"> },</span><br><span class="line"> "match": "title"</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> {</span><br><span class="line"> "title2": {</span><br><span class="line"> "mapping": {</span><br><span class="line"> "analyzer": "wordsEN",</span><br><span class="line"> "type": "text"</span><br><span class="line"> },</span><br><span class="line"> "match": "*_title"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ],</span><br><span class="line"> "properties": {</span><br><span class="line"> "avatar_url": {</span><br><span class="line"> "type": "keyword"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>更新索引的 <code>mapping</code>【在索引、类型都已经存在的情况下】:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">PUT /my-index-post/_mapping/post</span><br><span class="line">{</span><br><span class="line"> "post": {</span><br><span class="line"> "properties": {</span><br><span class="line"> "title": {</span><br><span class="line"> "type": "text",</span><br><span class="line"> "analyzer": "english",</span><br><span class="line"> "search_analyzer": "standard" </span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="添加删除别名"><a href="# 添加删除别名" class="headerlink" title="添加删除别名"></a>添加删除别名</h1><p>给索引增加别名:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">POST /_aliases</span><br><span class="line">{</span><br><span class="line"> "actions": [</span><br><span class="line"> {</span><br><span class="line"> "add": {</span><br><span class="line"> "index": "my-index-post",</span><br><span class="line"> "alias": "my-index-post-all"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>移除索引的别名:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">POST /_aliases</span><br><span class="line">{</span><br><span class="line"> "actions": [</span><br><span class="line"> {</span><br><span class="line"> "remove": {</span><br><span class="line"> "index": "my-index-post",</span><br><span class="line"> "alias": "my-index-post-all"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="导入数据"><a href="# 导入数据" class="headerlink" title="导入数据"></a>导入数据</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"> 把文件中的数据导入索引,批量的形式 </span><br><span class="line"> 由于数据中可能存在一些特殊符号,所以使用文件的形式,in 为文件路径 </span><br><span class="line"> 文件内容格式,1 条数据需要包含 2 行内容,index 表示索引数据 </span><br><span class="line">{"index":{}}</span><br><span class="line">JSON 原始数据 </span><br><span class="line"></span><br><span class="line">curl -XPOST 'http://localhost:9200/my-index-post/post/_bulk' --data-binary @"$in"</span><br></pre></td></tr></table></figure><p><code>bulk</code> 接口,详情参考另外一篇博客:<a href="https://www.playpi.org/2019101701.html"> 使用 Elasticsearch 的 bulk 接口批量导入数据 </a>。</p><h1 id="查询数据"><a href="# 查询数据" class="headerlink" title="查询数据"></a> 查询数据</h1><h2 id="脚本查询"><a href="# 脚本查询" class="headerlink" title="脚本查询"></a> 脚本查询</h2><p><code>Elasticsearch</code> 提供了脚本的支持,可以通过 <code>Groovy</code> 外置脚本【已经过时,<code>v6.x</code> 以及之后的版本,不建议使用】、内置 <code>painless</code> 脚本实现各种复杂的操作【类似于写逻辑代码,对数据进行 <code>ETL</code> 操作,需要集群配置开启】。</p><p>以下是关于 <code>v2.x</code> 的说明:</p><blockquote><p>默认的脚本语言是 Groovy,一种快速表达的脚本语言,在语法上与 JavaScript 类似。它在 Elasticsearch v1.3.0 版本首次引入并运行在沙盒中,然而 Groovy 脚本引擎存在漏洞,允许攻击者通过构建 Groovy 脚本,在 Elasticsearch Java VM 运行时脱离沙盒并执行 shell 命令。<br>因此,在版本 v1.3.8、1.4.3 和 v1.5.0 及更高的版本中,它已经被默认禁用。此外,您可以通过设置集群中的所有节点的 config/elasticsearch.yml 文件来禁用动态 Groovy 脚本:script.groovy.sandbox.enabled: false,这将关闭 Groovy 沙盒,从而防止动态 Groovy 脚本作为请求的一部分被接受。</p></blockquote><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">Groovy 脚本 </span><br><span class="line">{</span><br><span class="line"> "query": {</span><br><span class="line"> "bool": {</span><br><span class="line"> "filter": {</span><br><span class="line"> "script": {</span><br><span class="line"> "script": "doc ['keywords'].values.length == 2"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">painless 脚本 </span><br><span class="line">{</span><br><span class="line"> "query": {</span><br><span class="line"> "bool": {</span><br><span class="line"> "filter": {</span><br><span class="line"> "script": {</span><br><span class="line"> "script": {</span><br><span class="line"> "source": "doc ['keywords'].values.length == 2",</span><br><span class="line"> "lang": "painless"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">{</span><br><span class="line"> "query": {</span><br><span class="line"> "bool": {</span><br><span class="line"> "must": [</span><br><span class="line"> {</span><br><span class="line"> "range": {</span><br><span class="line"> "update_timestamp": {</span><br><span class="line"> "gte": 1607670109000</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> {</span><br><span class="line"> "script": {</span><br><span class="line"> "script": {</span><br><span class="line"> "source": "(doc ['view_cnt'].value)>doc ['comment_cnt'].value",</span><br><span class="line"> "lang": "painless"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="日期桶聚合"><a href="# 日期桶聚合" class="headerlink" title="日期桶聚合"></a>日期桶聚合</h2><p>对日期格式的字段做桶聚合,可以使用 <code>interval</code> 设置桶间隔,使用 <code>extended_bounds</code> 设置桶边界,其它还可以设置时区、<code>doc</code> 过滤等。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">"aggs": {</span><br><span class="line"> "by_month": {</span><br><span class="line"> "date_histogram": {</span><br><span class="line"> "field": "publish_timestamp",</span><br><span class="line"> "interval": "day",</span><br><span class="line"> "time_zone": "+08:00",</span><br><span class="line"> "format": "yyyy-MM-dd",</span><br><span class="line"> "min_doc_count": 100000,</span><br><span class="line"> "extended_bounds": {</span><br><span class="line"> "min": "2019-08-30",</span><br><span class="line"> "max": "2019-09-24"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></table></figure><p>对于聚合结果不准的问题,可以增加参数,适当提高准确性。<code>size</code> 参数规定了最后返回的 <code>term</code> 个数【默认是 10 个】,<code>shard_size</code> 参数规定了每个分片上返回的个数【默认是 <code>size * 1.5 + 10</code>】,如果 <code>shard_size</code> 小于 <code>size</code>,那么分片也会按照 <code>size</code> 指定的个数计算。</p><p>聚合的字段可能存在一些频率很低的词条,如果这些词条数目比例很大,那么就会造成很多不必要的计算。因此可以通过设置 <code>min_doc_count</code> 和 <code>shard_min_doc_count</code> 来规定最小的文档数目,只有满足这个参数要求的个数的词条才会被记录返回。<code>min_doc_count</code>:规定了最终结果的筛选,<code>shard_min_doc_count</code>:规定了分片中计算返回时的筛选。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">"aggs": {</span><br><span class="line"> "aggs_sentiment":{</span><br><span class="line"> "terms": {</span><br><span class="line"> "field": "sentiment",</span><br><span class="line"> "size": 10,</span><br><span class="line"> "shard_size": 30,</span><br><span class="line"> "min_doc_count": 10000,</span><br><span class="line"> "shard_min_doc_count": 50</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="更新文档"><a href="# 更新文档" class="headerlink" title="更新文档"></a>更新文档</h2><p>指定部分字段进行更新,不影响其它字段【但是要注意,如果字段只是索引 <code>index</code> 而没有存储 <code>_source</code>,更新后会无法查询这个字段】。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">POST /my-index-user/user/0f42d65be1f5287e1c9c26e3728814aa/_update</span><br><span class="line">{</span><br><span class="line"> "doc" : {</span><br><span class="line"> "friends" : ["98681482902","63639783663","59956667929"]</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="自动缓存相关"><a href="# 自动缓存相关" class="headerlink" title="自动缓存相关"></a>自动缓存相关</h2><p><code>terms lookup</code> 查询:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"> 自动缓存 </span><br><span class="line">POST my-index-post/_search</span><br><span class="line">{</span><br><span class="line"> "query": {</span><br><span class="line"> "terms": {</span><br><span class="line"> "user_item_id":{</span><br><span class="line"> "index": "my-index-user",</span><br><span class="line"> "type": "user",</span><br><span class="line"> "id": "0f42d65be1f5287e1c9c26e3728814aa",</span><br><span class="line"> "path": "friends"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>操作缓存的接口:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"> 关闭缓存 </span><br><span class="line">curl -XPOST 'localhost:9200/_cache/clear?filter_path=your_cache_key'</span><br></pre></td></tr></table></figure><h2 id="多层嵌套反转桶聚合"><a href="# 多层嵌套反转桶聚合" class="headerlink" title="多层嵌套反转桶聚合"></a>多层嵌套反转桶聚合</h2><p>多层聚合查询,关于嵌套、反转,参考:<a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/nested-aggregation.html" target="_blank" rel="noopener">nested-aggregation</a> 。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">POST combine-paas-1003-index/2723-data/_search</span><br><span class="line">{</span><br><span class="line"> "aggs": {</span><br><span class="line"> "x": {</span><br><span class="line"> "aggs": {</span><br><span class="line"> "xx": {</span><br><span class="line"> "aggs": {</span><br><span class="line"> "xxx": {</span><br><span class="line"> "aggs": {</span><br><span class="line"> "xxxx_interaction_cnt": {</span><br><span class="line"> "sum": {</span><br><span class="line"> "field": "2723_interaction_cnt"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> "reverse_nested": {}</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> "terms": {</span><br><span class="line"> "field": "Titan_sports.yundongerji",</span><br><span class="line"> "size": 100</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> "nested": {</span><br><span class="line"> "path": "Titan_sports"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> "query": {</span><br><span class="line"> "bool": {</span><br><span class="line"> "must": [</span><br><span class="line"> {</span><br><span class="line"> "term": {</span><br><span class="line"> "2723_is_noise": {</span><br><span class="line"> "value": " 否 & quot;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> "size": 1</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="统计个数聚合"><a href="# 统计个数聚合" class="headerlink" title="统计个数聚合"></a>统计个数聚合</h2><p>对于多篇文章,统计每个站点下面的作者个数:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">-- 多层嵌套以及特殊的聚合,每个 site_name 下面的作者个数统计 </span><br><span class="line">{</span><br><span class="line"> "aggs": {</span><br><span class="line"> "s": {</span><br><span class="line"> "aggs": {</span><br><span class="line"> "a": {</span><br><span class="line"> "cardinality": {</span><br><span class="line"> "field": "author"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> "terms": {</span><br><span class="line"> "field": "site_name",</span><br><span class="line"> "size": 0</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> "query": {},</span><br><span class="line"> "size": 0</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="存在查询"><a href="# 存在查询" class="headerlink" title="存在查询"></a>存在查询</h2><p><code>exists</code>、<code>missing</code> 这两类查询在不同的版本之间使用方式不一致。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">-- 存在、不存在判断条件,1.7.5 版本和 2.3.4 版本的方式不一样 </span><br><span class="line">-- 2.3.4:使用 exists、missing 关键字即可 </span><br><span class="line">{</span><br><span class="line"> "query": {</span><br><span class="line"> "exists": {</span><br><span class="line"> "field": "gender"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">{</span><br><span class="line"> "query": {</span><br><span class="line"> "missing": {</span><br><span class="line"> "field": "gender"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">-- 更高版本【v5.x 以及以上】的 ES 关键字 missing 已经被废弃,改为 must_not 和 exists 组合查询,以下有示例 </span><br><span class="line">{</span><br><span class="line"> "query": {</span><br><span class="line"> "bool": {</span><br><span class="line"> "must_not": {</span><br><span class="line"> "exists": {</span><br><span class="line"> "field": "user"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"> </span><br><span class="line">-- 1.7.5:使用 filter 后再使用对应关键词,本质是一种过滤器 </span><br><span class="line">{</span><br><span class="line"> "query": {</span><br><span class="line"> "filtered": {</span><br><span class="line"> "filter": {</span><br><span class="line"> "exists": {</span><br><span class="line"> "field": "data_type"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">-- 此外,不同版本连接 ES 的 client 方式也不一样【tcp 连接,如果是 http 连接就不会有问题】,代码不能兼容,所以只能使用其中 1 种方式【在本博客中可以搜索到相关总结】</span><br></pre></td></tr></table></figure><h2 id="随机取数"><a href="# 随机取数" class="headerlink" title="随机取数"></a> 随机取数</h2><p>随机取数【需要 <code>ES</code> 集群支持脚本请求】</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">GET your_index_name/_search</span><br><span class="line">{</span><br><span class="line"> "query": {</span><br><span class="line"> "match_all": {}</span><br><span class="line"> },</span><br><span class="line"> "sort": {</span><br><span class="line"> "_script": {</span><br><span class="line"> "script": "Math.random ()",</span><br><span class="line"> "type": "number",</span><br><span class="line"> "order": "desc"</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> "size": 20</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>如果 <code>ES</code> 集群不支持脚本请求,会抛出异常:<code>illegal_argument_exception</code>,原因:<code>cannot execute [inline] scripts</code>。</p><h1 id="删除数据"><a href="# 删除数据" class="headerlink" title="删除数据"></a>删除数据</h1><p>根据查询条件删除数据:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">POST my-index-post/post/_delete_by_query/</span><br><span class="line">{</span><br><span class="line"> "query": {</span><br><span class="line"> "terms": {</span><br><span class="line"> "id": [</span><br><span class="line"> "1",</span><br><span class="line"> "2"</span><br><span class="line"> ]</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>当然,如果是低版本的 <code>Elasticsearch</code>,在 <code>1.x</code> 的版本中还可以使用发送 <code>DELETE</code> 请求的方式删除数据,容易引发一些操作失误,不建议使用。</p><p>更多内容参考:<a href="https://www.playpi.org/2018022401.html">Elasticsearch 根据查询条件删除数据的 API</a> 。</p><h1 id="索引关闭开启"><a href="# 索引关闭开启" class="headerlink" title="索引关闭开启"></a>索引关闭开启</h1><p>主要有两个接口:</p><ul><li>开启索引,<code>curl -XPOST http://localhost:9200/your_index/_open</code></li><li>关闭索引,<code>curl -XPOST http://localhost:9200/your_index/_close</code></li></ul><p>参考这篇博客的部分内容:<a href="https://www.playpi.org/2019082101.html">使用 http 接口删除 Elasticsearch 集群的索引 </a>。</p><h1 id="迁移数据"><a href="# 迁移数据" class="headerlink" title="迁移数据"></a> 迁移数据</h1><p>迁移一个索引的数据到另外一个索引,切记需要提前创建好索引,包含 <code>mapping</code>,避免字段类型出问题:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">POST _reindex</span><br><span class="line">{</span><br><span class="line"> "source": {</span><br><span class="line"> "index": "my-index-post",</span><br><span class="line"> "type": "post"</span><br><span class="line"> },</span><br><span class="line"> "dest": {</span><br><span class="line"> "index": "my-index-post-bak",</span><br><span class="line"> "type": "post"</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">size 参数在最外层表示随机抽取 n 条测试;</span><br><span class="line">size 参数在 source 里面表示 batch 大小,默认 1000;</span><br><span class="line"> </span><br><span class="line"> 参数 wait_for_completion=false 可以让任务在后台一直运行到完成,否则当数据量大的时候,执行时间过长,会超时退出。</span><br><span class="line"></span><br><span class="line"> 查看任务状态,取消任务 </span><br><span class="line"></span><br><span class="line">GET _tasks?detailed=true&actions=*reindex</span><br><span class="line"></span><br><span class="line">POST _tasks/task_id:1/_cancel</span><br><span class="line"></span><br><span class="line">POST _tasks/_cancel?nodes=nodexx&actions=*search*</span><br></pre></td></tr></table></figure><p>此外,参考:<a href="https://www.playpi.org/2020011601.html">Elasticsearch 的 Reindex API 详解 </a>,里面包含了常见的参数使用方式,以及查看迁移任务进度、取消迁移任务的方式。</p><h1 id="移动分片"><a href="# 移动分片" class="headerlink" title="移动分片"></a> 移动分片</h1><p>需要先关闭 <code>rebalance</code>,再手动移动分片,否则由于手动迁移分片造成集群进行分片的重新分配,进而消耗 <code>IO</code>、<code>CPU</code> 资源。手动迁移分片完成之后,再打开 <code>rebalance</code>,让集群自行进行重新分配管理。</p><p>临时参数设置:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"> 关闭 </span><br><span class="line">curl -XPUT 'localhost:9200/_cluster/settings' -d</span><br><span class="line">'{</span><br><span class="line"> "transient": {</span><br><span class="line"> "cluster.routing.allocation.enable": "none"</span><br><span class="line"> }</span><br><span class="line">}'</span><br><span class="line"></span><br><span class="line"> 打开 </span><br><span class="line">curl -XPUT 'localhost:9200/_cluster/settings' -d</span><br><span class="line">'{</span><br><span class="line"> "transient": {</span><br><span class="line"> "cluster.routing.allocation.enable": "all"</span><br><span class="line"> }</span><br><span class="line">}'</span><br></pre></td></tr></table></figure><p>分片的迁移使用:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">move:移动分片 </span><br><span class="line">cancel:取消分片 </span><br><span class="line">allocate:重新分配分片 </span><br><span class="line"></span><br><span class="line">curl -XPOST 'localhost:9200/_cluster/reroute' -d '{</span><br><span class="line"> "commands" : [{</span><br><span class="line"> "move" :</span><br><span class="line"> {</span><br><span class="line"> "index" : "test", "shard" : 0,</span><br><span class="line"> "from_node" : "node1", "to_node" : "node2"</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> "cancel" :</span><br><span class="line"> {</span><br><span class="line"> "index" : "test", "shard" : 0, "node" : "node1"</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> {</span><br><span class="line"> "allocate" : {</span><br><span class="line"> "index" : "test", "shard" : 1, "node" : "node3"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line">}'</span><br><span class="line"></span><br><span class="line"> 将分配失败的分片重新分配 </span><br><span class="line">curl -XGET 'localhost:9200/_cluster/reroute?retry_failed=true'</span><br><span class="line"></span><br><span class="line"> 用命令手动分配分片,接受丢数据(ES 集群升级前关闭了 your_index 索引,升级后,把副本数设置为 0,打开有 20 个分片无法分配,集群保持红色。关闭也无效,只好接受丢数据恢复空分片)。</span><br><span class="line">{</span><br><span class="line"> "commands": [</span><br><span class="line"> {</span><br><span class="line"> "allocate_empty_primary": {</span><br><span class="line"> "index": "your_index",</span><br><span class="line"> "shard": 17,</span><br><span class="line"> "node": "nodexx",</span><br><span class="line"> "accept_data_loss": true</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>注意,<code>allocate</code> 命令还有一个参数,<code>"allow_primary" : true</code>,即允许该分片做主分片,但是这样可能会造成数据丢失【在不断写入数据的时候】,因此要慎用【如果数据在分配过程中是静态的则可以考虑使用】。</p><p>当然,手动操作需要在熟悉集群的 <code>API</code> 使用的情况下,例如需要获取节点、索引、分片的信息,不然的话不知道参数怎么填写、分片怎么迁移。此时可以使用 <code>Head</code>、<code>kopf</code>、<code>Cerebro</code> 等可视化工具进行查看,比较适合运维人员,而且,分片的迁移指挥工作也可以交给这些工具,只要通过鼠标点击就可以完成分片的迁移,很方便。</p><h1 id="验证"><a href="# 验证" class="headerlink" title="验证"></a>验证</h1><p>检验查询语句的合法性,不仅仅是满足 <code>JSON</code> 格式那么简单:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">POST /my-index-post/_validate/query?explain</span><br><span class="line">{</span><br><span class="line"> "query": {</span><br><span class="line"> "match": {</span><br><span class="line"> "content":{</span><br><span class="line"> "query": ",",</span><br><span class="line"> "analyzer": "wordsEN"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>检查分片分配的相关信息:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"> 不带任何参数执行该命令,会输出当前所有未分配分片的失败原因 </span><br><span class="line">curl -XGET 'localhost:9200/_cluster/allocation/explain</span><br><span class="line"></span><br><span class="line"> 该命令可查看指定分片当前所在节点以及分配到该节点的理由,和未分配到其他节点的原因 </span><br><span class="line">curl -XPOST 'localhost:9200/_cluster/reroute' -d '{</span><br><span class="line"> "index": < 索引名 & gt;,</span><br><span class="line"> "shard": < 分片号 & gt;,</span><br><span class="line"> "primary": true/false</span><br><span class="line">}'</span><br><span class="line"></span><br><span class="line"> 将分配失败的分片重新进行分配 </span><br><span class="line">POST _cluster/reroute?retry_failed=true</span><br><span class="line"></span><br><span class="line"> 移动分片 </span><br><span class="line">POST /_cluster/reroute/</span><br><span class="line">{</span><br><span class="line"> "commands": [</span><br><span class="line"> {</span><br><span class="line"> "move": {</span><br><span class="line"> "index": "your_index_name",</span><br><span class="line"> "shard": 0,</span><br><span class="line"> "from_node": "dev5",</span><br><span class="line"> "to_node": "dev6"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"> 用命令手动分配分片,接受丢数据【原因:ES 集群升级前关闭了某个索引,升级后,把副本数设置为 0,打开有 20 个分片无法分配,集群保持红色。关闭也无效,只好接受丢数据恢复空分片】。</span><br><span class="line">{</span><br><span class="line"> "commands": [</span><br><span class="line"> {</span><br><span class="line"> "allocate_empty_primary": {</span><br><span class="line"> "index": "your_index_name",</span><br><span class="line"> "shard": 17,</span><br><span class="line"> "node": "node1",</span><br><span class="line"> "accept_data_loss": true</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line">}</span><br></pre></td></tr></table></figure></div><div><div id="wechat_subscriber" style="display:block;padding:10px 0;margin:20px auto;width:100%;text-align:center"><img id="wechat_subscriber_qcode" src="/images/wechat-qr-personal.jpg" alt="虾丸派 wechat" style="width:200px;max-width:100%"><div>扫一扫添加博主,进技术交流群,共同学习进步</div></div></div><div><div style="padding:10px 0;margin:20px auto;width:90%;text-align:center"><div>永不止步</div><button id="rewardButton" disable="enable" onclick='var qr=document.getElementById("QR");"none"===qr.style.display?qr.style.display="block":qr.style.display="none"'><span>打赏</span></button><div id="QR" style="display:none"><div id="wechat" style="display:inline-block"><img id="wechat_qr" src="/images/wechat-pay-playpi.png" alt="虾丸派 微信支付"><p>微信支付</p></div></div></div></div><div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者:</strong> 虾丸派</li><li class="post-copyright-link"><strong>本文链接:</strong> <a href="https://www.playpi.org/2018051401.html" title="Elasticsearch 常用 HTTP 接口">https://www.playpi.org/2018051401.html</a></li><li class="post-copyright-license"><strong>版权声明: </strong>本博客所有文章除特别声明外,均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处!</li></ul></div><footer class="post-footer"><div class="post-tags"><a href="/tags/Elasticsearch/" rel="tag"><i class="fa fa-tag"></i> Elasticsearch</a> <a href="/tags/HTTP/" rel="tag"><i class="fa fa-tag"></i> HTTP</a> <a href="/tags/RESTful/" rel="tag"><i class="fa fa-tag"></i> RESTful</a></div><div class="post-nav"><div class="post-nav-next post-nav-item"><a href="/2018041801.html" rel="next" title="es-hadoop 遇上 Elasticsearch 的 Date 类型字段"><i class="fa fa-chevron-left"></i> es-hadoop 遇上 Elasticsearch 的 Date 类型字段</a></div><span class="post-nav-divider"></span><div class="post-nav-prev post-nav-item"><a href="/2018052901.html" rel="prev" title="es-hadoop 版本不匹配导致 discoverNodes 异常">es-hadoop 版本不匹配导致 discoverNodes 异常 <i class="fa fa-chevron-right"></i></a></div></div></footer></div></article><div class="post-spread"></div></div></div><div class="comments" id="comments"><div id="vcomments"></div></div></div><div class="sidebar-toggle"><div class="sidebar-toggle-line-wrap"><span class="sidebar-toggle-line sidebar-toggle-line-first"></span> <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span> <span class="sidebar-toggle-line sidebar-toggle-line-last"></span></div></div><aside id="sidebar" class="sidebar"><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">文章目录</li><li class="sidebar-nav-overview" data-target="site-overview-wrap">站点概览</li></ul><section class="site-overview-wrap sidebar-panel"><div class="site-overview"><div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="site-author-image" itemprop="image" src="/images/favicon-1536x1536-playpi.png" alt="虾丸派"><p class="site-author-name" itemprop="name">虾丸派</p><p class="site-description motion-element" itemprop="description">记录知识 | 分享技术</p></div><nav class="site-state motion-element"><div class="site-state-item site-state-posts"><a href="/archives/"><span class="site-state-item-count">144</span> <span class="site-state-item-name">日志</span></a></div><div class="site-state-item site-state-categories"><a href="/categories/index.html"><span class="site-state-item-count">13</span> <span class="site-state-item-name">分类</span></a></div><div class="site-state-item site-state-tags"><a href="/tags/index.html"><span class="site-state-item-count">294</span> <span class="site-state-item-name">标签</span></a></div></nav><div class="feed-link motion-element"><a href="/atom.xml" rel="alternate"><i class="fa fa-rss"></i> RSS</a></div><div class="links-of-author motion-element"><span class="links-of-author-item"><a href="https://github.com/iplaypi" target="_blank" title="GitHub"><i class="fa fa-fw fa-github"></i>GitHub</a> </span><span class="links-of-author-item"><a href="https://weibo.com/u/3086148515" target="_blank" title="微博"><i class="fa fa-fw fa-weibo"></i>微博</a> </span><span class="links-of-author-item"><a href="mailto:playpi@qq.com" target="_blank" title="E-Mail"><i class="fa fa-fw fa-envelope"></i>E-Mail</a></span></div><div class="cc-license motion-element" itemprop="license"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" target="_blank" rel="external nofollow"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a></div><div class="links-of-blogroll motion-element links-of-blogroll-inline"><div class="links-of-blogroll-title"><i class="fa fa-fw fa-link"></i> 友情链接</div><ul class="links-of-blogroll-list"><li class="links-of-blogroll-item"><a href="https://github.com/iplaypi" title="GitHub" target="_blank" rel="external nofollow">GitHub</a></li><li class="links-of-blogroll-item"><a href="https://weibo.com/u/3086148515" title="Weibo" target="_blank" rel="external nofollow">Weibo</a></li><li class="links-of-blogroll-item"><a href="https://www.playpi.org" title="虾丸派" target="_blank" rel="external nofollow">虾丸派</a></li><li class="links-of-blogroll-item"><a href="https://www.playpi.org" title="playpi" target="_blank" rel="external nofollow">playpi</a></li><li class="links-of-blogroll-item"><a href="https://www.liaoxuefeng.com" title="廖雪峰" target="_blank" rel="external nofollow">廖雪峰</a></li><li class="links-of-blogroll-item"><a href="http://www.ruanyifeng.com" title="阮一峰" target="_blank" rel="external nofollow">阮一峰</a></li><li class="links-of-blogroll-item"><a href="https://travis-ci.org/iplaypi/iplaypi.github.io" title="travis-ci" target="_blank" rel="external nofollow">travis-ci</a></li><li class="links-of-blogroll-item"><a href="https://www.vultr.com/?ref=7861302-4F" title="Vultr" target="_blank" rel="external nofollow">Vultr</a></li></ul></div></div></section><section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active"><div class="post-toc"><div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#集群状态"><span class="nav-number">1.</span> <span class="nav-text">集群状态</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#集群信息"><span class="nav-number">1.1.</span> <span class="nav-text">集群信息</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#健康状况"><span class="nav-number">1.2.</span> <span class="nav-text">健康状况</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#空间使用"><span class="nav-number">1.3.</span> <span class="nav-text">空间使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#分片分布"><span class="nav-number">1.4.</span> <span class="nav-text">分片分布</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#索引状态"><span class="nav-number">1.5.</span> <span class="nav-text">索引状态</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#集群配置信息"><span class="nav-number">1.6.</span> <span class="nav-text">集群配置信息</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#热点线程"><span class="nav-number">1.7.</span> <span class="nav-text">热点线程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#请求队列"><span class="nav-number">1.8.</span> <span class="nav-text">请求队列</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#节点配置信息"><span class="nav-number">1.9.</span> <span class="nav-text">节点配置信息</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用堆内存大小"><span class="nav-number">1.10.</span> <span class="nav-text">使用堆内存大小</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#清理缓存"><span class="nav-number">1.11.</span> <span class="nav-text">清理缓存</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#推迟索引分片的重新分配时间"><span class="nav-number">1.12.</span> <span class="nav-text">推迟索引分片的重新分配时间</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#排除掉节点"><span class="nav-number">1.13.</span> <span class="nav-text">排除掉节点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#基于负载的智能路由查询"><span class="nav-number">1.14.</span> <span class="nav-text">基于负载的智能路由查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#查询全局超时时间"><span class="nav-number">1.15.</span> <span class="nav-text">查询全局超时时间</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#查询时指定分片主机等"><span class="nav-number">1.16.</span> <span class="nav-text">查询时指定分片主机等</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#分片迁移的并发数带宽流量大小等等"><span class="nav-number">1.17.</span> <span class="nav-text">分片迁移的并发数带宽流量大小等等</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#只读索引问题"><span class="nav-number">1.18.</span> <span class="nav-text">只读索引问题</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#分析器"><span class="nav-number">2.</span> <span class="nav-text">分析器</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#创建索引"><span class="nav-number">3.</span> <span class="nav-text">创建索引</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#添加删除别名"><span class="nav-number">4.</span> <span class="nav-text">添加删除别名</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#导入数据"><span class="nav-number">5.</span> <span class="nav-text">导入数据</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#查询数据"><span class="nav-number">6.</span> <span class="nav-text">查询数据</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#脚本查询"><span class="nav-number">6.1.</span> <span class="nav-text">脚本查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#日期桶聚合"><span class="nav-number">6.2.</span> <span class="nav-text">日期桶聚合</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#更新文档"><span class="nav-number">6.3.</span> <span class="nav-text">更新文档</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#自动缓存相关"><span class="nav-number">6.4.</span> <span class="nav-text">自动缓存相关</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#多层嵌套反转桶聚合"><span class="nav-number">6.5.</span> <span class="nav-text">多层嵌套反转桶聚合</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#统计个数聚合"><span class="nav-number">6.6.</span> <span class="nav-text">统计个数聚合</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#存在查询"><span class="nav-number">6.7.</span> <span class="nav-text">存在查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#随机取数"><span class="nav-number">6.8.</span> <span class="nav-text">随机取数</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#删除数据"><span class="nav-number">7.</span> <span class="nav-text">删除数据</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#索引关闭开启"><span class="nav-number">8.</span> <span class="nav-text">索引关闭开启</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#迁移数据"><span class="nav-number">9.</span> <span class="nav-text">迁移数据</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#移动分片"><span class="nav-number">10.</span> <span class="nav-text">移动分片</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#验证"><span class="nav-number">11.</span> <span class="nav-text">验证</span></a></li></ol></div></div></section></div></aside></div></main><footer id="footer" class="footer"><div class="footer-inner"><div class="copyright">© 2016–<span itemprop="copyrightYear">2021</span> <span class="post-meta-divider">|</span> <span class="with-love"><i class="fa fa-heart"></i> </span><span class="author" itemprop="copyrightHolder">虾丸派</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-area-chart"></i> </span><span class="post-meta-item-text">全站字数统计</span> <span title="全站字数统计">326.3k 字</span></div><div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io" rel="external nofollow">Hexo</a> 强力驱动</div><span class="post-meta-divider">|</span><div class="theme-info">主题 <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next" rel="external nofollow">NexT.Mist</a><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_site_pv" style="display:none"><span class="post-meta-divider">|</span> 总访问量 <span id="busuanzi_value_site_pv"></span> 次 </span><span id="busuanzi_container_site_uv" style="display:none"><span class="post-meta-divider">|</span> 总访客 <span id="busuanzi_value_site_uv"></span> 人</span></div><div class="busuanzi-count"><script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></div></footer><div class="back-to-top"><i class="fa fa-arrow-up"></i> <span id="scrollpercent"><span>0</span>%</span></div></div><script type="text/javascript">"[object Function]"!==Object.prototype.toString.call(window.Promise)&&(window.Promise=null)</script><script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script><script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script><script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script><script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script><script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script><script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script><script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script><script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script><script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script><script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script><script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script><script src="//unpkg.com/valine@1.3.7/dist/Valine.min.js"></script><script type="text/javascript">new Valine({av:AV,el:"#comments",verify:!1,notify:!1,app_id:"FC5Jijeg1meo2K2OzPYWK327-gzGzoHsz",app_key:"6A1ReY8tjhPutK00F01YbJSq",placeholder:"没有问题吗?"})</script><script type="text/javascript">var isfetched=!1,isXml=!0,search_path="search.xml";0===search_path.length?search_path="search.xml":/json$/i.test(search_path)&&(isXml=!1);var path="/"+search_path,onPopupClose=function(t){$(".popup").hide(),$("#local-search-input").val(""),$(".search-result-list").remove(),$("#no-result").remove(),$(".local-search-pop-overlay").remove(),$("body").css("overflow","")};function proceedsearch(){$("body").append('<div class="search-popup-overlay local-search-pop-overlay"></div>').css("overflow","hidden"),$(".search-popup-overlay").click(onPopupClose),$(".popup").toggle();var t=$("#local-search-input");t.attr("autocapitalize","none"),t.attr("autocorrect","off"),t.focus()}var searchFunc=function(t,e,s){"use strict";$("body").append('<div class="search-popup-overlay local-search-pop-overlay"><div id="search-loading-icon"><i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i></div></div>').css("overflow","hidden"),$("#search-loading-icon").css("margin","20% auto 0 auto").css("text-align","center"),$.ajax({url:t,dataType:isXml?"xml":"json",async:!0,success:function(t){isfetched=!0,$(".popup").detach().appendTo(".header-inner");var o=isXml?$("entry",t).map(function(){return{title:$("title",this).text(),content:$("content",this).text(),url:$("url",this).text()}}).get():t,n=document.getElementById(e),r=document.getElementById(s),t=function(){var m=n.value.trim().toLowerCase(),x=m.split(/[\s\-]+/);1<x.length&&x.push(m);var e,w=[];0<m.length&&o.forEach(function(t){var e=!1,o=0,h=0,n=t.title.trim(),r=n.toLowerCase(),s=t.content.trim().replace(/<[^>]+>/g,""),a=s.toLowerCase(),i=decodeURIComponent(t.url),c=[],l=[];if(""!=n&&(x.forEach(function(t){function e(t,e,o){var n=t.length;if(0===n)return[];var r,s=0,a=[];for(o||(e=e.toLowerCase(),t=t.toLowerCase());-1<(r=e.indexOf(t,s));)a.push({position:r,word:t}),s=r+n;return a}c=c.concat(e(t,r,!1)),l=l.concat(e(t,a,!1))}),(0<c.length||0<l.length)&&(e=!0,o=c.length+l.length)),e){function p(t,e,o,n){for(var r=n[n.length-1],s=r.position,a=r.word,i=[],c=0;s+a.length<=o&&0!=n.length;){a===m&&c++,i.push({position:s,length:a.length});var l=s+a.length;for(n.pop();0!=n.length&&(s=(r=n[n.length-1]).position,a=r.word,s<l);)n.pop()}return h+=c,{hits:i,start:e,end:o,searchTextCount:c}}[c,l].forEach(function(t){t.sort(function(t,e){return e.position!==t.position?e.position-t.position:t.word.length-e.word.length})});t=[];0!=c.length&&t.push(p(0,0,n.length,c));for(var u=[];0!=l.length;){var f=l[l.length-1],d=f.position,g=f.word,v=d-20,f=d+80;v<0&&(v=0),(f=f<d+g.length?d+g.length:f)>s.length&&(f=s.length),u.push(p(0,v,f,l))}u.sort(function(t,e){return t.searchTextCount!==e.searchTextCount?e.searchTextCount-t.searchTextCount:t.hits.length!==e.hits.length?e.hits.length-t.hits.length:t.start-e.start});e=parseInt("1");function $(o,t){var n="",r=t.start;return t.hits.forEach(function(t){n+=o.substring(r,t.position);var e=t.position+t.length;n+='<b class="search-keyword">'+o.substring(t.position,e)+"</b>",r=e}),n+=o.substring(r,t.end)}0<=e&&(u=u.slice(0,e));var C="";0!=t.length?C+="<li><a href='"+i+"' class='search-result-title'>"+$(n,t[0])+"</a>":C+="<li><a href='"+i+"' class='search-result-title'>"+n+"</a>",u.forEach(function(t){C+="<a href='"+i+'\'><p class="search-result">'+$(s,t)+"...</p></a>"}),C+="</li>",w.push({item:C,searchTextCount:h,hitCount:o,id:w.length})}}),1===x.length&&""===x[0]?r.innerHTML='<div id="no-result"><i class="fa fa-search fa-5x" /></div>':0===w.length?r.innerHTML='<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>':(w.sort(function(t,e){return t.searchTextCount!==e.searchTextCount?e.searchTextCount-t.searchTextCount:t.hitCount!==e.hitCount?e.hitCount-t.hitCount:e.id-t.id}),e='<ul class="search-result-list">',w.forEach(function(t){e+=t.item}),e+="</ul>",r.innerHTML=e)};n.addEventListener("input",t),$(".local-search-pop-overlay").remove(),$("body").css("overflow",""),proceedsearch()}})};$(".popup-trigger").click(function(t){t.stopPropagation(),!1===isfetched?searchFunc(path,"local-search-input","local-search-result"):proceedsearch()}),$(".popup-btn-close").click(onPopupClose),$(".popup").click(function(t){t.stopPropagation()}),$(document).on("keyup",function(t){27===t.which&&$(".search-popup").is(":visible")&&onPopupClose()})</script><script>!function(){var t=document.createElement("script"),e=window.location.protocol.split(":")[0];t.src="https"===e?"https://zz.bdstatic.com/linksubmit/push.js":"http://push.zhanzhang.baidu.com/push.js";e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e)}()</script><script type="text/javascript" src="/js/src/js.cookie.js?v=5.1.3"></script><script type="text/javascript" src="/js/src/scroll-cookie.js?v=5.1.3"></script><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({pluginRootPath:"live2dw/",pluginJsPath:"lib/",pluginModelPath:"assets/",tagMode:!1,debug:!1,model:{scale:1,jsonPath:"/live2dw/assets/hijiki.model.json"},display:{position:"left",width:100,height:200,hOffset:0,vOffset:-20},mobile:{show:!1,motion:!0,scale:.3},log:!1})</script></body></html>