总结:ElasticSearch

一、ElasticSearch是什么

• 搜索引擎 : 一切设计都是为了提高搜索的性能

• 分布式,高可用,易扩展

• Lucence :最 先进、性能 最好、 功能最全的搜索引擎库

二、什么是全文检索

全文检索是指计算机索引程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序就根据事先建立的索引进行查找,并将查找的结果反馈给用户的检索方式。这个过程类似于通过字典中的检索字表查字的过程。

根据Apache Lucene - Apache Lucene Core定义:

Lucene是一个高效的,基于Java的全文检索库。

所以在了解Lucene之前要费一番工夫了解一下全文检索。

那么什么叫做全文检索呢?这要从我们生活中的数据说起。

我们生活中的数据总体分为两种:结构化数据非结构化数据

  • 结构化数据:指具有固定格式或有限长度的数据,如数据库,元数据等。
  • 非结构化数据:指不定长或无固定格式的数据,如邮件,word文档等。

当然有的地方还会提到第三种,半结构化数据,如XML,HTML等,当根据需要可按结构化数据来处理,也可抽取出纯文本按非结构化数据来处理。

非结构化数据又一种叫法叫全文数据。

三、核心概念

四、行存储?列存储?

参考链接:es索引优化(行存储、列存储、索引)_genghaihua的博客-CSDN博客_es 列式存储

es的底层存储使用lucene,主要包含行存储(storefiled),列存储(docvalues)和倒排索引(invertindex)。

大多数使用场景中,没有必要同时存储这三个部分,可以通过下面的参数来做适当调整

1 mapping type index 设置

 "_source": {
        "enabled": false
      }
       StoreFiled: 行存,其中占比最大的是_source字段,它控制doc原始数据的存储。在写入数据时,ES把doc原始数据的整个json结构体当做一个string,存储为_source字段。查询时,可以通过_source字段拿到当初写入时的整个json结构体。 所以,如果没有取出整个原始json结构体的需求,可以通过下面的命令,在mapping中关闭_source字段或者只在_source中存储部分字段,数据查询时仍可通过ES的docvalue_fields获取所有字段的值。

       注意:关闭_source后, update, update_by_query, reindex等接口将无法正常使用,所以有update等需求的index不能关闭_source。

2 字段doc_values设置

"doc_values": false
    控制列存。ES主要使用列存来支持sorting, aggregations和scripts功能。

3 字段索引设置

 "index": false
   控制倒排索引。ES默认对于所有字段都开启了倒排索引,用于查询。

五、什么是DSL ​

DSL (domain-specific language),领域特定语言指的是专注于某个应用程序领域的计算机语言,又译作领域专用语言。不同于普通的跨领域通用计算机语言(GPL),领域特定语言只用在某些特定的领域。

六、倒排索引

正排索引:

倒排索引:

七、物理设计:Shard -- > Segment -- > block

Shard(分片) 

为了支持对海量数据的存储和查询,Elasticsearch引入分片的概念,一个索引被分成多个分片(一个Shard就是一个Lucene实例,是一个完整的搜索引擎),每个分片可以有一个主分片和多个副本分片,每个分片副本都是一个具有完整功能的lucene实例。分片可以分配在不同的服务器上,同一个分片的不同副本不能分配在相同的服务器上。

segment 

     segment的设计主要是为了查询的时候可以并发查询,提高查询效率。
     elasticsearch中的每个Shard包含多个segment,每一个segment都是一个倒排索引;在查询的时,会把所有的segment查询结果汇总归并后作为最终的分片查询结果返回; 
     在创建索引的时候,elasticsearch会把文档信息写到内存bugffer中(为了安全,也一起写到translog),定时(可配置)把数据写到segment缓存小文件中,然后刷新查询,使刚写入的segment可查。 
虽然写入的segment可查询,但是还没有持久化到磁盘上。因此,还是会存在丢失的可能性的。 
      所以,elasticsearch会执行flush操作,把segment持久化到磁盘上并清除translog的数据(因为这个时候,数据已经写到磁盘上,不在需要了)。 
当索引数据不断增长时,对应的segment也会不断的增多,查询性能可能就会下降。因此,Elasticsearch会触发segment合并的线程,把很多小的segment合并成更大的segment,然后删除小的segment。 
     segment是不可变的,当我们更新一个文档时,会把老的数据打上已删除的标记,然后写一条新的文档。在执行flush操作的时候,才会把已删除的记录物理删除掉。

八、为什么Shard在index创建后不能修改?

Shards文档路由

当你对一个文档建立索引时,它仅存储在一个primary shard上。ES是怎么知道一个文档应该属于哪个shard?当你创建一个新的文档时,ES是怎么知道应该把它存储至shard1还是shard2? 这个过程不能随机无规律的,因为以后我们还要将它取出来。它的路由算法是:

shard = hash(routing) % number of primary_shards

routing的值可以是文档的id,也可以是用户自己设置的一个值。hash将会根据routing算出一个数值然后%primary shards的数量。这也是为什么primary_shards在index创建时就不能修改的原因。

我们可以向这个集群的任何一台NODE发送请求,每一个NODE都有能力处理请求。每一个NODE都知道每一个文档所在的位置所以可以直接将请求路由过去。下面的例子,我们将所有的请求都发送到NODE1。

九、路由(_routing)机制

一条数据是如何落地到对应的shard上的?

当索引一个文档的时候,文档会被存储到一个主分片中。 Elasticsearch 如何知道一个文档应该存放到哪个分片中呢?

首先这肯定不会是随机的,否则将来要获取文档的时候我们就不知道从何处寻找了。实际上,这个过程是根据下面这个算法决定的:

shard_num = hash(_routing) % num_primary_shards

其中 _routing 是一个可变值,默认是文档的 _id 的值 ,也可以设置成一个自定义的值。 _routing 通过 hash 函数生成一个数字,然后这个数字再除以 num_of_primary_shards (主分片的数量)后得到余数 。这个分布在 0 到 number_of_primary_shards-1 之间的余数,就是我们所寻求的文档所在分片的位置。这就解释了为什么我们要在创建索引的时候就确定好主分片的数量 并且永远不会改变这个数量:因为如果数量变化了,那么所有之前路由的值都会无效,文档也再也找不到了。

所以,ES的写入流程如下:

在进行写操作时,ES会根据传入的_routing参数(或mapping中设置的_routing, 如果参数和设置中都没有则默认使用_id), 按照公式shard_num = hash(_routing) % num_primary_shards,计算出文档要分配到的分片,在从集群元数据中找出对应主分片的位置,将请求路由到该分片进行文档写操作。

十、ES的写入流程

ES的任意节点都可以作为协调节点接受请求,当协调节点接受到请求后进行一系列处理,然后通过_routing字段找到对应的主分片,并将请求转发给主分片, 主分片完成写入后,将写入并发发送给各副本分片, 副本分片执行写入操作后返回给主分片, 主分片再将请求返回给协调节点。大致流程如下图:

协调节点(ES中接收并转发请求的节点称为协调节点,ES中所有节点都可以接受并转发请求):

  1. ingest pipeline:查看该请求是否符合某个ingest pipeline的pattern, 如果符合则执行pipeline中的逻辑,一般是对文档进行各种预处理,如格式调整,增加字段等。如果当前节点没有ingest角色,则需要将请求转发给有ingest角色的节点执行。
  2. 自动创建索引:判断索引是否存在,如果开启了自动创建则自动创建,否则报错。
  3. 设置routing:获取请求URL或mapping中的_routing,如果没有则使用_id, 如果没有指定_id则ES会自动生成一个全局唯一ID。
  4. 构建BulkShardRequest:由于Bulk Request中包含多种(Index/Update/Delete)请求,这些请求分别需要到不同的shard上执行,因此协调节点,会将请求按照shard分开,同一个shard上的请求聚合到一起,构建BulkShardRequest
  5. 将请求发送给主分片
  6. 等待主分片返回

主分片:

  1. 判断请求类型:遍历Bulk中的子请求,根据不同的请求类型跳转到不同的处理逻辑
  2. 将update操作转换为Index和Delete操作:获取文档的当前内容,与update内容合并生成新文档,然后将update请求转换成index请求
  3. Parse Doc:解析文档的各字段,并添加如_uid等ES相关的一些系统字段
  4. 更新mapping
  5. 获取Sequence ID 和version
  6. 写入lucene:这一步会开始对文档uid加锁。这里有个问题,如何保证Delete-Then-Add的原子性,ES是通过在Delete之前会加上已refresh锁,禁止被refresh,只有等待Add完成后释放了Refresh Lock, 这样就保证了这个操作的原子性。
  7. 写入translog:写入Lucene的Segment后,会以key value的形式写Translog, Key是Id, Value是Doc的内容。当查询的时候,如果请求的是GetDocById则可以直接根据_id从translog中获取。
  8. 重构bulk request:因为主分片已经将update操作转换为index操作或delete操作,因此要对之前的bulk request进行调整,只包含index或delete操作,不需要再进行update的处理操作。
  9. flush translog:落盘
  10. 发送请求给副本分片
  11. 等待副本分片响应

副本分片(与主分片类似)

十一、近实时性与数据可靠性

近实时性:

当一个文档写入Lucene后是不能被立即查询到的,Elasticsearch提供了一个refresh操作,会定时地调用lucene的reopen(新版本为openIfChanged)为内存中新写入的数据生成一个新的segment,此时被处理的文档均可以被检索到。默认为1s, 当然还可以在写入请求中带上refresh表示写入后立即refresh,另外还可以调用refresh API显式refresh。

数据存储可靠性:

  • 引入translog:当一个文档写入Lucene后是存储在内存中的,即使执行了refresh操作仍然是在文件系统缓存中,如果此时服务器宕机,那么这部分数据将会丢失。为此ES增加了translog, 当进行文档写操作时会先将文档写入Lucene,然后写入一份到translog,写入translog是落盘的。与传统的分布式系统不同,这里是先写入Lucene再写入translog,原因是写入Lucene可能会失败,为了减少写入失败回滚的复杂度,因此先写入Lucene.
  • flush操作:另外每30分钟或当translog达到一定大小(由index.translog.flush_threshold_size控制,默认512mb), ES会触发一次flush操作,此时ES会先执行refresh操作将buffer中的数据生成segment,然后调用lucene的commit方法将所有内存中的segment fsync到磁盘。此时lucene中的数据就完成了持久化,会清空translog中的数据(6.x版本为了实现sequenceIDs,不删除translog)

十二、分词

十三、文档相关性

ElasticSearch根据计算得分来确定文档相关性,得分越高,相关性越强。

计算相关性主要依据两个要素:词频和逆文档频率。

词频:TF

