Elasticsearch原始API基本操作
索引库操作
实际开发中,有五种方式操作Elasticsearch服务:
- 第一类:发送http请求(RESTful风格)操作:9200端口
- 使用Postman发送请求直接操作。
- 使用ElasticSearch-head-master图形化界面插件操作
- 使用Elastic官方数据可视化的平台Kibana进行操作
- 第二类:通过Java代码操作:9300端口
- Elasticsearch提供的Java API 客户端进行操作。
- Spring Data ElasticSearch 持久层框架进行操作。
Elasticsearch采用Rest风格API,因此其API就是一次http请求,你可以用任何工具发起http请求
创建索引库
发送请求:
# 在kibana中,不用写地址和端口,/ahu是简化写法,真实请求地址是:http://127.0.0.1:9200/ahu
# 请求方法:PUT
PUT /ahu
响应结果:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "ahu"
}
“acknowledged” : true, 代表操作成功
“shards_acknowledged” : true, 代表分片操作成功
“index” : “ahu” 表示创建的索引库名称
注意:创建索引库的分片数默认5片,在7.0.0之后的ElasticSearch版本中,默认分片数变为1片;
手动发请求亦可
查看索引库
发送请求:
# 请求方法GET
GET /ahu
响应结果:
{
"ahu" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1608112985428",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "-Ul8MH8rTye7g32syN1vZg",
"version" : {
"created" : "6080099"
},
"provided_name" : "ahu"
}
}
}
}
响应内容解释:
{
"ahu【索引库名】" : {
"aliases【别名】" : { },
"mappings【映射】" : { },
"settings【索引库设置】" : {
"index【索引】" : {
"creation_date【创建时间】" : "1608112985428",
"number_of_shards【索引库分片数】" : "5",
"number_of_replicas【索引库副本数】" : "1",
"uuid【唯一标识】" : "6Ffe20CIT76KchAcvqE6NA",
"version【版本】" : {
"created" : "6080099"
},
"provided_name【索引库名称】" : "ahu"
}
}
}
}
删除索引库
发送请求:
# 请求方法:DELETE
DELETE /ahu
响应结果:
{
"acknowledged" : true
}
类型(type)及映射(mapping)操作
有了索引库
,等于有了数据库中的database
。接下来就需要索引库中的类型
了,也就是数据库中的表
。创建数据库表需要设置字段约束,索引库也一样,在创建索引库的类型时,需要知道这个类型下有哪些字段,每个字段有哪些约束信息,这就叫做映射(mapping)
1.配置映射
给ahu这个索引库添加了一个名为goods
的类型,并且在类型中设置了4个字段:
- title:商品标题
- subtitle: 商品子标题
- images:商品图片
- price:商品价格
发送请求:
PUT /ahu/goods/_mapping
{
"properties": {
"title":{
"type":"text",
"analyzer": "ik_max_word",
"index": true,
"store": false
},
"subtitle":{
"type":"text",
"analyzer": "ik_max_word",
"index": true,
"store": false
},
"images":{
"type": "keyword",
"index": false,
"store": false
},
"price":{
"type":"double",
"index": true,
"store": false
}
}
}
响应结果:
{
"acknowledged" : true
}
内容解释:
PUT /索引库名/_mapping/类型名称 或 索引库名/类型名称/_mapping
{
"properties": {
"字段名称":{
"type【类型】": "类型",
"index【是否索引】": true,
"store【是否存储】": true,
"analyzer【分析器】": "分词器"
}
...
}
}
类型名称:就是前面将的type的概念,类似于数据库中的表
字段名:任意填写,下面指定许多属性,例如:
- type:类型,Elasticsearch中支持的数据类型非常丰富,几个关键的如下:
-
String类型,又分两种:
- text:可分词
- keyword:不可分词,数据会作为完整字段进行匹配
-
Numerical:数值类型,分两类
- 基本数据类型:long、interger、short、byte、double、float、half_float
- 浮点数的高精度类型:scaled_float
-
Date:日期类型
-
Array:数组类型
-
Object:对象
-
- index:是否索引,默认为true,如果不进行任何配置,所有字段都会被索引。
- true:字段会被索引,则可以用来进行搜索。默认值就是true
- false:字段不会被索引,不能用来搜索
- store:是否将数据进行独立存储,默认为false
- 原始的文本会存储在
_source
里面,默认情况下其他提取出来的字段都不是独立存储的,是从_source
里面提取出来的。当然你也可以独立的存储某个字段,只要设置store:true即可,获取独立存储的字段要比从_source中解析快得多,但是也会占用更多的空间,所以要根据实际业务需求来设置,默认为false。
- 原始的文本会存储在
- analyzer:分词器,这里的
ik_max_word
即使用ik分词器
2.查看映射
GET /ahu/goods/_mapping
创建索引和映射
PUT /ahu1
{
"settings": {},
"mappings": {
"goods":{
"properties": {
"title":{
"type":"text",
"analyzer": "ik_max_word",
"index": true,
"store": false
},
"subtitle":{
"type":"text",
"analyzer": "ik_max_word",
"index": true,
"store": false
},
"images":{
"type": "keyword",
"index": false,
"store": false
},
"price":{
"type":"double",
"index": true,
"store": false
}
}
}
}
}
文档操作
文档,即索引库中某个类型下的数据,会根据规则创建索引,将来用来搜索。可以类比做数据库中的每一行数据。
1.新增文档
发送请求:
POST /ahu/goods/
{
"title":"小米手机",
"price":2500,
"images":"http://baidu.com/img/1"
}
响应结果:
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "N_L6a3YBIwQ7y5uMafdB",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
响应结果解析:
{
"_index【索引库】" : "ahu",
"_type【类型】" : "goods",
"_id【主键id】" : "N_L6a3YBIwQ7y5uMafdB",
"_version【版本】" : 1,
"result【操作结果】" : "created",
"_shards【分片】" : {
"total【总数】" : 2,
"successful【成功】" : 1,
"failed【失败】" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
可以看到结果显示为:created
,是创建成功了。
另外,需要注意的是,在响应结果中有个_id
字段,这个就是这条文档数据的唯一标示
,以后的增删改查都依赖这个id作为唯一标示。可以看到id的值为:N_L6a3YBIwQ7y5uMafdB,这里我们新增时没有指定id,所以是ES帮我们随机生成的id。
2.查看文档
根据rest风格,新增是put,查询是get(post也可以用来做查询),不过查询一般都需要条件,这里我们把刚刚生成数据的id带上。
发送请求:
GET /ahu/goods/O_IDbHYBIwQ7y5uMN_dc
响应结果:
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "O_IDbHYBIwQ7y5uMN_dc",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "小米手机",
"price" : 2500,
"images" : "http://baidu.com/img/1"
}
}
响应结果解析:
{
"_index【索引库】" : "ahu",
"_type【类型】" : "goods",
"_id【主键id】" : "O_IDbHYBIwQ7y5uMN_dc",
"_version【版本】" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found【查询结果】" : true,
"_source【源文档信息】" : {
"title" : "小米手机",
"images" : "http://baidu.com/img/1",
"price" : 2500
}
}
_source
:源文档信息,所有的数据都在里面。_id
:这条文档的唯一标示- found:查询结果,返回true代表查到,false代表没有
3.自定义id新增文档
发送请求:
POST /ahu/goods/1
{
"title":"小米手机",
"price":2500,
"images":"http://baidu.com/img/1"
}
响应结果:
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
主键id变为指定的id
请求内容解析:
POST /ahu/goods/{自定义注解id}
{
"title":"小米手机",
"images":"http://baidu.com/img/1",
"price":2500
}
4.修改文档
新增时,主键不变则会将原有内容覆盖。操作一次数据version字段都会增加,可以达成版本锁定的效果
发送请求:
POST /ahu/goods/1
{
"title":"红米手机",
"price":1500,
"images":"http://baidu.com/img/1"
}
响应结果:
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
可以看到result结果是:updated
,显然是更新数据
5.删除文档
1、删除一条
删除一个文档也不会立即从磁盘上移除,它只是被标记成已删除。Elasticsearch将会在你之后添加更多索引的时候才会在后台进行删除内容的清理。
发送请求:
DELETE ahu/goods/1
响应结果:
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "1",
"_version" : 3,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
可以看到result结果是:deleted,数据被删除。如果删除不存在的问题,result:not_found
2、根据条件删除:
发送请求:
POST /ahu/_delete_by_query
{
"query":{
"match":{
"title":"小米"
}
}
}
响应结果:
{
"took" : 125,
"timed_out" : false,
"total" : 3,
"deleted" : 3,
"batches" : 1,
"version_conflicts" : 0,
"noops" : 0,
"retries" : {
"bulk" : 0,
"search" : 0
},
"throttled_millis" : 0,
"requests_per_second" : -1.0,
"throttled_until_millis" : 0,
"failures" : [ ]
}
响应结果解析:
{
"took【耗时】" : 58,
"timed_out" : false,
"total【总数】" : 2,
"deleted【删除总数】" : 2,
"batches" : 1,
"version_conflicts" : 0,
"noops" : 0,
"retries" : {
"bulk" : 0,
"search" : 0
},
"throttled_millis" : 0,
"requests_per_second" : -1.0,
"throttled_until_millis" : 0,
"failures" : [ ]
}
6.发送请求批量操作_bulk
Bulk 操作是将文档的增删改查一些列操作,通过一次请求全都做完。减少网络传输次数。相当于,将多个新增、修改、删除的请求写到一次请求当中。
注意:bulk的请求体与其他的请求体稍有不同!
请求语法:
POST /ahu/goods/_bulk
{ action: { metadata }}\n
{ request body }\n
{ action: { metadata }}\n
{ request body }\n
...
语法解析:
- 每行一定要以换行符(\n)结尾,包括最后一行
- action/metadata 部分,指定做什么操作
- action代表操作的动作,必须是如下的动作之一
- create:如果文档不存在,那么就创建
- index:创建一个新的文档或者替换现有文档
- update:部分更新文档
- delete:删除一个文档,这种操作不带请求体
- metadata,是文档的元数据,包括索引(
_index
),类型(_type
),id(_id
)…等
- action代表操作的动作,必须是如下的动作之一
- request body 请求体,正常的新增文档的请求体内容(注意,不要带换行符)
隔离:每个操作互不影响。操作失败的行会返回其失败信息。
实际用法:bulk请求一次不要太大,否则积压到内存中,性能会下降。所以,一次请求几千个操作、大小控制在5M-15M之间正好。
发送请求:
POST /ahu/goods/_bulk
{"index":{"_index" : "ahu","_type" : "goods"}}
{"title":"大米手机","images":"http://baidu.com/12479122.jpg","price":3288}
{"index":{"_index" : "ahu","_type" : "goods"}}
{"title":"小米手机","images":"http://baidu.com/12479122.jpg","price":2699}
{"index":{"_index" : "ahu","_type" : "goods"}}
{"title":"小米电视4A","images":"http://baidu.com/12479122.jpg","price":4288}
{"index":{"_index" : "ahu","_type" : "goods"}}
{"title": "华为手机","images": "http://baidu.com/12479122.jpg","price": 5288,"subtitle": "小米"}
{"index":{"_index" : "ahu","_type" : "goods"}}
{"title":"apple手机","images":"http://baidu.com/12479122.jpg","price":5899.00}
注意:
- 请求体的内容不要换行
- 请注意 delete 动作不能有请求体
- 谨记最后一个换行符不要落下。
响应结果:
{
"took" : 41,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "ahu",
"_type" : "goods",
"_id" : "P_JRbHYBIwQ7y5uMq_dC",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
}
...
]
}
每个子请求都是独立执行,因此某个子请求的失败不会对其他子请求的成功与否造成影响。 如果其中任何子请求失败,最顶层的 error 标志被设置为 true ,并且在相应的请求报告出错误明细。
status属性:代表响应状态码
执行删除所有数据
POST /ahu/goods/_delete_by_query
{
"query":{
"match_all":{
}
}
}
响应结果
{
"took" : 32,
"timed_out" : false,
"total" : 10,
"deleted" : 10,
"batches" : 1,
"version_conflicts" : 0,
"noops" : 0,
"retries" : {
"bulk" : 0,
"search" : 0
},
"throttled_millis" : 0,
"requests_per_second" : -1.0,
"throttled_until_millis" : 0,
"failures" : [ ]
}
基本查询
1、查询所有(match_all)
发送请求:
POST /ahu/goods/_search
{
"query": {
"match_all": {}
}
}
请求内容解析:
请求方法:POST
请求地址:http://127.0.0.1:9200/索引库名/_search
POST /{索引库}/_search
{
"query":{
"查询类型":{
"查询条件":"查询条件值"
}
}
}
这里的query代表一个查询对象,里面可以有不同的查询属性
- 查询类型:
- 例如:
match_all(代表查询所有)
,match
,term
,range
等等
- 例如:
- 查询条件:查询条件会根据类型的不同,写法也有差异
响应结果
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 1.0,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HrCdbnYByd2OmNcmILNa",
"_score" : 1.0,
"_source" : {
"title" : "apple手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 5899.0
}
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "GrCdbnYByd2OmNcmILNZ",
"_score" : 1.0,
"_source" : {
"title" : "大米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 3288
}
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "G7CdbnYByd2OmNcmILNa",
"_score" : 1.0,
"_source" : {
"title" : "小米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 2699
}
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HLCdbnYByd2OmNcmILNa",
"_score" : 1.0,
"_source" : {
"title" : "小米电视4A",
"images" : "http://baidu.com/12479122.jpg",
"price" : 4288
}
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HbCdbnYByd2OmNcmILNa",
"_score" : 1.0,
"_source" : {
"title" : "华为手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 5288,
"subtitle" : "小米"
}
}
]
}
}
响应结果解析:
{
"took【查询花费时间,单位毫秒】" : 1,
"timed_out【是否超时】" : false,
"_shards【分片信息】" : {
"total【总数】" : 5,
"successful【成功】" : 5,
"skipped【忽略】" : 0,
"failed【失败】" : 0
},
"hits【搜索命中结果】" : {
"total【命中总数】" : 3,
"max_score【所有查询结果中,文档的最高得分】" : 1.0,
"hits【命中结果集合】" : [
{
"_index【索引库】" : "ahu",
"_type【类型】" : "goods",
"_id【主键】" : "ADWoZ24Bx8DA1HO-R9DD",
"_score【当前结果匹配得分】" : 1.0,
"_source【源文档信息】" : {
"title" : "小米电视4A",
"images" : "http://baidu.com/12479122.jpg",
"price" : 4288
}
}...}]}}
2、匹配查询(match)
match
类型查询,带分词的查询,会把查询条件进行分词,然后进行查询,多个词条之间是or的关系
发送请求:
POST /ahu/goods/_search
{
"query": {
"match": {
"title":"小米手机"
}
}
}
响应结果:
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 1.0470967,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "G7CdbnYByd2OmNcmILNa",
"_score" : 1.0470967,
"_source" : {
"title" : "小米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 2699
}
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HbCdbnYByd2OmNcmILNa",
"_score" : 0.52354836,
"_source" : {
"title" : "华为手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 5288,
"subtitle" : "小米"
}
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HLCdbnYByd2OmNcmILNa",
"_score" : 0.3901917,
"_source" : {
"title" : "小米电视4A",
"images" : "http://baidu.com/12479122.jpg",
"price" : 4288
}
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HrCdbnYByd2OmNcmILNa",
"_score" : 0.2876821,
"_source" : {
"title" : "apple手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 5899.0
}
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "GrCdbnYByd2OmNcmILNZ",
"_score" : 0.2876821,
"_source" : {
"title" : "大米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 3288
}
}
]
}
}
分词的结果
POST /_analyze
{
"analyzer": "ik_max_word",
"text":"小米手机"
}
{
"tokens" : [
{
"token" : "小米",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "手机",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 1
}
]
}
在上面的案例中,不仅会查询到电视,而且与小米相关的都会查询到。因为是分词查询,只要包含分词就显示。某些情况下,我们需要更精确查找,我们希望这个关系变成and
,可以这样做:
发送请求:
本例中,只有同时包含小米
和手机
的词条才会被搜索到。
POST /ahu/goods/_search
{
"query": {
"match": {
"title":{
"query": "小米手机",
"operator": "and"
}
}
}
}
响应结果:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0470967,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "G7CdbnYByd2OmNcmILNa",
"_score" : 1.0470967,
"_source" : {
"title" : "小米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 2699
}
}
]
}
}
3、多字段匹配查询(multi_match)
multi_match
与match
类似,不同的是它可以在多个字段中查询。
发送请求:
本例中,我们在title字段和subtitle字段中查询小米
这个词
POST /ahu/goods/_search
{
"query": {
"multi_match": {
"query": "小米",
"fields": ["title","subtitle"]
}
}
}
fields属性:设置查询的多个字段
响应结果:
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.52354836,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "G7CdbnYByd2OmNcmILNa",
"_score" : 0.52354836,
"_source" : {
"title" : "小米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 2699
}
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HLCdbnYByd2OmNcmILNa",
"_score" : 0.3901917,
"_source" : {
"title" : "小米电视4A",
"images" : "http://baidu.com/12479122.jpg",
"price" : 4288
}
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HbCdbnYByd2OmNcmILNa",
"_score" : 0.2876821,
"_source" : {
"title" : "华为手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 5288,
"subtitle" : "小米"
}
}
]
}
}
4、关键词精确查询(term)
term查询,精确的关键词匹配查询,不对象查询条件进行分词,目录存入Type的时候已经将关键词存入了
发送请求:
#关键词精确查询
POST /ahu/goods/_search
{
"query": {
"term": {
"title": {
"value": "小米"
}
}
}
}
响应结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.52354836,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "G7CdbnYByd2OmNcmILNa",
"_score" : 0.52354836,
"_source" : {
"title" : "小米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 2699
}
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HLCdbnYByd2OmNcmILNa",
"_score" : 0.3901917,
"_source" : {
"title" : "小米电视4A",
"images" : "http://baidu.com/12479122.jpg",
"price" : 4288
}
}
]
}
}
查询小米手机则查询不到
5、多关键词精确查询(terms)
terms
查询和 term 查询一样,但它允许你指定多值进行匹配。如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件,类似于mysql的in:
发送请求:
查询价格为2699或3288的商品
POST /ahu/goods/_search
{
"query": {
"terms": {
"price": [
"2699",
"3288"
]
}
}
}
响应结果:
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "GrCdbnYByd2OmNcmILNZ",
"_score" : 1.0,
"_source" : {
"title" : "大米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 3288
}
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "G7CdbnYByd2OmNcmILNa",
"_score" : 1.0,
"_source" : {
"title" : "小米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 2699
}
}
]
}
}
结果过滤
默认情况下,elasticsearch在搜索的结果中,会把文档中保存在_source
的所有字段都返回。如果我们只想获取其中的部分字段,我们可以添加_source
的过滤
1、指定字段
指定查询结果中,只显示title和price两个字段
发送请求:
POST /ahu/goods/_search
{
"_source": ["title","price"],
"query": {
"term": {
"price": {
"value": 2699
}
}
}
}
响应结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "G7CdbnYByd2OmNcmILNa",
"_score" : 1.0,
"_source" : {
"price" : 2699,
"title" : "小米手机"
}
}
]
}
}
2、过滤指定字段:includes和excludes
我们也可以通过:
- includes:来指定想要显示的字段
- excludes:来指定不想要显示的字段
二者都是可选的。
发送请求:
POST /ahu/goods/_search
{
"_source": {
"includes": ["title","price"]
},
"query": {
"term": {
"price": {
"value": 2699
}
}
}
}
POST /ahu/_search
{
"_source": {
"excludes": ["images"]
},
"query": {
"term": {
"price": 2699
}
}
}
响应结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "G7CdbnYByd2OmNcmILNa",
"_score" : 1.0,
"_source" : {
"price" : 2699,
"title" : "小米手机"
}
}
]
}
}
发送请求
POST /ahu/goods/_search
{
"_source": {
"excludes": ["price"]
},
"query": {
"term": {
"price": {
"value": 2699
}
}
}
}
响应结果
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "G7CdbnYByd2OmNcmILNa",
"_score" : 1.0,
"_source" : {
"images" : "http://baidu.com/12479122.jpg",
"title" : "小米手机"
}
}
]
}
}
高级查询
1、布尔组合(bool)
bool
把各种其它查询通过must
(必须 )、must_not
(必须不)、should
(应该)的方式进行组合
发送请求:
post /ahu/_search
{
"query":{
"bool":{
"must": { "match": { "title": "小米" }},
"must_not": { "match": { "title": "电视" }},
"should": { "match": { "title": "手机" }}
}
}
}
响应结果:
{
"took": 11,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "ahu",
"_type": "goods",
"_id": "qPHnLG4BWrjRrOzL3Yxl",
"_score": 0.5753642,
"_source": {
"title": "小米手机",
"images": "http://baidu.com/12479122.jpg",
"price": 2699
}
}
]
}
}
2、范围查询(range)
range
查询找出那些落在指定区间内的数字或者时间。range
查询允许以下字符:
操作符 | 说明 |
---|---|
gt == (greater than) | 大于> |
gte == (greater than equal) | 大于等于>= |
lt == (less than) | 小于< |
lte == (less than equal) | 小于等于<= |
发送请求:
查询价格大于等于2000,且小于2700元的所有商品。
POST /ahu/goods/_search
{
"query": {
"range": {
"price": {
"gte": 2000,
"lte": 2700
}
}
}
}
响应结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "G7CdbnYByd2OmNcmILNa",
"_score" : 1.0,
"_source" : {
"title" : "小米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 2699
}
}
]
}
}
3、模糊查询(fuzzy)
fuzzy自动将拼写错误的搜索文本,进行纠正,纠正以后去尝试匹配索引中的数据。它允许用户搜索词条与实际词条出现偏差,但是偏差的编辑距离不得超过2:
发送请求:
如下查询,也能查询到apple手机
##模糊查询
POST /ahu/goods/_search
{
"query": {
"fuzzy": {
"title": "appla"
}
}
}
响应结果:
{
"took" : 27,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.23014566,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HrCdbnYByd2OmNcmILNa",
"_score" : 0.23014566,
"_source" : {
"title" : "apple手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 5899.0
}
}
]
}
}
修改偏差值:
搜索关键词的偏差,默认就是2,我们可以通过fuzziness修改。
POST /ahu/goods/_search
{
"query": {
"fuzzy": {
"title": {
"value": "applaA",
"fuzziness": 2
}
}
}
}
查询排序
1、单字段排序
sort
可以让我们按照不同的字段进行排序,并且通过order
指定排序的方式。desc降序,asc升序。
发送请求:
POST /ahu/goods/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
响应结果:
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : null,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HrCdbnYByd2OmNcmILNa",
"_score" : null,
"_source" : {
"title" : "apple手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 5899.0
},
"sort" : [
5899.0
]
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HbCdbnYByd2OmNcmILNa",
"_score" : null,
"_source" : {
"title" : "华为手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 5288,
"subtitle" : "小米"
},
"sort" : [
5288.0
]
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HLCdbnYByd2OmNcmILNa",
"_score" : null,
"_source" : {
"title" : "小米电视4A",
"images" : "http://baidu.com/12479122.jpg",
"price" : 4288
},
"sort" : [
4288.0
]
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "GrCdbnYByd2OmNcmILNZ",
"_score" : null,
"_source" : {
"title" : "大米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 3288
},
"sort" : [
3288.0
]
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "G7CdbnYByd2OmNcmILNa",
"_score" : null,
"_source" : {
"title" : "小米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 2699
},
"sort" : [
2699.0
]
}
]
}
}
2、多字段排序
假定我们想要结合使用 price和 _score(得分) 进行查询,并且匹配的结果首先按照价格排序,然后按照相关性得分排序:
发送请求:
POST /ahu/goods/_search
{
"query": {
"match_all": {}
},
"sort": [
{"price": {"order": "desc"}},
{"_score":{"order": "desc"}}
]
}
响应结果:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : null,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HrCdbnYByd2OmNcmILNa",
"_score" : 1.0,
"_source" : {
"title" : "apple手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 5899.0
},
"sort" : [
5899.0,
1.0
]
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HbCdbnYByd2OmNcmILNa",
"_score" : 1.0,
"_source" : {
"title" : "华为手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 5288,
"subtitle" : "小米"
},
"sort" : [
5288.0,
1.0
]
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HLCdbnYByd2OmNcmILNa",
"_score" : 1.0,
"_source" : {
"title" : "小米电视4A",
"images" : "http://baidu.com/12479122.jpg",
"price" : 4288
},
"sort" : [
4288.0,
1.0
]
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "GrCdbnYByd2OmNcmILNZ",
"_score" : 1.0,
"_source" : {
"title" : "大米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 3288
},
"sort" : [
3288.0,
1.0
]
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "G7CdbnYByd2OmNcmILNa",
"_score" : 1.0,
"_source" : {
"title" : "小米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 2699
},
"sort" : [
2699.0,
1.0
]
}
]
}
}
高亮查询(Highlighter)
什么是高亮显示
在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮
京东商城搜索"笔记本"
高亮显示的html分析
通过开发者工具查看高亮数据的html代码实现:
高亮查询请求
ElasticSearch可以对查询内容中的关键字部分,进行标签和样式(高亮)的设置。
在使用match查询的同时,加上一个highlight属性:
- pre_tags:前置标签
- post_tags:后置标签
- fields:需要高亮的字段
- title:这里声明title字段需要高亮,后面可以为这个字段设置特有配置,也可以空
发送请求:
POST /ahu/goods/_search
{
"query": {
"match": {
"title": "电视"
}
},
"highlight": {
"pre_tags": "<font color='pink'>",
"post_tags": "</font>",
"fields": {
"title": {}
}
}
}
响应结果:
{
"took" : 55,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.8142733,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HLCdbnYByd2OmNcmILNa",
"_score" : 0.8142733,
"_source" : {
"title" : "小米电视4A",
"images" : "http://baidu.com/12479122.jpg",
"price" : 4288
},
"highlight" : {
"title" : [
"小米<font color='pink'>电视</font>4A"
]
}
}
]
}
}
分页查询
发送请求:
POST /ahu/goods/_search
{
"query": {
"match_all": {
}
},
"size": 2,
"from": 0
}
- size:每页显示多少条
- from:当前页的起始索引,int from = (当前页 - 1) * 每页条数
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 1.0,
"hits" : [
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "HrCdbnYByd2OmNcmILNa",
"_score" : 1.0,
"_source" : {
"title" : "apple手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 5899.0
}
},
{
"_index" : "ahu",
"_type" : "goods",
"_id" : "GrCdbnYByd2OmNcmILNZ",
"_score" : 1.0,
"_source" : {
"title" : "大米手机",
"images" : "http://baidu.com/12479122.jpg",
"price" : 3288
}
}
]
}
}