【从入门到精通,ElasticSearch】Linux版本

ElasticSearch

1.elasticsearch 概述

Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它能很方便的使大量数据具有搜索、分析和探索的能力。它提供可扩展的搜索,具有接近实时的搜索。ES本身扩展性很好,可以扩展到上百台服务器。ES也使用Java开发并使用Lucene作为核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTful API来隐藏Lucene的复杂性,从而让全文检索变得简单。

据国际权威的数据库产品评测机构DB Engines的统计,在2016年1月,ElasticSearch已经超过Solr等,成为排名第一的搜索引擎类应用!

2.elasticsearch环境

2.1.安装elasticsearch

下载地址:https://www.elastic.co/cn/downloads/elasticsearch

 # jdk1.8最低要求!elasticsearch支持客户端,界面工具!
 # Java开发,elasticsearch的版本和我们之后对应的Java的核心包!版本对应!JDK环境正常
 # 1、下载elasticsearch-7.8.0-linux-x86_64.tar.gz,然后解压到指定文件,就可以使用了!
 ​
 # 2、熟悉elasticsearch目录
 -bin                   # 启动文件 
 -config                # 配置文件
     log4j2              # 日志配置文件
     jvm.options         # JVM相关配置 如过内存小 修改一下JVM的配置
     elasticsearch.yml   # ElasticSearch配置文件  默认9200端口
 -lib                   # 相关jar
 -modules               # 功能模块
 -plugins               # 插件
 -logs                  # 日志
 ​
 # 3、启动elasticsearch之前的准备工作
 # 由于elasticsearch-7.X不能以Root启动elasticsearch,所以需要创建用户
 adduser Tangs # 添加用户
 passwd Tangs # 设置密码
 chown -R Tangs /opt/elasticsearch/elasticsearch-7.8.0/  # 修改文件用户权限!
 ​
 # 4、以新的用户到bin目录启动elasticsearch脚本
 [Tangs@localhost bin]$ ./elasticsearch
 ​
 # 5、测试连接
 elasticsearch监听的端口是 9100
 [root@localhost ~]# curl 127.0.0.1:9200
 {
   "name" : "localhost.localdomain",
   "cluster_name" : "elasticsearch",
   "cluster_uuid" : "cfeyNF5dSsC3aoCcbEPlj
   "version" : {
     "number" : "7.8.0",
     "build_flavor" : "default",
     "build_type" : "tar",
     "build_hash" : "757314695644ea9a1dc2f5",
     "build_date" : "2020-06-14T19:35:50.2
     "build_snapshot" : false,
     "lucene_version" : "8.5.1",
     "minimum_wire_compatibility_version" 
     "minimum_index_compatibility_version"
   },
   "tagline" : "You Know, for Search"
 }

2.2.安装elasticsearch-head

Elasticsearch-Head是一个开源的、基于Web的前端应用程序,它充当Elasticsearch集群的管理工具和可视化界面。它允许用户以图形化的方式直观地查看、管理和操作Elasticsearch索引、文档、集群健康状况、节点信息等,提供了比命令行更友好的交互体验。

下载地址:https://github.com/mobz/elasticsearch-head