考虑一篇文档得分的首要方式,是查看一个词条在文档中出现的次数,比如某篇文章围绕es的打分展开的,那么文章中肯定会多次出现相关字眼,当查询时,我们认为该篇文档更符合,所以,这篇文档的得分会更高。
 

逆文档频率:IDF

相对于词频,逆文档频率稍显复杂,如果一个词条在索引中的不同文档中出现的次数越多,那么它就越不重要。
来个例子,示例地址

The rules-which require employees to work from 9 am to 9 pm
In the weeks that followed the creation of 996.ICU in March
The 996.ICU page was soon blocked on multiple platforms including the messaging tool WeChat and the UC Browser.

假如es索引中,有上述3篇文档:

  • 词条ICU的文档频率是2,因为它出现在2篇文档中,文档的逆源自得分乘以1/DFDF是该词条的文档频率,这就意味着,由于ICU词条拥有更高的文档频率,所以,它的权重会降低。
  • 词条the的文档频率是3,它在3篇文档中都出现了,注意:尽管the在后两篇文档出都出现两次,但是它的词频是还是3,因为,逆文档词频只检查词条是否出现在某篇文档中,而不检查它在这篇文档中出现了多少次,那是词频该干的事儿

逆文档词频是一个重要的因素,用来平衡词条的词频。比如我们搜索the 996.ICU。单词the几乎出现在所有的文档中(中文中比如),如果这个鬼东西要不被均衡一下,那么the的频率将完全淹没996.ICU。所以,逆文档词频就有效的均衡了the这个常见词的相关性影响。以达到实际的相关性得分将会对查询的词条有一个更准确地描述。
当词频和逆文档词频计算完成。就可以使用TF-IDF公式来计算文档的得分了。

十四、选举原理

早期 es 版本有 split brain 问题,俗称脑裂。ES 采用的是一种 P2P 的 gossip 选举方式,Gossip 算法因为 Cassandra 而名声大噪。
背景:
Gossip 算法, 灵感来自办公室八卦, 只要一个人八卦一下, 在有限的时间内所有人都会知道该八卦的信息,
这种方式也与病毒传播类似, 因为 Gossip 有众多的别名"闲话算法"、"疫情传播算法"、"病毒感染算法"、"谣言传播(Rumor-Mongering)算法".
但 Gossip 并不是一个新东西, 之前的泛洪查找、路由算法都归属于这个范畴, 不同的是 Gossip 给这类算法提供了明确的语义、具体实施方法及收敛性证明.

特点:
Gossip 算法又被称为反熵(Anti-Entropy), 熵是物理学上的一个概念, 代表杂乱无章, 而反熵就是在杂乱无章中寻求一致,
这充分说明了 Gossip 的特点:在一个有界网络中, 每个节点都随机地与其他节点通信, 经过一番杂乱无章的通信,
最终所有节点的状态都会达成一致. 每个节点可能知道所有其他节点, 也可能仅知道几个邻居节点,
只要这些节可以通过网络连通, 最终他们的状态都是一致的, 当然这也是疫情传播的特点.
要注意到的一点是, 即使有的节点因宕机而重启, 有新节点加入, 但经过一段时间后,
这些节点的状态也会与其他节点达成一致, 也就是说, Gossip 天然具有分布式容错的优点.

本质:
Gossip 是一个带冗余的容错算法, 更进一步, Gossip 是一个最终一致性算法。
虽然无法保证在某个时刻所有节点状态一致, 但可以保证在”最终“所有节点一致, ”最终“是一个现实中存在, 但理论上无法证明的时间点。
因为 Gossip 不要求节点知道所有其他节点, 因此又具有去中心化的特点, 节点之间完全对等, 不需要任何的中心节点。
实际上 Gossip 可以用于众多能接受“最终一致性”的领域:失败检测、路由同步、Pub/Sub、动态负载均衡。
但 Gossip 的缺点也很明显, 冗余通信会对网路带宽、CPU 资源造成很大的负载, 而这些负载又受限于通信频率, 该频率又影响着算法收敛的速度。

总结:
Gossip 是一种去中心化、容错而又最终一致性的绝妙算法, 其收敛性不但得到证明还具有指数级的收敛速度。
使用 Gossip 的系统可以很容易的把 Server 扩展到更多的节点, 满足弹性扩展轻而易举。
唯一的缺点是收敛是最终一致性, 不适应那些强一致性的场景, 比如 2PC。

十五、架构图

1、第一层:Gateway层:elasticsearch支持的索引数据存储格式。Elasticsearch关闭再启动时或从gateway里面读取数据。

2、第二层:Distributed lucene directory层:elasticsearch是基于lucenu框架开发的。

3、第三层:elasticsearch对数据的加工处理方式,mapping:定义索引下面type字段的处理规则,比如:索引如何建立、数据类型等等,相当于关系型数据里面的schema。River是一个运行在elasticsearch集群内部的一个插件,主要是用来从外部获取异构数据,然后在elasticsearch里创建索引,常见的插件有rabbitmq、twitter river。

4、第四层:是elasticsearch自动发现节点的机制。Zen是用来实现节点自动发现,还有master节点选取用的,假如maste出现了故障,不能工作了,那么其它节点会自动选举,然后产生一个新的master。Elasticsearch是基于P2P的系统,它首先头通过广播机制寻找存在的节点,然后再通过多播协议来进行节点间的通信,同时也支持点对点交互。

5、第五层:是elasticsearch的脚本执行功能,有了这个功能很方便的对查询出来的数据进行加工处理,脚本类型:mvel、js、python等。

6、第六层:3rd plugins:意思是elasticsearch支持安装很多第三方插件。

7、第七层:是elasticsearch的交互方式,支持三种协议:thrift、memcached、http,其中elasticsearch是默认用http协议传输的。

8、Restful Style API:是elasticsearch的API支持模式,现在这个RESTFUL这样的API接口的标准是非常流行的

9、java(Netty):elasticsearch采用了java语言,同时java语言也是对elasticsearch支持度最好的语言,因为这个lucene是基于java开发的。

 
十六、对比solr

1.elasticsearch更侧重于实时的数据分析,solr在实时搜索方面的效率是不如elasticsearch的。

2.在支持文本格式方面,solr比elasticsearch强。Solr支持的文本格式有:html,pd,word,excel,cvs,而elasticsearch只支持json这种格式。

小结:大家选择工具的同时,要根据自己项目的情况去选择,这样才有利于项目的开展。

十七、ElasticSearch查询

环境:ES2.3.2     

地址:xxx:9200

注意动词使用:GET,POST,PUT,DELETE

ElasticSearch  PK  Mysql

我的告警页面,查询条件会status='OK',isalarm=1,时间范围在:2019-03-01 13:45:41   到    2019-03-07 13:47:37的告警消息

Mysql:

select mca.endpoint_counter,mca.host_uuid,mca.host_status,mca.type,mca.pid,mca.id as id,businessid as grp,businessid as grp_en_name, eventid as eventid, endpoint as endpoint,node as node,triggername as triggername ,itemid as metric ,leveles as alertlevel ,itemvalue as leftvalue ,alertstatus,fuxi_status,round(case when enddate is null then ((UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(begindate))/60 ) else ((UNIX_TIMESTAMP(enddate) - UNIX_TIMESTAMP(begindate))/60 ) end,2) as duration,handlestatus as handlestatus,handleprocess as handleprocess,handlemessage as handlemessage,mca.createdate as createdate,mca.modifydate as modifydate,begindate as eventTime,enddate as enddate,defaultType as defaultType,userId as userId,strategy as strategy,pushedtags as pushedtags,tu.username,mca.isalarm,source,idc,mca.is_process_timeout as isTimeout 
from mc_alerts mca LEFT JOIN t_users tu on tu.id = mca.userid join grp g on mca.businessid=g.grp_en_name 
where mca.begindate >= '2019-03-01 13:45:41' and mca.begindate <= '2019-03-07 13:47:37' and mca.alertstatus ='OK' 
and mca.isalarm  = 1   and find_in_set('weiwei',g.contacts)

查询时间约;10秒

http://xxx:9200/hubble-alarm-mysqlio/mc_alerts/_search?pretty

ElasticSearch:

{
	"profile":false,
	"from" : 0, "size" : 50,
    "query": {
    	"bool": {
            "must": [
            	{"range":{"eventTime":{"gte":1551419141000,"lte":1551937657000}}},
            	{"term":{"status":"ok"}},
            	{"term":{"isalarm":"1"}},
            	{"terms": {
		            "grp": ["monitor","hubble_dialtest","sh-ops","mesos","personal-weiwei","Qiyi-Cloud-Monitor","hubble","sysnet-qim-test","qim_test_","idrac","idrac_cangnan_ct","idrac_jiujiang_ct","idrac_jilin_crtc","idrac_shijiazhuang_cnc","idrac_zhanjiang_ct","idrac_shijiazhuang2_cnc","idrac_foshan_ct","idrac_beijing2_ct","idrac_qingdao_cnc","idrac_zibo2_cnc","idrac_shanghai_crtc","idrac_sichuan_cmnet","idrac_tianjin_scc","idrac_wuxi_ct","idrac_shanghai_colnet","idrac_heilongjiang_cmnet","idrac_handan_cnc","idrac_jiangsu_cmnet","idrac_jiangsu_crtc","idrac_beijing4_dxt","idrac_beijing_dxt","idrac_xinjiang_cmnet","idrac_shan3xi_cmnet","idrac_zhanjiang2_ct","idrac_liaoning_crtc","idrac_zhejiang_cmnet","idrac_hangzhou2_cnc","idrac_shan1xi_cmnet","idrac_zhejiang_crtc","idrac_hubei_crtc","idrac_guangdong_crtc","idrac_sichuan_crtc","idrac_beijing_crtc","idrac_yunnan_aipu","idrac_guangzhou_gwbn","idrac_anhui_cmnet","idrac_shan3xi_crtc","idrac_handan2_cnc","idrac_changshu_ct","idrac_taicang_ct","idrac_wujiang_ct","idrac_fujian_crtc","idrac_hunan_crtc","idrac_jinan_ct","idrac_chongqing_aipu","idrac_qingdao_cmnet","idrac_shenyang_gwbn","idrac_jilin_cnc","idrac_shenzhen_twnet","idrac_shandong_crtc","idrac_jiamusi_cnc","idrac_heilongjiang_crtc","idrac_beijing_bgctvnet","idrac_jiangsu_scc","idrac_jilin2_cnc","idrac_hengyang_ct","idrac_ningbo2_ct","idrac_hebei_crtc","idrac_wuhan2_gwbn","idrac_henan_crtc","idrac_beijing_office","idrac_fujian_cfic","idrac_jinan2_cnc","idrac_jiangxi_crtc","idrac_beijing4_ct","idrac_hunan_citic","idrac_shan3xi_scc","idrac_jinan_cmnet","idrac_shenzhen_gwbn","idrac_hunan_scc","idrac_qingdao_scc","idrac_beijing2_crtc","idrac_chongqing_ct","idrac_shanghai6_ct","idrac_jiyang_cnc","idrac_nanning_crtc","idrac_zibo_cnc","idrac_beijing3_crtc","idrac_liaoning_cmnet","idrac_changchun_fbwn","idrac_chongqing_gwbn","idrac_beijing2_cnc","idrac_xian_gwbn","idrac_kunming_ct","idrac_quanzhou_cnc","idrac_foshan2_ct","idrac_beijing_21vianet","idrac_suzhou_cnc","idrac_jinan4_cnc","idrac_zhejiang3_cmnet","idrac_beijing_cnc","idrac_guangzhou_crtc","idrac_sichuan_gwbn","idrac_dongguan4_ct","idrac_jilin_cmnet","idrac_jining_cnc","idrac_dongguan5_ct","idrac_shijiazhuang3_cnc","idrac_wuxi2_ct","idrac_anhui2_cmnet","idrac_dongguan7_ct","idrac_tianjin_gwbn","idrac_beijing2_ispun","idrac_jinan2_cmnet","idrac_shijiazhuang_gwbn","idrac_xiamen_gwbn","idrac_nanjing_cmnet","idrac_kunming_gwbn","idrac_hangzhou_gwbn","idrac_changsha_gwbn","idrac_dalian_fbwn","idrac_nanning_gwbn","idrac_yichang_gwbn","idrac_haerbin_gwbn","idrac_changsha_cscatv","idrac_nanjing_gwbn","idrac_chongqing2_ct","idrac_nanchang_gwbn","idrac_suzhou_gwbn","idrac_changchun_gwbn","idrac_taiyuan_gwbn","idrac_jinan_gwbn","idrac_fuzhou_gwbn","idrac_dongguan_gwbn","idrac_beijing3_fbwn","idrac_wuhan3_gwbn","idrac_wuhan_scc","idrac_shantou_gwbn","idrac_guiyang_gwbn","idrac_jiyang_cmnet","idrac_hangzhou3_wasu","idrac_taicang2_ct","idrac_beijing6_dxt","idrac_shenzhen_cmnet","idrac_pps_shanghai_office","idrac_beijing_citic","idrac_quanzhou3_cnc","idrac_beijing2_21vianet","idrac_shenyang4_cnc","idrac_foshan6_ct","idrac_jinan5_cnc","idrac_losangeles_dxt","idrac_shenyang5_cnc","idrac_chongqing3_ct","idrac_shanghai12_ct","idrac_shanghai3_crtc","idrac_taibei_office","idrac_beijing_cmnet","idrac_wuhan4_gwbn","idrac_taiyuan2_cmnet","idrac_shanghai3_gwbn","idrac_tianjin4_cnc","idrac_beijing10_ct","idrac_zibo5_cnc","idrac_shenyang2_gwbn","idrac_shenzhen2_gwbn","idrac_fuzhou_scc","idrac_chongqing4_ct","idrac_hefei_cmnet","idrac_taibei_other","idrac_beijing7_ct","idrac_haerbin2_cnc","idrac_jinan7_cnc","idrac_chengdu_gwbn","idrac_changchun_crtc","idrac_tianjin3_gwbn","idrac_shenyang_fengantongwei","idrac_haidong_cmnet","idrac_zhejiang6_cmnet","idrac_taian2_scc","idrac_chongqing2_scc","idrac_nanchang5_ct","idrac_shanghai6_gwbn","idrac_hefei2_scc","idrac_beijing5_cnc","idrac_chongqing_office","idrac_haerbin_cnc","idrac_dalian_gwbn","idrac_chongqing_cnc","idrac_ganzhou_ct","idrac_beijing2_office","idrac_wuhan_cernet","idrac_beijing3_dxt","idrac_guangzhou_ehome","idrac_longsheng_storeroom","idrac_chongqing3_cnc","idrac_hangzhou_wasu","idrac_baidu_jinan_cnc","idrac_shenzhen_fengantongwei","idrac_foshan_fengantongwei","idrac_yichun_fengantongwei","idrac_zhengzhou_fengantongwei","idrac_xianyang_fengantongwei","idrac_guangzhou_fengantongwei","idrac_nanning_fengantongwei","idrac_hefei_fengantongwei","idrac_nanjing_fengantongwei","idrac_changsha_fengantongwei","idrac_changchun_fengantongwei","idrac_lanzhou_scc","idrac_shijiazhuang_wasu","idrac_shanghai4_gwbn","idrac_shenzhen3_gwbn","idrac_xianyang_scc","idrac_huhehaote_scc","idrac_guiyang_scc","idrac_fuzhou_wasu","idrac_seoul_dxt","idrac_toronto_dxt","idrac_shenzhen_other","idrac_guangzhou_21vianet","idrac_nanchang_scc","idrac_changsha_scc","idrac_jinan_scc","idrac_beijing_baidu","idrac_quanzhou_wasu","idrac_hangzhou7_wasu","idrac_dongguan_scc","idrac_zhengzhou2_scc","idrac_jiangyin2_wasu","idrac_nanning2_wasu","idrac_beijing6_office","idrac_shenyang_crtc","idrac_beijing11_ct","idrac_wuhan_ct","idrac_lanzhou2_ct","idrac_wenzhou_ct","idrac_qingdao_gwbn","idrac_weifang_cnc","idrac_chongqing2_cnc","idrac_kunming2_ct","idrac_hangzhou_cnc","idrac_dalian_cnc","idrac_jinan_cnc","idrac_hongkong_bn","idrac_ningbo6_ct","idrac_yantai_cnc","idrac_zhanjiang5_ct","idrac_loudi_ct","idrac_zhenhai_ct","idrac_dongguan_ct","idrac_jiyang_ct","idrac_dongguan2_ct","idrac_zhaotong_ct","idrac_zhanjiang4_ct","idrac_zhengzhou4_cnc","idrac_beijing5_dxt","idrac_zhejiang2_cmnet","idrac_ningbo3_ct","idrac_dongguan3_ct","idrac_dalian2_cnc","idrac_lanzhou3_ct","idrac_beijing9_dxt","idrac_zibo3_cnc","idrac_nanning_ct","idrac_changshu2_ct","idrac_haikou_ct","idrac_ruian2_ct","idrac_ruian_ct","idrac_xiamen2_ct","idrac_wuxi3_ct","idrac_zhejiang4_cmnet","idrac_xiamen3_ct","idrac_nanning2_ct","idrac_changshu3_ct","idrac_wujiang2_ct","idrac_taiyuan5_cnc","idrac_beijing2_cmnet","idrac_qingdao_ct","idrac_wuxi4_ct","idrac_haikou2_ct","idrac_xiamen_cnc","idrac_yiyang_ct","idrac_zhejiang5_cmnet","idrac_chongqing4_cnc","idrac_jilin_scc","idrac_zhaotong2_ct","idrac_jiangmen_cnc","idrac_cangzhou2_cnc","idrac_qingdao2_ct","idrac_hefei_gwbn","idrac_zhengzhou_gwbn","idrac_lanzhou4_ct","idrac_qingdao4_cnc","idrac_shenzhen_ct","idrac_guangzhou_cmnet","idrac_suzhou2_cnc","idrac_nanning3_ct","idrac_quzhou_ct","idrac_xiamen4_ct","idrac_zibo4_cnc","idrac_zhenjiang2_ct","idrac_nanjing2_cmnet","idrac_nanjing3_cmnet","idrac_wuxi_gwbn","idrac_chengdu_cmnet","idrac_beijing3_21vianet","idrac_zhengzhou5_cnc","idrac_beijing5_ct","idrac_zhanjiang6_ct","idrac_beijing6_ct","idrac_beijing4_office","idrac_hengyang3_ct","idrac_dongguan8_ct","idrac_wuxi5_ct","idrac_beijing7_dxt","idrac_chongqing7_cnc","idrac_nanjing4_cmnet","idrac_jilin3_cnc","idrac_guangzhou2_gwbn","idrac_quanzhou2_cnc","idrac_beijing3_cnc","idrac_beijing4_cnc","idrac_foshan3_ct","idrac_foshan4_ct","idrac_foshan8_ct","idrac_beijing8_dxt","idrac_jinan3_cmnet","idrac_xiamen2_cnc","idrac_shanghai11_ct","idrac_shanghai8_ct","idrac_shanghai10_ct","idrac_beijing3_cmnet","idrac_beijing9_ct","idrac_shaoguan_gwbn","idrac_ningbo_cnc","idrac_nanchang3_ct","idrac_nanchang2_ct","idrac_dalian3_cnc","idrac_wuhan2_ct","idrac_xiamen5_ct","idrac_zhenjiang3_ct","idrac_wuhan4_ct","idrac_kunming3_ct","idrac_yuyao_ct","idrac_beijing5_crtc","idrac_huizhou_cnc","idrac_huizhou3_cnc","idrac_shijiazhuang4_cnc","idrac_hangzhou4_wasu","idrac_nanchang4_ct","idrac_hengyang4_ct","idrac_hangzhou2_cmnet","idrac_hangzhou_cmnet","idrac_foshan5_ct","idrac_lasa_cmnet","idrac_nanning4_ct","idrac_huhehaote_cnc","idrac_jinan4_cmnet","idrac_jiyang2_ct","idrac_beijing4_21vianet","idrac_lanzhou5_ct","idrac_yangzhou_ct","idrac_yangzhou2_ct","idrac_shanghai2_chilian","idrac_shijiazhuang5_cnc","idrac_shaoxing_ct","idrac_shaoxing2_ct","idrac_kunming_cnc","idrac_qingdao3_ct","idrac_zhenjiang4_ct","idrac_wuxi7_ct","idrac_qingdao5_cnc","idrac_wuxi_cmnet","idrac_jinan5_cmnet","idrac_guangzhou2_crtc","idrac_nanjing5_cmnet","idrac_hengyang6_ct","idrac_hangzhou3_cmnet","idrac_qingdao2_cmnet","idrac_chongqing_scc","idrac_taicang3_ct","idrac_changshu4_ct","idrac_wujiang3_ct","idrac_ruian3_ct","idrac_hangzhou5_wasu","idrac_nanning2_crtc","idrac_suqian_ct","idrac_qingdao6_cnc","idrac_luoyang_cnc","idrac_luoyang2_cnc","idrac_kunming4_ct","idrac_foshan7_ct","idrac_shenyang_cmnet","idrac_shenzhen2_ct","idrac_jinan8_cnc","idrac_chengdu2_aipu","idrac_chongqing6_cnc","idrac_jiangmen2_cnc","idrac_lanzhou_cmnet","idrac_chengdu2_cmnet","idrac_handan3_cnc","idrac_xian2_gwbn","idrac_yantai2_cnc","idrac_hengyang7_ct","idrac_binzhou_cnc","idrac_yangzhou3_ct","idrac_qingdao2_gwbn","idrac_wuhan3_ct","idrac_hefei3_cmnet","idrac_hefei2_cmnet","idrac_yangzhou4_ct","idrac_suzhou3_cnc","idrac_lanzhou7_ct","idrac_shenyang6_cnc","idrac_xiamen6_ct","idrac_qingdao4_ct","idrac_wulumuqi_cmnet","idrac_quzhou2_ct","idrac_beijing4_fbwn","idrac_cangzhou3_cnc","idrac_kunming_cmnet","idrac_shanghai4_crtc","idrac_beijing10_dxt","idrac_hangzhou5_cmnet","idrac_xiangyang_cmnet","idrac_xiangyang2_cmnet","idrac_hangzhou4_cmnet","idrac_haerbin_cmnet","idrac_hengyang8_ct","idrac_kunming_fengantongwei","idrac_chongqing2_cmnet","idrac_jinan_fengantongwei","idrac_hangzhou_fengantongwei","idrac_chengdu_fengantongwei","idrac_kunming5_ct","idrac_dongguan10_ct","idrac_guiyang_fengantongwei","idrac_suqian2_ct","idrac_dalian4_cnc","idrac_zhengzhou6_cnc","idrac_beijing2_bgctvnet","idrac_qingdao3_cmnet","idrac_qingdao4_cmnet","idrac_zhaotong3_ct","idrac_hangzhou6_cmnet","idrac_hangzhou7_cmnet","idrac_hangzhou8_cmnet","idrac_xian3_ct","idrac_taiyuan_fengantongwei","idrac_shanghai5_gwbn","idrac_zhengzhou2_cmnet","idrac_huhehaote2_cnc","idrac_beijing2_cernet","idrac_wulumuqi_ct","idrac_chongqing3_cmnet","idrac_yunnan_crtc","idrac_chengdu2_scc","idrac_tianjin_crtc","idrac_wuxi2_cmnet","idrac_tianjin_fengantongwei","idrac_guangzhou2_cmnet","idrac_changsha_cmnet","idrac_xiamen_fengantongwei","idrac_shijiazhuang_crtc","idrac_wuhan5_cnc","idrac_nanjing_cnc","idrac_shijiazhuang6_cnc","idrac_xian4_ct","idrac_taiyuan6_cnc","idrac_jilin4_cnc","idrac_jining3_cnc","idrac_hefei4_cmnet","idrac_zhengzhou3_cmnet","idrac_changchun_iocp","idrac_wuxi3_cmnet","idrac_zhengzhou7_cnc","idrac_zhengzhou8_cnc","idrac_nantong_ct","idrac_taizhou_ct","idrac_taiyuan7_cnc","idrac_zhuzhou_ct","idrac_beijing2_cnix","idrac_shenyang2_cmnet","idrac_tianjin6_cnc","idrac_haikou3_ct","idrac_shijiazhuang7_cnc","idrac_wuxi10_ct","idrac_chongqing5_ct","idrac_ningbo7_ct","idrac_chengdu3_cmnet","idrac_chongqing4_cmnet","idrac_nanchang3_cmnet","idrac_fuzhou2_cfic","idrac_nanjing_iocp","idrac_wuhan_qnet","idrac_suzhou_test","idrac_shanghai_office","idrac_shenyang_cnc","idrac_tianjin2_cnc","idrac_haerbin_fengantongwei","idrac_shanghai_yicheng","idrac_wuhan_fengantongwei","idrac_beijing5_office","idrac_chongqing2_gwbn","idrac_hangzhou6_wasu","idrac_chengdu_scc","idrac_wulumuqi_scc","idrac_hongkong_hkix","idrac_guangzhou2_scc","idrac_hefei_scc","idrac_beijing5_21vianet","idrac_fuzhou_cfic","idrac_losangeles2_dxt","idrac_haerbin_scc","idrac_beijing_qishunix","idrac_jinan_wasu","idrac_huhehaote2_scc","idrac_guangzhou_gzcatv","idrac_shenyang_scc","idrac_hongkong2_equinix","idrac_shanghai_qnet","idrac_macau_ctm","idrac_wuhan2_scc","idrac_yinchuan_scc","jk-vrs","ads-backend_proxy_bjdxt","ads_serving_proxy_zjyd","idrac_shijiazhuang_scc","idrac_xianyang2_scc","idrac_dezhou_scc","idrac_taian_scc","idrac_beijing2_baidu","idrac_nanjing2_iocp","hcdn_uproxy","idrac_lasa_ngaa","ads_online_exchange","idrac_guangzhou_iocp","idrac_shijiazhuang_cmnet","idrac_tianjin2_scc","idrac_zhengzhou4_cmnet","idrac_nanchang_fengantongwei","idrac_wuxi_scc","dxt9/aggregator","beijing_baidu/aggregator","dxt9/hbs","beijing_baidu/hbs","beijing_baidu/transfer","dxt9/transfer","beijing_baidu/judge","dxt9/judge","beijing_baidu/task","dxt9/task","beijing_baidu/query","dxt9/query","beijing_baidu/graph","jiyang/graph","idrac_beijing_zjy","agent_data_delivery_bdyf","idrac_kelamayi_cmnet","idrac_taibei_aptg","idrac_xian2_iocp","idrac_haerbin_iocp","idrac_xian_scc","idrac_changshu_cmnet","idrac_nanjing6_cmnet","idrac_taibei_twm","idrac_taibei_tst","iocp_cache","idrac_hangzhou9_cmnet","idrac_tianjin2_crtc","NODE","personal-renkai-sx","idrac_chengdu_office","idrac_nanning_scc","idrac_beijing_heijing","idrac_guangzhou3_cmnet","idrac_guangzhou4_cmnet","idrac_zhengzhou5_cmnet","idrac_nanjing3_iocp","idrac_guiyang2_scc","idrac_guangzhou2_iocp","idrac_chengdu4_cmnet","idrac_xianyang_cmnet","idrac_shijiazhuang_test","idrac_heze_fengantongwei","idrac_quanzhou_fengantongwei","idrac_linyi_fengantongwei","idrac_dongguan_fengantongwei","idrac_xuzhou_fengantongwei","idrac_ningbo_fengantongwei","idrac_handan_fengantongwei","idrac_shijiazhuang_fengantongwei","idrac_baoding_fengantongwei","idrac_dongguan2_scc","exchange_offline","hwm","idrac_tianjin_iocp","idrac_beijing_iocp","idrac_beijing2_iocp","idrac_chongqing6_cmnet","hubblemanager","hubble-api/portal/nginx","hubble-sdk-api/graph","hubble-alarm","hubble-job","hubble-test","idrac_changchun2_iocp","idrac_xian_iocp","idrac_yangzhou_iocp","idrac_shanghai3_cmnet","idrac_guiyang2_cmnet","idrac_guangzhou3_iocp","cluster_monitor","idrac_shanghai_iocp","idrac_chongqing5_cmnet","idrac_wuhan3_scc","idrac_shanghai2_iocp","idrac_shanghai3_iocp","idrac_yichang_gezhouba","idrac_tianjin2_iocp","idrac_yichang_iocp","idrac_zhengzhou6_cmnet","idrac_kunming2_cmnet","idrac_chengdu2_office","idrac_nanning2_cmnet","     ","renkaitest111","renkaitest2","idrac_changchun_scc","idrac_fuzhou2_wasu","idrac_xiangyang3_cmnet","idrac_hangzhou10_cmnet","idrac_nanchang4_cmnet","idrac_beijing_chinacache","mesos-chronos","idrac_shijiazhuang2_cmnet","idrac_fuzhou_cmnet","idrac_huainan_cmnet","idrac_haerbin2_scc","idrac_beijing_xingchi","idrac_shenzhen2_twnet","idrac_beijing_cnisp","idrac_haerbin_test","idrac_hangzhou8_wasu","idrac_chengdu3_scc","idrac_langfang_ct_test","idrac_tonghua_test","idrac_zhengzhou7_cmnet","qim-test-sub-ww","qim-test-sub-2","weiwei-test-qim3","idrac_nanchang5_cmnet","idrac_wuhan_iocp","idrac_jinan_iocp","idrac_taiyuan8_cnc","idrac_changsha2_scc","idrac_huhehaote_cmnet","idrac_shijiazhuang2_scc","idrac_shijiazhuang3_cmnet","idrac_shenyang3_cmnet","idrac_shanghai2_colnet","idrac_haikou2_cmnet","idrac_guangzhou5_cmnet","idrac_beijing4_iocp","idrac_wuxi2_scc","idrac_shanghai4_iocp","idrac_guangzhou6_cmnet","idrac_guangzhou7_cmnet","idrac_wuhan_lkg","idrac_chongqing3_scc","idrac_shanghai5_iocp","idrac_shanghai10_iocp","idrac_shanghai9_iocp","idrac_lanzhou_cnciptv","idrac_beijing3_baidu","idrac_hangzhou9_wasu","idrac_wuhan_baidu","idrac_shanghai_huanwei","idrac_beijing2_xingchi","Hubble-Transfer-Judge","idrac_qingdao5_cmnet","a/b/c","test_child_group","idrac_shenzhen3_ct","idrac_chongqing_cnciptv","idrac_beijing5_iocp","test-luyafei-04","test2","test22","test-luyafei-05","test-luyafei-06","test-luyafei-07","test-luyafei-08","idrac_zhoukou_fengantongwei","idrac_shangqiu_fengantongwei","idrac_wulumuqi_cnciptv","idrac_jining_fengantongwei","idrac_nanyang_fengantongwei","idrac_langfang_fengantongwei","idrac_cangzhou_fengantongwei","idrac_tangshan_fengantongwei","idrac_xinyang_fengantongwei","idrac_yancheng_fengantongwei","idrac_taizhou_fengantongwei","idrac_jinhua_fengantongwei","idrac_luoyang_fengantongwei","idrac_wuxi_fengantongwei","idrac_xuchang_fengantongwei","idrac_puyang_fengantongwei","idrac_liaocheng_fengantongwei","idrac_jinan_cernet","idrac_fuzhou2_cmnet","idrac_shanghai8_iocp","idrac_shanghai7_iocp","idrac_huanggang_iocp","idrac_shanghai6_iocp","idrac_ningbo_iocp","idrac_beijing6_iocp","idrac_tonghua_cnc","idrac_haerbin3_cnc","idrac_haerbin2_iocp","personal-weiwei-test","idrac_changsha2_cmnet","idrac_shijiazhuang3_scc","idrac_bangkok_ais","idrac_fuzhou_cnciptv","idrac_huhehaote3_scc","idrac_wuhan4_scc","Hubble-Grafana","idrac_quanzhou_scc","hubble-updater-server","idrac_zhengzhou_cnciptv"]
		        }
		    }]
    	}
    }
}