Elasticsearch 在 5.0 版本后,Elasticsearch-head 插件需要作为独立服务进行安装,需要使用npm工具(NodeJS的包管理工具)安装。 安装 Elasticsearch-head 需要提前安装好依赖软件 node 和 phantomjs。 node:是一个基于 Chrome V8 引擎的 JavaScript 运行环境。 phantomjs:是一个基于 webkit 的JavaScriptAPI,可以理解为一个隐形的浏览器,任何基于 webkit 浏览器做的事情,它都可以做到。

 1,编译安装 node
 #上传软件包 node-v8.2.1.tar.gz 到/opt
 yum install gcc gcc-c++ make -y
 ​
 cd /opt
 tar zxvf node-v8.2.1.tar.gz
 ​
 cd node-v8.2.1/
 ./configure
 make && make install
 ​
 2,安装 phantomjs
 #上传软件包 phantomjs-2.1.1-linux-x86_64.tar.bz2 到
 cd /opt
 tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
 cd /opt/phantomjs-2.1.1-linux-x86_64/bin
 cp phantomjs /usr/local/bin
 ​
 3,安装 Elasticsearch-head 数据可视化工具
 #上传软件包 elasticsearch-head-master.zip 到/opt
 cd /opt
 unzip elasticsearch-head-master.zip
 cd /opt/elasticsearch-head-master/
 npm install      //安装依赖包
 ​
 4,修改 Elasticsearch 主配置文件
 vim /opt/elasticsearch/elasticsearch-7.8.0/config/elasticsearch.yml
 ......
 --末尾添加以下内容--
 http.cors.enabled: true             #开启跨域访问支持,默认为 false
 http.cors.allow-origin: "*"         #指定跨域访问允许的域名地址为所有
 ​
 5,启动 elasticsearch-head 服务
 #必须在解压后的 elasticsearch-head 目录下启动服务,进程会读取该目录下的 gruntfile.js 文件,否则可能启动失败。
 cd /opt/elasticsearch-head-master
 npm run start &
 ​
 > elasticsearch-head@0.0.0 start /usr/local/src/elasticsearch-head
 > grunt server
 ​
 Running "connect:server" (connect) task
 Waiting forever...
 Started connect web server on http://localhost:9100
 ​
 #elasticsearch-head 监听的端口是 9100
 netstat -natp |grep 9100
 ​
 6,通过 Elasticsearch-head 查看 Elasticsearch 信息
 通过浏览器访问 http://127.0.0.1:9100/ 地址并连接群集。如果看到群集健康值为 green 绿色,代表群集很健康。(单节点可能为黄色,)

2.3.安装kibana

下载地址:https://www.elastic.co/cn/downloads/kibana

Kibana的版本要和elasticsearch版本对应!

 1.安装 Kiabana
 #上传软件包 kibana-7.8.0-linux-x86_64.tar.gz 到/opt目录
 cd /opt
 tar xf kibana-7.8.0-linux-x86_64.tar.gz
 ​
 2.设置 Kibana 的主配置文件
 vim /opt/kibana-7.8.0-linux-x86_64config/kibana.yml
 --2--取消注释,Kiabana 服务的默认监听端口为5601
 server.port: 5601
 --7--取消注释,设置 Kiabana 的监听地址,0.0.0.0代表所有地址
 server.host: "0.0.0.0"
 --28--取消注释,配置es服务器的ip,如果是集群则配置该集群中master节点的ip
 elasticsearch.url:  ["http://192.168.80.10:9200","http://192.168.80.11:9200"] 
 --37--取消注释,设置在 elasticsearch 中添加.kibana索引
 kibana.index: ".kibana"
 --96--取消注释,配置kibana的日志文件路径(需手动创建),不然默认是messages里记录日志
 logging.dest: /var/log/kibana.log
 ​
 3.创建日志文件,启动 Kibana 服务
 touch /var/log/kibana.log
 useradd kibana
 chown kibana:kibana /var/log/kibana.log
 ​
 systemctl start kibana.service
 systemctl enable kibana.service
 ​
 4.验证 Kibana
 浏览器访问 http://ip:5601
 第一次登录需要添加一个 Elasticsearch 索引:
 Management -> Index Pattern -> Create index pattern
 Index pattern 输入:system-*   #在索引名中输入之前配置的 Output 前缀“system”
 ​
 Next step -> Time Filter field name 选择 @timestamp -> Create index pattern

3.ES核心概念

3.1.基本介绍

elasticsearch是面向文档的。

关系型数据库 和 elasticsearch客观对比!一切都是JSON!