查询时间:184毫秒

1、创建索引

动词:POST

http://xxx:9200/weiwei-test-index-1

fail:

{
    "error": {
        "root_cause": [
            {
                "type": "index_already_exists_exception",
                "reason": "already exists",
                "index": "weiwei-test-index-1"
            }
        ],
        "type": "index_already_exists_exception",
        "reason": "already exists",
        "index": "weiwei-test-index-1"
    },
    "status": 400
}

success:

http://xxx:9200/weiwei-test-index-2

{
    "acknowledged": true
}

2、删除索引

动词:DELETE

和创建索引url一样,只是动词由POST改为DELETE

3、创建映射(mapping)

动词:POST

http://xxx:9200/weiwei-test-index-1/weiwei-test-type-1/_mapping?pretty

输入:

{
    "weiwei-test-type-1": {
            "properties": {
                "id": {
                    "type": "integer",
                    "index": "not_analyzed"
                },
				"name": {
                    "type": "string"
                },
				"age": {
                    "type": "integer",
                    "index": "not_analyzed"
                },
				"desc": {
                    "type": "string"
                }
            }
    }
}

输出:

{
    "acknowledged": true
}

4、查看创建的mapping映射

动词:GET

http://xxx9200/weiwei-test-index-1/weiwei-test-type-1/_mapping?pretty

输入:

输出:

{
    "weiwei-test-index-1": {
        "mappings": {
            "weiwei-test-type-1": {
                "properties": {
                    "age": {
                        "type": "integer"
                    },
                    "desc": {
                        "type": "string"
                    },
                    "id": {
                        "type": "integer"
                    },
                    "name": {
                        "type": "string"
                    }
                }
            }
        }
    }
}

5、插入数据

动词:POST

插入id为1的文档数据

http://xxx:9200/weiwei-test-index-1/weiwei-test-type-1/1

输入:

{
	"name":"weiwei1",
	"age":30,
	"height":174,
	"desc":"hi hello my company and elasticsearch lucence"
}

输出:

{
    "_index": "weiwei-test-index-1",
    "_type": "weiwei-test-type-1",
    "_id": "1",
    "_version": 1,
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

6、查询ID为1的文档

动词:GET

http://xxx:9200/weiwei-test-index-1/weiwei-test-type-1/1

输入:

输出:

{
    "_index": "weiwei-test-index-1",
    "_type": "weiwei-test-type-1",
    "_id": "1",
    "_version": 1,
    "found": true,
    "_source": {
        "name": "weiwei1",
        "age": 30,
        "height": 174,
        "desc": "hi hello my company and elasticsearch lucence"
    }
}

7、简单查询

7.1、查询前

注:_search 表示是查询请求

关键词解析:

1、term于match(参考:elasticsearch 中term与match区别_sxf_0123的博客-CSDN博客_elastic term和match

term:精确查询,输入的值不会被分词器分词
match:模糊查询,即先分词,再查找

2、match_phase:短语匹配

       1.match_phase中的所有term都出现在待查询字段之中

  2.待查询字段之中的所有term都必须和match_phase具有相同的顺序

3、query_string,类似于match_phase,但不用顺序匹配,而且匹配到一个就行了

4、range:范围查询

5、查询与过滤

查询:进行相关性分析,效率较低

过滤:不进行相关性分析,效率比查询高。

7.2、精确查找

http://xxx:9200/weiwei-test-index-1/weiwei-test-type-1/_search?pretty

输入:

{
	"profile":false,
	"fields": ["name","desc"], 
    "query": {
        "bool": {
            "must": {
                "term": {
                    "name": "solr"
                }
            }
        }
    }
}

输出:

{
    "took": 4,    
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.30685282,
        "hits": [
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "2",
                "_score": 0.30685282,
                "fields": {
                    "name": [
                        "solr"
                    ],
                    "desc": [
                        "hi Solr is base lucence"
                    ]
                }
            }
        ]
    }
}

7.3、模糊查找:对被分词的字段进行查找

http://xxx:9200/weiwei-test-index-1/weiwei-test-type-1/_search?pretty

分词搜索:

{
	"profile":false,
	"fields": ["name","desc"], 
    "query": {
        "bool": {
            "must": {
                "term": {
                    "desc": "hi Solr is base lucence"
                }
            }
        }
    }
}

输出:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}

未搜到原因:desc字段被分词了,分词有很多分词器,默认使用标准分词器,其中之一是会将空格两边对应的单词拆分并建立索引

搜索某个单词:

{
	"profile":false,
	"fields": ["name","desc"], 
    "query": {
        "bool": {
            "must": {
                "term": {
                    "desc": "lucence"
                }
            }
        }
    }
}

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 0.13424811,
        "hits": [
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "2",
                "_score": 0.13424811,
                "fields": {
                    "name": [
                        "solr"
                    ],
                    "desc": [
                        "hi Solr is base lucence"
                    ]
                }
            },
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "3",
                "_score": 0.13424811,
                "fields": {
                    "name": [
                        "lucence"
                    ],
                    "desc": [
                        "what is lucence and solr?"
                    ]
                }
            },
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "1",
                "_score": 0.11506981,
                "fields": {
                    "name": [
                        "weiwei1"
                    ],
                    "desc": [
                        "hi hello my company and elasticsearch lucence"
                    ]
                }
            }
        ]
    }
}

7.4、不分词

desc2在创建mapping映射的时候没有被分词。

mapping的字段如果不分词,必须输入完整字段

http://xxx:9200/weiwei-test-index-1/weiwei-test-type-2/_search?pretty

输入:

{
    "query": {
        "bool": {
            "must": {
                "term": {
                    "desc2": "hi hello my company and elasticsearch lucence"
                }
            }
        }
    }
}

输出:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-2",
                "_id": "AWlRbTTWumMPR0n84Njs",
                "_score": 1,
                "_source": {
                    "name": "weiwei",
                    "age": 30,
                    "height": 174,
                    "desc": "hi hello my company and elasticsearch lucence",
                    "desc2": "hi hello my company and elasticsearch lucence"
                }
            }
        ]
    }
}

7.5、match查找

会对搜索的字段进行分词,再分别查找每个词的匹配结果并合并

{
	"profile":false,
	"fields": ["name","desc"], 
    "query": {
        "bool": {
            "must": {
                "match": {
                    "desc": "hi Solr is base lucence"
                }
            }
        }
    }
}

输出:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 0.7220034,
        "hits": [
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "2",
                "_score": 0.7220034,
                "fields": {
                    "name": [
                        "solr"
                    ],
                    "desc": [
                        "hi the Solr is base lucence"
                    ]
                }
            },
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "3",
                "_score": 0.04908036,
                "fields": {
                    "name": [
                        "lucence"
                    ],
                    "desc": [
                        "what is lucence and solr?"
                    ]
                }
            },
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "1",
                "_score": 0.01581979,
                "fields": {
                    "name": [
                        "weiwei1"
                    ],
                    "desc": [
                        "hi hello my company and elasticsearch lucence"
                    ]
                }
            }
        ]
    }
}

7.6、大小写问题

不支持大写输入:ES在对Term建立索引的时候会默认都解析为小写单词

输入:小写的solr

{
	"profile":false,
	"fields": ["name","desc"], 
    "query": {
        "bool": {
            "must": {
                "term": {
                    "desc": "solr"
                }
            }
        }
    }
}

输出:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0.13424811,
        "hits": [
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "2",
                "_score": 0.13424811,
                "fields": {
                    "name": [
                        "solr"
                    ],
                    "desc": [
                        "hi Solr is base lucence"
                    ]
                }
            },
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "3",
                "_score": 0.13424811,
                "fields": {
                    "name": [
                        "lucence"
                    ],
                    "desc": [
                        "what is lucence and solr?"
                    ]
                }
            }
        ]
    }
}

输入大写的Solr

{
	"profile":false,
	"fields": ["name","desc"], 
    "query": {
        "bool": {
            "must": {
                "term": {
                    "desc": "Solr"
                }
            }
        }
    }
}

输出:未查到数据

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}

7.7、match_phrase

http://xxx:9200/weiwei-test-index-1/weiwei-test-type-1/_search?pretty

输入:

{
    "query": {
            "match_phrase": {
                "desc": "Solr is"
            }
    }
}

输出:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.75,
        "hits": [
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "2",
                "_score": 0.75,
                "_source": {
                    "name": "solr",
                    "age": 31,
                    "height": 174,
                    "desc": "hi the Solr is base lucence"
                }
            }
        ]
    }
}

7.8、query_string

http://xxx:9200/weiwei-test-index-1/weiwei-test-type-1/_search?pretty

输入:

{
    "query": {
            "query_string": {
            	"fields" : ["desc","name"],
            	"query" : "Solr is weiwei1"
            }
    }
}

输出:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 0.35325736,
        "hits": [
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "2",
                "_score": 0.35325736,
                "_source": {
                    "name": "solr",
                    "age": 31,
                    "height": 174,
                    "desc": "hi the Solr is base lucence"
                }
            },
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "3",
                "_score": 0.031711474,
                "_source": {
                    "name": "lucence",
                    "age": 30,
                    "height": 174,
                    "desc": "what is lucence and solr?"
                }
            },
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "1",
                "_score": 0.018120842,
                "_source": {
                    "name": "weiwei1",
                    "age": 30,
                    "height": 174,
                    "desc": "hi hello my company and elasticsearch lucence"
                }
            }
        ]
    }
}

7.9、Range

http://xxx:9200/weiwei-test-index-1/weiwei-test-type-1/_search?pretty

输入:

{
    "query": {
        "range" : {
            "age" : {
                "gte" : 31,
                "lte" : 40
            }
        }
    }
}

输出:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "name": "solr",
                    "age": 31,
                    "height": 174,
                    "desc": "hi the Solr is base lucence"
                }
            }
        ]
    }
}

7.10、过滤器

http://xxx:9200/weiwei-test-index-1/weiwei-test-type-1/_search?pretty

输入:

{
  "query" : {
        "filtered" : {
        	"filter" : { 
        		"term" : { "age" : 30 }
        	}
         }
   }
}