Relational DB Elasticsearcg
数据库(database) 索引(index)
表(tables) types
行(rows documents
字段(columns) fields
   

elasticsearch(集群)中可以包含多个索引(数据库),每个索引中可以包含多个类型(表),每个类型下又包含多个文档(行),每个文档中又包含多个字段(列)。

物理设计:

elasticsearch在后台把每个索引划分成多个分片,每分分片可以在集群中的不同服务器之间迁移!

一个elasticsearch就是一个集群。默认的集群名称就是 elasticsearch

逻辑设计:

一个索引类型中,包含多个文档,比如说文档1,文档2。当我们索引一篇文档时,可以通过这样的一个序列找到它: 索引>类型>文档ID,通过这个组合我们就能索引到某个具体的文档。注意:ID不必是整数,实际上它是个字符串!

3.2.文档

文档就类比表中的一条条数据。

 user
 1     zangsan     18
 2      lisi       3

之前说 elasticsearch是面向文档的,那么就意味着索引和搜索数据的最小单位是文档, elasticsearch中,文档有几个重要的属性:

  • 自我包含,一篇文档同时包含字段和对应的值,也就是同时包含 key-value

  • 可以是层次型的,一个文档中包含自文档,复杂的逻辑实体就是这么来的!

  • 灵活的结构,文档不依赖预先定义的模式,我们知道关系型数据库中,要提前定义字段才能使用,在 elasticsearch中,对于字段是非常灵活的,有时候,我们可以忽略该字段,或者动态的添加一个新的字段。

3.3.类型

类型是文档的逻辑容器,就像关系型数据库一样,表格是行的容器。类型中对于字段的定义成为映射,比如 name映射为字符串类型。我们说文档是无模式的,他们不需要拥有映射中所定义的所有字段,比如新增一个字段,那么 elasticsearch是怎么做的呢? elasticsearch会自动的将新字段加入映射,但是这个字段的不确定它是什么类型, elasticsearch就开始猜,如果这个值是18,那么 elasticsearch就会认为它是整型,但是 elasticsearch也可能猜不对,所以最安全的办法的是提前定义好所需要的映射,这点跟关系型数据库殊途同归了,先是定义好字段,然后再使用。

3.4.索引

索引就类比数据库!

索引是映射类型的容器, elasticsearch中的索引是一个非常大的文档集合。索引存储了映射类型的字段和其他设置。然后它们被存储到了各个分片上。我们来研究下分片是如何工作的。

物理设计:节点和分片 如何工作

一个集群至少有一个节点,而一个节点就是一个 elasticsearch进程,节点可以有多个索引,如果创建索引,那么默认索引将会有5个分片( primary shard,又称主分片)构成,每一个主分片会有一个副本( replica shard,又称复制分片)。

image-20240606101410243

上图是一个有3个节点的集群,可以直接看到主分片[P]和对应的复制分片[R]都不会在同一个节点内,这样有利于某个节点挂掉了,数据也不会丢失。实际上,一个分片是一个 Lucene索引,一个包含倒排索引的文件目录,倒排索引的结构使得 elasticsearch在不扫描全部文档的情况下,就能告诉你哪些文档包含特定的关键字。不过,倒排索引是什么?

3.5.倒排索引

 倒排索引基本介绍

elasticsearch使用的是一种称为倒排索引的结构,采用 Lucene倒排索引作为底层。这种结构适用于快速的全文检索,一个索引由文档中所有不重复的列表构成,对于每一个词,都有一个包含它的文档列表。例如,现在有两个文档,每个文档包含如下内容:

 Study every day,good good up to forever # 文档1包含的内容
 To forever,study every day, good good up # 文档2包含的内容

为了创建倒排索引,我们首先要将每个文档拆分成独立的词(或称为词条或者tokens),然后创建一个包含所有不重复的词条的排序列表,然后列出每个词条出现在哪个文档!

term doc.1 doc.2
Study ×
To ×
every
forever
day
study ×
good
every v
to ×
up

现在我们试图搜索to forever,只需要查看包含每个词条的文档

term doc.1 doc.2
to ×
forever
total 2 1

两个文档都匹配,但是第一个文档比第二个匹配程度更高。如果没有别的条件,现在,这两个包含关键字的都将返回。

 创建倒排索引步骤

1、创建文档列表: Lucene首先对原始文档数据进行编号,形成列表,就是一个文档列表。

image-20240606102609498

2、创建倒排索引列表:対原始文档中的数据进行分词,得到词条。対词条进行编号,以词条创建索引。然后记录下包含该词条的所有文档编号及其他信息。

搜索的过程:

当用户输入任意的词条时,首先对用户输入的数据进行分词,得到用户要搜索的所有词条,然后拿着这些词条去倒排索引列表中进行匹配。找到这些词条就能找到包含这些词条的所有文档的编号。

然后根据这些编号去文档列表中找到文档。

4.IK分词器插件

4.1.什么是IK分词器?

分词:即把一段中文或者别的划分为一个个的关键字,我们在搜索时候会把自己的信息进行分词,会把数据库中或者索引库中的数据进行分词,然后进行一个匹配操作,默认的中文分词是将每个字看成一个词,比如"我喜欢你"会被分为"我","喜","欢","你",这显然是不符合要求的,所以我们需要安装中文分词器ik来解决这个问题。

如果要使用中文,建议使用ik分词器。

IK提供了两个分词算法: ik_smartik_max_word,其中 ik_smart为最少切分, ik_max_word为最细粒度划分!

4.2.安装IK分词器插件

压缩文件下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases

 # 注意IK要和ES版本一直
 #1、注意:替换为您自己的 elasticsearch 版本7.8.0
 ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.8.0/elasticsearch-analysis-ik-7.8.0.zip
 ​
 # 2、查看IK是否安装成功
 [root@localhost bin]# ./bin/elasticsearch-plugin list
 analysis-ik
 ​
 3.重启 Elasticsearch

简单示例

1.创建索引

 curl -XPUT http://localhost:9200/index

2.创建映射

 curl -XPOST http://localhost:9200/index/_mapping -H 'Content-Type:application/json' -d'
 {
         "properties": {
             "content": {
                 "type": "text",
                 "analyzer": "ik_max_word",
                 "search_analyzer": "ik_smart"
             }
         }
 ​
 }'

3.索引部分文档

 curl -XPOST http://localhost:9200/index/_create/1 -H 'Content-Type:application/json' -d'
 {"content":"美国留给伊拉克的是个烂摊子吗"}
 '
 ​
 curl -XPOST http://localhost:9200/index/_create/2 -H 'Content-Type:application/json' -d'
 {"content":"公安部:各地校车将享最高路权"}
 '
 ​
 curl -XPOST http://localhost:9200/index/_create/3 -H 'Content-Type:application/json' -d'
 {"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
 '
 ​
 curl -XPOST http://localhost:9200/index/_create/4 -H 'Content-Type:application/json' -d'
 {"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
 '

4.突出显示查询

 curl -XPOST http://localhost:9200/index/_search  -H 'Content-Type:application/json' -d'
 {
     "query" : { "match" : { "content" : "中国" }},
     "highlight" : {
         "pre_tags" : ["<tag1>", "<tag2>"],
         "post_tags" : ["</tag1>", "</tag2>"],
         "fields" : {
             "content" : {}
         }
     }
 }
 '

结果

 {
     "took": 14,
     "timed_out": false,
     "_shards": {
         "total": 5,
         "successful": 5,
         "failed": 0
     },
     "hits": {
         "total": 2,
         "max_score": 2,
         "hits": [
             {
                 "_index": "index",
                 "_type": "fulltext",
                 "_id": "4",
                 "_score": 2,
                 "_source": {
                     "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
                 },
                 "highlight": {
                     "content": [
                         "<tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 "
                     ]
                 }
             },
             {
                 "_index": "index",
                 "_type": "fulltext",
                 "_id": "3",
                 "_score": 2,
                 "_source": {
                     "content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
                 },
                 "highlight": {
                     "content": [
                         "均每天扣1艘<tag1>中国</tag1>渔船 "
                     ]
                 }
             }
         ]
     }
 }

 

4.3.查看不同的分词器

ik_smart最少切分

POST /_analyze
{
  "analyzer": "ik_smart",
  "text": "我好喜欢你"
}

image-20240606110351299

 

ik_max_word为最细粒度划分!穷尽所有可能!

POST /_analyze
{
  "analyzer": "ik_max_word",
  "text": "我好喜欢你"
}

image-20240606110316830

4.3.自定义字典/热更新

IKAnalyzer.cfg.xml可以位于 或{conf}/analysis-ik/config/IKAnalyzer.cfg.xml``{plugins}/elasticsearch-analysis-ik-*/config/IKAnalyzer.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<comment>IK Analyzer 扩展配置</comment>
	<!--用户可以在这里配置自己的扩展字典 -->
	<entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic</entry>
	 <!--用户可以在这里配置自己的扩展停止词字典-->
	<entry key="ext_stopwords">custom/ext_stopword.dic</entry>
 	<!--用户可以在这里配置远程扩展字典 -->
	<entry key="remote_ext_dict">location</entry>
 	<!--用户可以在这里配置远程扩展停止词字典-->
	<entry key="remote_ext_stopwords">http://xxx.com/xxx.dic</entry>
</properties>

测试

 # 1、没有增加自己定义的字典之前
 # 测试样例
 GET _analyze
 {  
 "analyzer": "ik_smart",
 "text": "棠时"
 }
 # 结果
 {  
 "tokens": [    
 {     
 "token": "棠",      
 "start_offset": 0,      
 "end_offset": 1,      
 "type" : "CN_CHAR",      
 "position" : 0    
 },    
 {    
 "token" : "时",      
 "start_offset" : 1,     
 "end_offset": 2,      
 "type": "CN_CHAR",      
 "position" :1   
 }  ]}
 # 2、增加自己自定义的词典并重启elasticsearch之后
 # 测试样例
 GET _analyze
 {  
 "analyzer": "ik_smart",  
 "text": "棠时"
 }
 # 结果
 { 
 "tokens" :
 [   
 {     
 "token":"棠时",     
 "start_offset" :0,     
 "end_offset":2,     
 "type" : "CN_WORD",      
 "position" : 0   
 }  ]}

5.关于索引的基本操作

5.1.Rest模板

method url地址 描述
PUT localhost:9200/索引名称/类型名称/文档id 创建文档(指定文档id)
POST localhost:9200/索引名称/类型名称 创建文档(随机文档id)
POST localhost:9200/索引名称/类型名称/文档id/_update 修改文档
DELETE localhost:9200/索引名称/类型名称/文档id 删除文档
GET localhost:9200/索引名称/类型名称/文档id 通过文档id查询文档
POST localhost:9200/索引名称/类型名称/_search 查询所有数据

5.2.添加索引

当然不是只有 kibana可以测试,使用其他软件如 Postman或者T alend APTTester都可以。

 #使用Postman测试 
 # 基本语法
 POST请求 http://localhost:9200/索引名/~类型名~/文档id
  {请求体}
  # 1、测试创建索引(添加了文档)
  POST请求 http://localhost:9200/test1/_doc/1
  curl -XPOST "localhost:9200/test/_doc/1" 
  '{
   "name":"Rindo",
   "age": 3
 }'
 ​
 # 2、测试创建索引规则(不添加文档)
 PUT请求 http://localhost:9200/test2
 {
   "mappings": {
     "properties": {
       "name": {
         "type": "text"
       },
       "age": {
         "type": "long"
       },
       "birthday": {
         "type": "date"
       }
     }
   }
 }
 ​
 # 3、获取索引具体的信息
 GET请求 http://localhost:9200/test2

5.3.查看索引默认信息

  # 1、创建索引并添加数据
 POST请求 http://39.97.3.60:9200/test3/_doc/1
 {
 "name":"Ringo",
 "age": 18,
 "birth": "1997-11-13"
 }'
 ​
 # 2、我们自己没有为索引写mapping映射查看索引默认的信息
 GET请求 http://39.97.3.60:9200/test3
 # 返回的结果,ES给我们的字段自动加上了类型
 如果自己的文档字段没有指定类型,那么 elasticsearch就会给我们默认配置字段类型!
 {
   "test3" : {
     "aliases" : { },
     "mappings" : {
       "properties" : {
         "age" : {
           "type" : "long"
         },
         "birth" : {
           "type" : "date"
         },
         "name" : {
           "type" : "text",
           "fields" : {
             "keyword" : {
               "type" : "keyword",
               "ignore_above" : 256
             }
           }
         }
       }
     },
     "settings" : {
       "index" : {
         "creation_date" : "1717668184668",
         "number_of_shards" : "1",
         "number_of_replicas" : "1",
         "uuid" : "Hf49A0fOQZqbrd72AvLKug",
         "version" : {
           "created" : "7080099"
         },
         "provided_name" : "test3"
       }
     }
   }

5.4.扩展命令

 # 1、查看ElasticSearch健康状态
 GET请求 http://localhost:9200/_cat/health
 ​
 # 2、查看ElasticSearch详细信息
 GET请求 http://localhost:9200/_cat/indices?v

5.5.修改索引

 # 方式一:修改文档
 PUT请求 http://localhost:9200/test3/_doc/1
  {
  "name": "Ringo",
  "age": 18,
  "birth": "1997-11-13"
  }'
 # 返回结果
 {"_index":"test3","_type":"_doc","_id":"1","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}
 ​
 # 方式二:修改文档
 POST请求 http://localhost:9200/test3/_doc/1/_update
 {
   "doc": {
     "name": "Ringo",
     "age": 19,
     "birth": "1977-11-13"
   }
 }
 # 返回结果
 {
   "_index" : "test3",
   "_type" : "_doc",
   "_id" : "1",
   "_version" : 3,
   "result" : "updated",
   "_shards" : {
     "total" : 2,
     "successful" : 1,
     "failed" : 0
   },
   "_seq_no" : 2,
   "_primary_term" : 1
 }

5.6.删除索引

# 1、删除索引
DELETE请求 http://localhost:9200/test1
# 2、删除文档
DELETE
请求 http://localhost:9200/test3/_doc/1

6.关于文档的基本操作

6.1.基本操作

# 1、创建文档
POST请求 http://localhost:9200/ringo/_doc/1
{
 "name": "RingoTangs",
 "age": 18,
 "describe": "铃铃铃",
 "tags": ["阳光","喜欢篮球","喜欢学习"]
}

# 2、获取数据
GET请求 http://localhost:9200/ringo/_doc/1

# 3、更新数据,PUT如果不写全字段就会被覆盖
PUT请求 http://localhost:9200/ringo/_doc/1
{
  "name": "李四233",
  "age": 30,
  "describe": "法外狂徒,李四",
  "tags": ["靓仔","喜欢唱歌","喜欢学习"]
}

# 4、推荐使用POST来更新,自由度很高,携带的JSON可以只写修改字段
POST请求 http://localhost:9200/ringo/_doc/1/_update
 {
  "doc": {
    "name": "李四999"
  }
}

6.2.简单查询

# 1、简单的条件查询
GET请求 http://localhost:9200/ringo/_search?q=name:李四
GET请求 http://localhost:9200/ringo/_search?q=tags:靓仔

6.3.复杂查询

 # 1、带上查询条件,match只要名字中有"张三"的都会被检索出来
 POST请求 http://localhost:9200/ringo/_search
 {
   "query": {
     "match": {
       "name": "李四"
     }
   }
 }
 ​
 # 2、查询结果返回具体的字段,使用"_source"
 POST请求 http://localhost:9200/ringo/_search
 {
   "query": {
     "match": {
       "name": "李四"
     }
   },
   "_source": ["name","age"]
 }
 ​
 # 3、查询结果排序,使用"sort",通过某个字段进行排序
 POST请求 http://localhost:9200/ringo/_search
 {
   "query": {
     "match": {
       "name": "李四"
     }
   },
   "_source": ["name","age"],
   "sort": [
     {
       "age": {
         "order": "asc"
       }
     }
   ]
 }
 ​
 # 4、分页查询 "from"从哪里开始,“size"每页显示几条数据
 POST请求 http://localhost:9200/ringo/_search
 {
   "query": {
     "match": {
       "name": "李四"
     }
   },
   "_source": ["name","age"],
   "from": 0,
   "size": 1
 }
 ​
 # 5、通过"bool"和"must"组合使用,可以多条件组合查询,等价于and
 # 会把name="李四"和age=30的文档查出来
 POST请求 http://localhost:9200/ringo/_search
 {
   "query": {
     "bool": {
       "must": [{
         "match": {
          "name":"李四"
         }
       },{
         "match": {
           "age": "30"
         }
       }
       ]
     }
   }
 }
 ​
 # 4、"should"有一个条件符合即可,等价于or
 # 会把name="李四"或者age=18的文档查出来
 POST请求 http://localhost:9200/ringo/_searc
 POST /ringo/_search
 {
   "query": {
     "bool": {
       "should": [
         {
           "match": {
             "name": "李四"
           }
         },{
           "match": {
             "age": "18"
           }
         }
       ]
     }
   }
 }
 ​
 # 5、"must_not"查询年龄不是18岁的人
 POST请求 http://localhost:9200/ringo/_searc
 POST ringo/_search
 {
   "query": {
     "bool": {
       "must_not": [
         {
           "match": {
             "age": "18"
           }
         }
       ]
     }
   }
 }
 ​
 # 6、查询结果过滤,范围查询
 # gt:大于
 # gte:大于等于
 # lt:小于
 # lte:小于等
 POST请求 http://localhost:9200/ringo/_searc
 {
   "query": {
     "bool": {
       "must": [
         {
           "match": {
             "name": "李四"
           }
         }
       ],
       "filter": 
         {
           "range": {
             "age": {
               "gt": 19
             }
           }
         }
       
     }
   }
 }
 ​
 # 7、多条件使用空格隔开,只要满足其中一个结果就可以被查出
 POST请求 http://localhost:9200/ringo/_searc
 {
   "query": {
     "match": {
       "tags": "喜欢 阳光"
     }
   }
 }
 ​
 # 8、精确查询term  "term"输入的词不会被分词,"match"会使用分词器解析
 # term查询是直接通过倒排索引指定的词条进行精确查找的
 # 注意:keyword类型的字段不会被分词器解析!!!
 PUT /testdb
 {
   "mappings": {
     "properties": {
       "name": {
         "type": "text"   # text类型会走分词器
         },
         "describe": {
            "type": "keyword"  # keyword不会走分词器,当成一个整体      
       }    
     } 
   }
 }
 GET /testdb/_search
 {
   "query": {
     "term": {
       "describe": "Ringo 每天都要好好学习    
 } 
 }
 }
 ​
 # 9、高亮查询
 # 测试样例
 GET /testdb/_search
 {
   "query": {
     "match": {
       "name": "棠时"
     }
   },
   
 "highlight": {
     "pre_tags": "<p class='key' style='color:red'>", 
     "post_tags": "</p>", 
     "fields": {      
     "name": {}   
 }  
 }
 }
 # 结果
 "highlight" : {
      "name" : [         
         "<p class='key' style='color:red'>棠</p><p class='key' style='color:red'>时</p>每天都要开心"
 ]
 }
posted @ 2024-06-07 12:00  旧巷g  阅读(36)  评论(0)    收藏  举报