输出:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "name": "weiwei1",
                    "age": 30,
                    "height": 174,
                    "desc": "hi hello my company and elasticsearch lucence"
                }
            },
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "name": "lucence",
                    "age": 30,
                    "height": 174,
                    "desc": "what is lucence and solr?"
                }
            }
        ]
    }
}

7.11、查看各个分片查询详情

profile显示分片查询详情:作用:当查询请求较慢的时候,可以根据分片查找详情的分片耗时确定分片是否有问题或节点连通性是否良好等

{
	"profile":true,
	"fields": ["name","desc"], 
    "query": {
        "bool": {
            "must": {
                "term": {
                    "name": "solr"
                }
            }
        }
    }
}

输出:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.30685282,
        "hits": [
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "2",
                "_score": 0.30685282,
                "fields": {
                    "name": [
                        "solr"
                    ],
                    "desc": [
                        "hi Solr is base lucence"
                    ]
                }
            }
        ]
    },
    "profile": {
        "shards": [
            {
                "id": "[w3ITy1o9RxypVxkDDhpQbg][weiwei-test-index-1][1]",
                "searches": [
                    {
                        "query": [
                            {
                                "query_type": "BooleanQuery",
                                "lucene": "+name:solr #ConstantScore(_type:weiwei-test-type-1)",
                                "time": "0.06619900000ms",
                                "breakdown": {
                                    "match": 0,
                                    "score": 0,
                                    "create_weight": 44420,
                                    "build_scorer": 0,
                                    "next_doc": 0,
                                    "advance": 0
                                },
                                "children": [
                                    {
                                        "query_type": "TermQuery",
                                        "lucene": "name:solr",
                                        "time": "0.01083200000ms",
                                        "breakdown": {
                                            "match": 0,
                                            "score": 0,
                                            "create_weight": 10832,
                                            "build_scorer": 0,
                                            "next_doc": 0,
                                            "advance": 0
                                        }
                                    },
                                    {
                                        "query_type": "ConstantScoreQuery",
                                        "lucene": "ConstantScore(_type:weiwei-test-type-1)",
                                        "time": "0.01094700000ms",
                                        "breakdown": {
                                            "match": 0,
                                            "score": 0,
                                            "create_weight": 9530,
                                            "build_scorer": 0,
                                            "next_doc": 0,
                                            "advance": 0
                                        },
                                        "children": [
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "_type:weiwei-test-type-1",
                                                "time": "0.001417000000ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 0,
                                                    "create_weight": 1417,
                                                    "build_scorer": 0,
                                                    "next_doc": 0,
                                                    "advance": 0
                                                }
                                            }
                                        ]
                                    }
                                ]
                            }
                        ],
                        "rewrite_time": 27796,
                        "collector": [
                            {
                                "name": "SimpleTopScoreDocCollector",
                                "reason": "search_top_hits",
                                "time": "0.002415000000ms"
                            }
                        ]
                    }
                ]
            },
            {
                "id": "[BM_eMt22SbOAleUWHlEelw][weiwei-test-index-1][0]",
                "searches": [
                    {
                        "query": [
                            {
                                "query_type": "BooleanQuery",
                                "lucene": "+name:solr #ConstantScore(_type:weiwei-test-type-1)",
                                "time": "0.06402800000ms",
                                "breakdown": {
                                    "match": 0,
                                    "score": 0,
                                    "create_weight": 43387,
                                    "build_scorer": 0,
                                    "next_doc": 0,
                                    "advance": 0
                                },
                                "children": [
                                    {
                                        "query_type": "TermQuery",
                                        "lucene": "name:solr",
                                        "time": "0.01016100000ms",
                                        "breakdown": {
                                            "match": 0,
                                            "score": 0,
                                            "create_weight": 10161,
                                            "build_scorer": 0,
                                            "next_doc": 0,
                                            "advance": 0
                                        }
                                    },
                                    {
                                        "query_type": "ConstantScoreQuery",
                                        "lucene": "ConstantScore(_type:weiwei-test-type-1)",
                                        "time": "0.01048000000ms",
                                        "breakdown": {
                                            "match": 0,
                                            "score": 0,
                                            "create_weight": 9556,
                                            "build_scorer": 0,
                                            "next_doc": 0,
                                            "advance": 0
                                        },
                                        "children": [
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "_type:weiwei-test-type-1",
                                                "time": "0.0009240000000ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 0,
                                                    "create_weight": 924,
                                                    "build_scorer": 0,
                                                    "next_doc": 0,
                                                    "advance": 0
                                                }
                                            }
                                        ]
                                    }
                                ]
                            }
                        ],
                        "rewrite_time": 23151,
                        "collector": [
                            {
                                "name": "SimpleTopScoreDocCollector",
                                "reason": "search_top_hits",
                                "time": "0.001909000000ms"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

7.12、复杂查询

http://xxx:9200/weiwei-test-index-1/weiwei-test-type-1/_search?pretty

输入:

{
	"profile":true,
    "query": {
        "bool": {
            "must": {
                "term": {
                    "desc": "lucence"
                }
            },
            "must_not": {
                "term": {
                    "name": "weiwei1"
                }
            },
            "should": [
                {
                    "term": {
                        "height": "174"
                    }
                },
                {
                    "term": {
                        "age": "30"
                    }
                }
            ],
            "minimum_should_match": 1
        }
    }
}

输出:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0.4318313,
        "hits": [
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "3",
                "_score": 0.4318313,
                "_source": {
                    "name": "lucence",
                    "age": 30,
                    "height": 174,
                    "desc": "what is lucence and solr?"
                }
            },
            {
                "_index": "weiwei-test-index-1",
                "_type": "weiwei-test-type-1",
                "_id": "2",
                "_score": 0.07917817,
                "_source": {
                    "name": "solr",
                    "age": 31,
                    "height": 174,
                    "desc": "hi the Solr is base lucence"
                }
            }
        ]
    }
}

8、分词

8.1、查看分词结果:

http://xxx:9200/weiwei-test-index-1/weiwei-test-type-1/_validate/query?explain

输入:

{
  "query": {
    "match": {
      "desc": "hi Solr is base lucence"
    }
  }
}

输出:

{
    "valid": true,
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "explanations": [
        {
            "index": "weiwei-test-index-1",
            "valid": true,
            "explanation": "+(desc:hi desc:solr desc:is desc:base desc:lucence) #ConstantScore(+ConstantScore(_type:weiwei-test-type-1))"
        }
    ]
}

8.2、根据指定分析器对查询进行分析

http://xxx:9200/weiwei-test-index-1/_analyze?analyzer=Standard

{
    "field": "desc",
    "text": "hi-Solr is base lucence好样的"
}

输出:

{
    "tokens": [
        {
            "token": "hi",
            "start_offset": 0,
            "end_offset": 2,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "solr",
            "start_offset": 3,
            "end_offset": 7,
            "type": "<ALPHANUM>",
            "position": 1
        },
        {
            "token": "is",
            "start_offset": 8,
            "end_offset": 10,
            "type": "<ALPHANUM>",
            "position": 2
        },
        {
            "token": "base",
            "start_offset": 11,
            "end_offset": 15,
            "type": "<ALPHANUM>",
            "position": 3
        },
        {
            "token": "lucence",
            "start_offset": 16,
            "end_offset": 23,
            "type": "<ALPHANUM>",
            "position": 4
        },
        {
            "token": "好",
            "start_offset": 23,
            "end_offset": 24,
            "type": "<IDEOGRAPHIC>",
            "position": 5
        },
        {
            "token": "样",
            "start_offset": 24,
            "end_offset": 25,
            "type": "<IDEOGRAPHIC>",
            "position": 6
        },
        {
            "token": "的",
            "start_offset": 25,
            "end_offset": 26,
            "type": "<IDEOGRAPHIC>",
            "position": 7
        }
    ]
}

8.3、标准分析器 指定停用词

http://xxx:9200/weiwei-test-index-1/weiwei-test-type-1/_settings

输入:

{
  "index": {
    "analysis": {
      "analyzer": {
        "standard": {
          "type": "standard",
          "stop_words": [ "it", "is", "a" ]
        }
      }
    }
  }
}

输出:

{
    "_index": "weiwei-test-index-1",
    "_type": "weiwei-test-type-1",
    "_id": "_settings",
    "_version": 1,
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

一、_analyze

_analyze作用是查看数据分析详情;

直接上案例:

GET /index/_analyze

输入:

{
	"field":"grp",
	"text":"game-operation-group,物理机,computecloud_KVM,hubble-job"
}

输出:

{
    "tokens": [
        {
            "token": "game",
            "start_offset": 0,
            "end_offset": 4,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "operation",
            "start_offset": 5,
            "end_offset": 14,
            "type": "<ALPHANUM>",
            "position": 1
        },
        {
            "token": "group",
            "start_offset": 15,
            "end_offset": 20,
            "type": "<ALPHANUM>",
            "position": 2
        },
        {
            "token": "物",
            "start_offset": 21,
            "end_offset": 22,
            "type": "<IDEOGRAPHIC>",
            "position": 3
        },
        {
            "token": "理",
            "start_offset": 22,
            "end_offset": 23,
            "type": "<IDEOGRAPHIC>",
            "position": 4
        },
        {
            "token": "机",
            "start_offset": 23,
            "end_offset": 24,
            "type": "<IDEOGRAPHIC>",
            "position": 5
        },
        {
            "token": "computecloud_kvm",
            "start_offset": 25,
            "end_offset": 41,
            "type": "<ALPHANUM>",
            "position": 6
        },
        {
            "token": "hubble",
            "start_offset": 42,
            "end_offset": 48,
            "type": "<ALPHANUM>",
            "position": 7
        },
        {
            "token": "job",
            "start_offset": 49,
            "end_offset": 52,
            "type": "<ALPHANUM>",
            "position": 8
        }
    ]
}

二、profile

在输入中加入profile并指定为true,就会将查找分片的逻辑展示出来

输入:

{
    "profile": true,
    "query": {
        "query_string": {
            "default_field": "pushedtags",
            "query": "物理"
        }
    },
    "size": 1
}

输出:会将具体查找了哪些分别展示出来

{
    "took": 2486,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 5395800,
        "max_score": 0.5007491,
        "hits": [
            {
                "_index": "hubble-alarm-mysqlio",
                "_type": "mc_alerts",
                "_id": "25318629",
                "_score": 0.5007491,
                "_source": {
                    "endpoint_counter": "vcdn_http_error_code_4xx",
                    "createdate": 1543602396000,
                    "pid": -1,
                    "alertlevel": "P5",
                    "source": "hubble",
                    "type": 0,
                    "shakecount": 0,
                    "appId": 5430,
                    "id": 25318629,
                    "handlestatus": "0",
                    "idc": "hefei2_cmnet",
                    "needupgrade": "2",
                    "isshake": "0",
                    "version": 0,
                    "host_status": 1,
                    "currentStep": 1,
                    "node": "10.55.0.43",
                    "triggername": "nginx日志4xx异常",
                    "judge_address": "10.19.6.225:6081",
                    "strategy_id": 7520,
                    "strategyGrade": 1,
                    "status": "OK",
                    "eventid": "s_7520_5ff64f081b093761b60552ff93cffae8",
                    "isupgrade": "0",
                    "leftvalue": "42",
                    "fuxi_status": 1,
                    "host_uuid": "4c4c4544-0031-4d10-8057-cac04f374332",
                    "rightvalue": "50",
                    "endpoint": "localhost.localdomain",
                    "isalarm": 1,
                    "eventTime": 1543602360000,
                    "grp": "vcdn_cache_10G",
                    "init_level": "P5",
                    "defaultType": "autook",
                    "metric": "vcdn_http_error_code_4xx",
                    "pushedtags": "{\"boxtype\":\"物理机\",\"group\":\"vcdn_cache_10G\",\"idc\":\"hefei2_cmnet\",\"uuid\":\"4c4c4544-0031-4d10-8057-cac04f374332\"}",
                    "strategy": "{\"aiops\":0,\"func\":\"all(#3)\",\"id\":7520,\"maxStep\":1,\"metric\":\"vcdn_http_error_code_4xx\",\"note\":\"\",\"operator\":\">=\",\"priority\":5,\"rel_id\":-1,\"rightValue\":50,\"step\":0,\"tags\":{},\"tags_regexp\":{},\"tpl\":{\"actionId\":0,\"creator\":\"litaowang,jiangxianzeng,maliheng,zhaohui\",\"id\":1702,\"name\":\"vcdn-nginx\",\"parentId\":0}}",
                    "userId": "96c5d6ebd81c4bbba9eded059db6aad6",
                    "modifydate": 1543602516000,
                    "enddate": 1543602480000
                }
            }
        ]
    },
    "profile": {
        "shards": [
            {
                "id": "[BM_eMt22SbOAleUWHlEelw][hubble-alarm-mysqlio][0]",
                "searches": [
                    {
                        "query": [
                            {
                                "query_type": "BooleanQuery",
                                "lucene": "+(pushedtags:物 pushedtags:理) #ConstantScore(_type:mc_alerts)",
                                "time": "3931.298657ms",
                                "breakdown": {
                                    "match": 0,
                                    "score": 801778303,
                                    "create_weight": 803327,
                                    "build_scorer": 1742270,
                                    "next_doc": 1228867720,
                                    "advance": 0
                                },
                                "children": [
                                    {
                                        "query_type": "BooleanQuery",
                                        "lucene": "pushedtags:物 pushedtags:理",
                                        "time": "1683.194536ms",
                                        "breakdown": {
                                            "match": 0,
                                            "score": 585529731,
                                            "create_weight": 573065,
                                            "build_scorer": 1514674,
                                            "next_doc": 810859,
                                            "advance": 600919277
                                        },
                                        "children": [
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "pushedtags:物",
                                                "time": "245.7367070ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 121591065,
                                                    "create_weight": 351287,
                                                    "build_scorer": 1404515,
                                                    "next_doc": 132552,
                                                    "advance": 122257288
                                                }
                                            },
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "pushedtags:理",
                                                "time": "248.1102230ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 126919479,
                                                    "create_weight": 198412,
                                                    "build_scorer": 43552,
                                                    "next_doc": 108667,
                                                    "advance": 120840113
                                                }
                                            }
                                        ]
                                    },
                                    {
                                        "query_type": "ConstantScoreQuery",
                                        "lucene": "ConstantScore(_type:mc_alerts)",
                                        "time": "214.9125010ms",
                                        "breakdown": {
                                            "match": 0,
                                            "score": 0,
                                            "create_weight": 206460,
                                            "build_scorer": 124764,
                                            "next_doc": 162819144,
                                            "advance": 34772677
                                        },
                                        "children": [
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "_type:mc_alerts",
                                                "time": "16.98945600ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 0,
                                                    "create_weight": 175895,
                                                    "build_scorer": 52149,
                                                    "next_doc": 13207271,
                                                    "advance": 3554141
                                                }
                                            }
                                        ]
                                    }
                                ]
                            }
                        ],
                        "rewrite_time": 18509,
                        "collector": [
                            {
                                "name": "SimpleTopScoreDocCollector",
                                "reason": "search_top_hits",
                                "time": "1016.001227ms"
                            }
                        ]
                    }
                ]
            },
            {
                "id": "[jOgIfDtJShus0Q6s6mg_7Q][hubble-alarm-mysqlio][3]",
                "searches": [
                    {
                        "query": [
                            {
                                "query_type": "BooleanQuery",
                                "lucene": "+(pushedtags:物 pushedtags:理) #ConstantScore(_type:mc_alerts)",
                                "time": "3682.876232ms",
                                "breakdown": {
                                    "match": 0,
                                    "score": 792233023,
                                    "create_weight": 1032654,
                                    "build_scorer": 2759248,
                                    "next_doc": 1101475081,
                                    "advance": 0
                                },
                                "children": [
                                    {
                                        "query_type": "BooleanQuery",
                                        "lucene": "pushedtags:物 pushedtags:理",
                                        "time": "1604.326820ms",
                                        "breakdown": {
                                            "match": 0,
                                            "score": 570488622,
                                            "create_weight": 763015,
                                            "build_scorer": 2576342,
                                            "next_doc": 411552,
                                            "advance": 563624905
                                        },
                                        "children": [
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "pushedtags:物",
                                                "time": "234.4865370ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 118204042,
                                                    "create_weight": 467470,
                                                    "build_scorer": 2477660,
                                                    "next_doc": 95173,
                                                    "advance": 113242192
                                                }
                                            },
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "pushedtags:理",
                                                "time": "231.9758470ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 118929530,
                                                    "create_weight": 267978,
                                                    "build_scorer": 43827,
                                                    "next_doc": 81885,
                                                    "advance": 112652627
                                                }
                                            }
                                        ]
                                    },
                                    {
                                        "query_type": "ConstantScoreQuery",
                                        "lucene": "ConstantScore(_type:mc_alerts)",
                                        "time": "181.0494060ms",
                                        "breakdown": {
                                            "match": 0,
                                            "score": 0,
                                            "create_weight": 242757,
                                            "build_scorer": 113760,
                                            "next_doc": 138139699,
                                            "advance": 33744836
                                        },
                                        "children": [
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "_type:mc_alerts",
                                                "time": "8.808354000ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 0,
                                                    "create_weight": 233681,
                                                    "build_scorer": 52172,
                                                    "next_doc": 6367007,
                                                    "advance": 2155494
                                                }
                                            }
                                        ]
                                    }
                                ]
                            }
                        ],
                        "rewrite_time": 91562,
                        "collector": [
                            {
                                "name": "SimpleTopScoreDocCollector",
                                "reason": "search_top_hits",
                                "time": "996.2324610ms"
                            }
                        ]
                    }
                ]
            },
            {
                "id": "[w3ITy1o9RxypVxkDDhpQbg][hubble-alarm-mysqlio][4]",
                "searches": [
                    {
                        "query": [
                            {
                                "query_type": "BooleanQuery",
                                "lucene": "+(pushedtags:物 pushedtags:理) #ConstantScore(_type:mc_alerts)",
                                "time": "3556.433048ms",
                                "breakdown": {
                                    "match": 0,
                                    "score": 763042635,
                                    "create_weight": 1000035,
                                    "build_scorer": 1586874,
                                    "next_doc": 1065203649,
                                    "advance": 0
                                },
                                "children": [
                                    {
                                        "query_type": "BooleanQuery",
                                        "lucene": "pushedtags:物 pushedtags:理",
                                        "time": "1559.889749ms",
                                        "breakdown": {
                                            "match": 0,
                                            "score": 557857755,
                                            "create_weight": 755395,
                                            "build_scorer": 1415973,
                                            "next_doc": 2671477,
                                            "advance": 550704132
                                        },
                                        "children": [
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "pushedtags:物",
                                                "time": "224.9082080ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 112228265,
                                                    "create_weight": 476211,
                                                    "build_scorer": 1334724,
                                                    "next_doc": 532340,
                                                    "advance": 110336668
                                                }
                                            },
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "pushedtags:理",
                                                "time": "221.5768090ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 110845081,
                                                    "create_weight": 239724,
                                                    "build_scorer": 24242,
                                                    "next_doc": 524820,
                                                    "advance": 109942942
                                                }
                                            }
                                        ]
                                    },
                                    {
                                        "query_type": "ConstantScoreQuery",
                                        "lucene": "ConstantScore(_type:mc_alerts)",
                                        "time": "165.7101060ms",
                                        "breakdown": {
                                            "match": 0,
                                            "score": 0,
                                            "create_weight": 217248,
                                            "build_scorer": 85996,
                                            "next_doc": 120117974,
                                            "advance": 34194164
                                        },
                                        "children": [
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "_type:mc_alerts",
                                                "time": "11.09472400ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 0,
                                                    "create_weight": 207588,
                                                    "build_scorer": 28250,
                                                    "next_doc": 7746008,
                                                    "advance": 3112878
                                                }
                                            }
                                        ]
                                    }
                                ]
                            }
                        ],
                        "rewrite_time": 18021,
                        "collector": [
                            {
                                "name": "SimpleTopScoreDocCollector",
                                "reason": "search_top_hits",
                                "time": "965.1408870ms"
                            }
                        ]
                    }
                ]
            },
            {
                "id": "[w3ITy1o9RxypVxkDDhpQbg][hubble-alarm-mysqlio][1]",
                "searches": [
                    {
                        "query": [
                            {
                                "query_type": "BooleanQuery",
                                "lucene": "+(pushedtags:物 pushedtags:理) #ConstantScore(_type:mc_alerts)",
                                "time": "3539.066940ms",
                                "breakdown": {
                                    "match": 0,
                                    "score": 763629098,
                                    "create_weight": 843500,
                                    "build_scorer": 398678,
                                    "next_doc": 1057646326,
                                    "advance": 0
                                },
                                "children": [
                                    {
                                        "query_type": "BooleanQuery",
                                        "lucene": "pushedtags:物 pushedtags:理",
                                        "time": "1559.916723ms",
                                        "breakdown": {
                                            "match": 0,
                                            "score": 559143772,
                                            "create_weight": 655620,
                                            "build_scorer": 256005,
                                            "next_doc": 912877,
                                            "advance": 552146666
                                        },
                                        "children": [
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "pushedtags:物",
                                                "time": "224.7127090ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 114736073,
                                                    "create_weight": 373942,
                                                    "build_scorer": 133092,
                                                    "next_doc": 197072,
                                                    "advance": 109272530
                                                }
                                            },
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "pushedtags:理",
                                                "time": "222.0890740ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 110905539,
                                                    "create_weight": 205109,
                                                    "build_scorer": 26343,
                                                    "next_doc": 178561,
                                                    "advance": 110773522
                                                }
                                            }
                                        ]
                                    },
                                    {
                                        "query_type": "ConstantScoreQuery",
                                        "lucene": "ConstantScore(_type:mc_alerts)",
                                        "time": "156.6326150ms",
                                        "breakdown": {
                                            "match": 0,
                                            "score": 0,
                                            "create_weight": 161188,
                                            "build_scorer": 78703,
                                            "next_doc": 115532793,
                                            "advance": 32501439
                                        },
                                        "children": [
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "_type:mc_alerts",
                                                "time": "8.358492000ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 0,
                                                    "create_weight": 152201,
                                                    "build_scorer": 32647,
                                                    "next_doc": 6128062,
                                                    "advance": 2045582
                                                }
                                            }
                                        ]
                                    }
                                ]
                            }
                        ],
                        "rewrite_time": 18586,
                        "collector": [
                            {
                                "name": "SimpleTopScoreDocCollector",
                                "reason": "search_top_hits",
                                "time": "965.2964370ms"
                            }
                        ]
                    }
                ]
            },
            {
                "id": "[jOgIfDtJShus0Q6s6mg_7Q][hubble-alarm-mysqlio][2]",
                "searches": [
                    {
                        "query": [
                            {
                                "query_type": "BooleanQuery",
                                "lucene": "+(pushedtags:物 pushedtags:理) #ConstantScore(_type:mc_alerts)",
                                "time": "3644.109234ms",
                                "breakdown": {
                                    "match": 0,
                                    "score": 781631573,
                                    "create_weight": 793847,
                                    "build_scorer": 375744,
                                    "next_doc": 1091596980,
                                    "advance": 0
                                },
                                "children": [
                                    {
                                        "query_type": "BooleanQuery",
                                        "lucene": "pushedtags:物 pushedtags:理",
                                        "time": "1587.766916ms",
                                        "breakdown": {
                                            "match": 0,
                                            "score": 563435055,
                                            "create_weight": 542609,
                                            "build_scorer": 222933,
                                            "next_doc": 963418,
                                            "advance": 558358204
                                        },
                                        "children": [
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "pushedtags:物",
                                                "time": "236.6689340ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 122747874,
                                                    "create_weight": 339818,
                                                    "build_scorer": 149881,
                                                    "next_doc": 199749,
                                                    "advance": 113231612
                                                }
                                            },
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "pushedtags:理",
                                                "time": "227.5757630ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 114179424,
                                                    "create_weight": 177161,
                                                    "build_scorer": 25046,
                                                    "next_doc": 191028,
                                                    "advance": 113003104
                                                }
                                            }
                                        ]
                                    },
                                    {
                                        "query_type": "ConstantScoreQuery",
                                        "lucene": "ConstantScore(_type:mc_alerts)",
                                        "time": "181.9441740ms",
                                        "breakdown": {
                                            "match": 0,
                                            "score": 0,
                                            "create_weight": 186711,
                                            "build_scorer": 84753,
                                            "next_doc": 137896740,
                                            "advance": 33639941
                                        },
                                        "children": [
                                            {
                                                "query_type": "TermQuery",
                                                "lucene": "_type:mc_alerts",
                                                "time": "10.13602900ms",
                                                "breakdown": {
                                                    "match": 0,
                                                    "score": 0,
                                                    "create_weight": 177155,
                                                    "build_scorer": 31948,
                                                    "next_doc": 7394558,
                                                    "advance": 2532368
                                                }
                                            }
                                        ]
                                    }
                                ]
                            }
                        ],
                        "rewrite_time": 48078,
                        "collector": [
                            {
                                "name": "SimpleTopScoreDocCollector",
                                "reason": "search_top_hits",
                                "time": "981.7486840ms"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

三、查看类型的列mapping创建情况

输入:

URL:GET  /XXX/XXX/_mapping?pretty

输出:

{
    "hubble-alarm-mysqlio": {
        "mappings": {
            "mc_alerts": {
                "properties": {
                    "alertlevel": {
                        "type": "string"
                    },
                    "alertstatus": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "appId": {
                        "type": "integer"
                    },
                    "begindate": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"
                    },
                    "businessid": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "cmaggid": {
                        "type": "string"
                    },
                    "createdate": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"
                    },
                    "currentStep": {
                        "type": "integer"
                    },
                    "defaultType": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "endTime": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"
                    },
                    "enddate": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"
                    },
                    "endpoint": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "endpoint_counter": {
                        "type": "string"
                    },
                    "eventTime": {
                        "type": "long"
                    },
                    "eventid": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "expression": {
                        "type": "string"
                    },
                    "fuxi_status": {
                        "type": "integer"
                    },
                    "grp": {
                        "type": "string"
                    },
                    "handlemessage": {
                        "type": "string"
                    },
                    "handleprocess": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "handlestatus": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "host_status": {
                        "type": "long"
                    },
                    "host_uuid": {
                        "type": "string"
                    },
                    "id": {
                        "type": "integer"
                    },
                    "idc": {
                        "type": "string"
                    },
                    "init_level": {
                        "type": "string"
                    },
                    "isalarm": {
                        "type": "integer"
                    },
                    "isshake": {
                        "type": "string"
                    },
                    "isupgrade": {
                        "type": "string"
                    },
                    "itemid": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "itemkey": {
                        "type": "string"
                    },
                    "itemvalue": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "judge_address": {
                        "type": "string"
                    },
                    "lasthandle": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"
                    },
                    "leftvalue": {
                        "type": "string"
                    },
                    "leveles": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "message": {
                        "type": "string"
                    },
                    "metric": {
                        "type": "string"
                    },
                    "modifydate": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"
                    },
                    "needupgrade": {
                        "type": "string"
                    },
                    "node": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "pid": {
                        "type": "integer"
                    },
                    "proxyname": {
                        "type": "string"
                    },
                    "pushedtags": {
                        "type": "string"
                    },
                    "rightvalue": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "server": {
                        "type": "string"
                    },
                    "shakecount": {
                        "type": "integer"
                    },
                    "shaketime": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"
                    },
                    "source": {
                        "type": "string"
                    },
                    "status": {
                        "type": "string"
                    },
                    "strategy": {
                        "type": "string"
                    },
                    "strategyGrade": {
                        "type": "integer"
                    },
                    "strategy_id": {
                        "type": "integer"
                    },
                    "triggername": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "type": {
                        "type": "integer"
                    },
                    "userId": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "version": {
                        "type": "integer"
                    }
                }
            }
        }
    }
}

四、term的使用;

1.对于分词的,会将包含这个词的结果查出来;对于不分词的必须要输入完整的目标才能查出来;

url:GET /INDEX/TYPE/_search?pretty

输入:

{
    "profile": false,
    "query": {
        "bool": {
            "must": [
                {"term": {"grp": "operation"}}, //对于分词的,会将包含这个词的结果查出来;对于不分词的必须要输入完整的目标才能查
                {"term": {"init_level": "p3"}}  //只能用小写去查
            ],
            "must_not": {
                "term": {
                    "id": "26938727"
                }
            }
        }
    }
}

输出:

{
    "took": 16,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 685,
        "max_score": 6.19172,
        "hits": [
            {
                "_index": "xxx",
                "_type": "xxx",
                "_id": "26047832",
                "_score": 6.19172,
                "_source": {
                    "rightvalue": "2",
                    "endpoint": "read-operation-web-online001-bjlt.qiyi.virtual",
                    "isalarm": 1,
                    "eventTime": 1543992120000,
                    "grp": "read-operation",//由于grp分词后改为了read和operation两个单词,operation包含输入,因此被查出来了
                    "init_level": "P3",
                    "defaultType": "autook",
                    "metric": "mem.memfree.percent",
                    "userId": "c1f1b839e50148b1ab1b121490a946d1",
                    "modifydate": 1543992669000,
                    "enddate": 1543992660000
                }
            },
            {
                "_index": "xxx",
                "_type": "xxx",
                "_id": "26300330",
                "_score": 6.19172,
                "_source": {
                    "isalarm": 0,
                    "eventTime": 1544085300000,
                    "grp": "Operation-monitoring",
                    "init_level": "P3",
                    "defaultType": "basicserviceother",
                    "metric": "dnsmasq_resolved",
                    "userId": "2cffe92f4d7441719826611d9ee0d6ba",
                    "modifydate": 1544253796000,
                    "enddate": 1544253600000
                }
            }
        ]
    }
}

五、大小写问题;

输入(大写):

{
    "profile": false,
    "query": {
        "bool": {
            "must": {
                	"term": {
                		"init_level": "P3"
                		
                	}
            }
        }
    }
}

输出:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}

没有任何结果;再讲输入改为小写:

{
    "profile": false,
    "query": {
        "bool": {
            "must": {
                	"term": {
                		"init_level": "p3"
                		
                	}
            }
        }
    },
    "size": 1
}

输出:

{
    "took": 30,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 624136,
        "max_score": 3.535564,
        "hits": [
            {
                "_index": "hubble-alarm-mysqlio",
                "_type": "mc_alerts",
                "_id": "26108329",
                "_score": 3.535564,
                "_source": {
                    "endpoint_counter": "dnsmasq_resolved/type=check_resolved",
                    "createdate": 1544014149000,
                    "pid": -1,
                    "isalarm": 0,
                    "eventTime": 1544013900000,
                    "grp": "lingxu-allvm",
                    "init_level": "P3",
                    "defaultType": "n_ignore",
                    "metric": "dnsmasq_resolved",
                    "userId": "2cffe92f4d7441719826611d9ee0d6ba",
                    "modifydate": 1544072649000,
                    "enddate": 1544072400000
                }
            }
        ]
    }
}

以上现象大写查不出来,但是小写能查出来,是因为如果写入ES的是大写,搜索出来看到的结果也是大写,但是创建的索引却是小写(可以用分词验证)

验证参考第一大点_analyze案例,

可以看到,computecloud_kvm被解析成了小写,所以用大写没有匹配到,所以查不出来

六、ID生成

es生成id分两种,

  1. 若创建数据不指定id值,则会自动生成guid,即使指定id为integer也会生成guid
  2. 若指定id,则会使用给的id

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值