1.索引库
Elasticsearch采用Rest风格API。
语法:
- 请求方式:PUT/GET/DELETE
- 请求路径:/索引库名
- 请求参数:json格式:
创建
PUT /test
{
"settings": {
"number_of_shards": 5, // 分片数量
"number_of_replicas": 1 // 副本数量
}
}
查看
GET /test
删除
DELETE /test
2.映射字段
映射是定义文档的过程,文档包含哪些字段,这些字段是否保存,是否索引,是否分词等。
语法:
PUT /索引库名/_mapping/类型名称
{
"properties": {
"字段名": {
"type": "类型",
"index": true,
"store": true,
"analyzer": "分词器"
}
}
}
- 类型名称:相当于type的概念,类似于数据库中的不同表
字段名:任意填写 ,可以指定许多属性,例如: - type:类型,可以是text、long、short、date、integer、object等
- index:是否索引,默认为true
- store:是否存储,默认为false
- analyzer:分词器,这里的ik_max_word即使用ik分词器
新增:
PUT /orcas/_mapping/products
{
"properties": {
"title": {
"type": "text", // text 可分词
"analyzer": "ik_max_word"
},
"images": {
"type": "keyword", // keyword 不可分词
"index": "false"
},
"price": {
"type": "float"
}
}
}
查询映射:
GET /orcas/_mapping
2.1.type
- String类型,又分两种:
- text:可分词,不可参与聚合
- keyword:不可分词,数据会作为完整字段进行匹配,可以参与聚合
- Numerical:数值类型,分两类
- 基本数据类型:long、interger、short、byte、double、float、half_float
- 浮点数的高精度类型:scaled_float
- 需要指定一个精度因子,比如10或100。elasticsearch会把真实值乘以这个因子后存储,取出时再还原。
- Date:日期类型
elasticsearch可以对日期格式化为字符串存储,但是建议我们存储为毫秒值,存储为long,节省空间。
2.2.index
- true:字段会被索引,则可以用来进行搜索。默认值就是true
- false:字段不会被索引,不能用来搜索
2.3.store
是否将数据进行额外存储。默认值是false。
Elasticsearch在创建文档索引时,会将文档中的原始数据备份,保存到_source的属性中。可以通过过滤_source来选择哪些要显示,哪些不显示。所以即便store设置为false,也可以搜索到结果。
2.3.boost
激励因子,影响权重
3.数据
语法:
POST /索引库名/类型名
{
"key":"value"
}
新增数据:
POST /orcas/products/1
{
"title":"iPhone XS Max 256G",
"images":"bbbb.jpg",
"price":10999
}
响应:
{
"_index": "orcas",
"_type": "products",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
查询所有:
GET /orcas/_search
{
"query": {"match_all": {}}
}
根据id查询:
GET /orcas/products/1
修改数据:
- id对应文档存在,则修改
- id对应文档不存在,则新增
PUT /orcas/products/1
{
"title":"iPhone XS Max 256G",
"images":"bbbb.jpg",
"price":10999,
"stock": 999,
"desc": "贵得一比"
}
这里在修改的时候新增了两个字段,Elasticsearch可以智能地根据输入的数据来判断类型,自动添加数据映射。
响应:
{
"orcas": {
"mappings": {
"products": {
"properties": {
"desc": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"images": {
"type": "keyword",
"index": false
},
"price": {
"type": "float"
},
"stock": {
"type": "long"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
}
可以看到,stock和desc字段成功映射了。
但如果存储的是String类的数据,因为无法判断是否分词,我们可以看到,它实际是形成了两种类型字段:
- name: text类型
- name.keyword: keyword类型
PS:可以配置动态模板制定动态映射的规则,详情见:
https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html
删除:
DELETE /索引库名/类型名/id值
4.查询
4.1基本查询
语法:
GET /索引库名/_search
{
"query":{
"查询类型":{
"查询条件":"查询条件值"
}
}
}
4.1.1.查询所有(match_all)
GET /orcas/_search
{
"query":{
"match_all": {}
}
}
- query:代表查询对象
- match_all:代表查询所有
- took:查询花费时间,单位是毫秒
- time_out:是否超时
- _shards:分片信息
- hits:搜索结果总览对象
- total:搜索到的总条数
- max_score:所有结果中文档得分的最高分
- hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
- _index:索引库
- _type:文档类型
- _id:文档id
- _score:文档得分
- _source:文档的源数据
4.1.2.匹配查询(match)
GET /orcas/_search
{
"query":{
"match":{
"title":"保温杯"
}
}
}
- or关系
match类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是or的关系
GET /orcas/_search
{
"query":{
"match": {
"title": {
"query": "保温杯",
"operator": "and"
}
}
}
}
- and关系
可以更精确查找
match 查询支持 minimum_should_match 最小匹配参数, 这让我们可以指定必须匹配的词项数用来表示一个文档是否相关。
4.1.3.多字段查询(multi_match)
GET /orcas/_search
{
"query":{
"multi_match": {
"query": "保温杯",
"fields": [ "title", "subTitle" ]
}
}
}
4.1.4.词条匹配(term)
term 查询被用于精确值匹配,这些精确值可能是数字、时间、布尔或者未分词的字符串。
GET /orcas/_search
{
"query":{
"term":{
"price":648.00
}
}
}
4.1.5.多词条精确匹配(terms)
如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件。
GET /orcas/_search
{
"query":{
"terms":{
"price":[648.00,333.00,555.00]
}
}
}
4.2.结果过滤
4.2.1.指定字段过滤
默认情况下,elasticsearch在搜索的结果中,会把文档中保存在_source的所有字段都返回。
如果只想获取其中的部分字段,可以添加_source的过滤。
GET /orcas/_search
{
"_source": ["title","price"],
"query": {
"term": {
"price": 648
}
}
}
4.2.2.指定includes和excludes
- includes:来指定想要显示的字段
- excludes:来指定不想要显示的字段
GET /orcas/_search
{
"_source": {
"includes":["title","price"] // 或者 "excludes": ["images"]
},
"query": {
"term": {
"price": 648
}
}
}
4.3 高级查询
4.3.1.范围查询(range)
查询指定区间内的数字或者时间。
GET /orcas/_search
{
"query":{
"range": {
"price": {
"gte": 500.0,
"lt": 5000.00
}
}
}
}
4.3.2.布尔组合(bool)
把各种其它查询通过must(与)、must_not(非)、**should(或)**的方式进行组合。
GET /orcas/_search
{
"query":{
"bool":{
"must": [
{"match": {
"title": "保温杯"
}}
],
"must_not": [
{"range": {
"price": {
"gte": 600
}
}}
]
}}
}
要查询title为保温杯(保温杯分词的),price不超过600元。
4.3.3.模糊查询(fuzzy)
允许用户搜索词条与实际词条的拼写出现偏差,但是偏差的编辑距离不得超过2。
GET /orcas/_search
{
"query": {
"fuzzy": {
"title": "aphonx" // 可以匹配到iphone
}
}
}
通过fuzziness来指定允许的编辑距离:
GET /orcas/_search
{
"query": {
"fuzzy": {
"title": {
"value":"aphone",
"fuzziness":1
}
}
}
}
这里设置了1,因此上面例子中偏差的2个编辑距离的"aphonx"就查询不到了。
4.4.过滤(filter)
4.4.1.有查询条件的过滤
所有的查询都会影响到文档的评分及排名。
如果需要在查询结果中进行过滤,并且不希望过滤条件影响评分,就不要把过滤条件作为查询条件来用。而是使用filter方式:
GET /orcas/_search
{
"query":{
"bool":{
"must": [
{"match": {
"title": "保温杯"
}}
],
"filter": {
"bool": {
"must_not": [
{"range": {
"price": {
"gte": 600
}
}}
]
}
}
}
}
}
然而我实际没使用filter时的_score和使用filter的相同的???
4.4.2.无查询条件的过滤
如果一次查询只有过滤,没有查询条件,不希望进行评分,可以使用constant_score取代只有 filter 语句的 bool 查询。在性能上是完全相同的,但对于提高查询简洁性和清晰度有很大帮助。
GET /orcas/_search
{
"query":{
"constant_score": {
"filter": {
"range":{"price": {"lt":600.00 }}
}
}
}
}
4.5 排序
4.5.1. 单字段排序
按照不同的字段进行排序,并且通过order指定排序的方式
GET /orcas/_search
{
"query": {
"match": {
"title": "保温杯"
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
4.5.1. 多字段排序
假定我们想要结合使用 price和 _score(得分) 进行查询,并且匹配的结果首先按照价格排序,然后按照相关性得分排序:
GET /orcas/_search
{
"query":{
"bool":{
"must":{ "match": { "title": "保温杯" }},
"filter":{
"range":{"price":{"gt":200,"lt":600}}
}
}
},
"sort": [
{ "price": { "order": "desc" }},
{ "_score": { "order": "desc" }}
]
}
5.聚合aggregations
便于实现对数据的统计、分析。
5.1.基本概念
Elasticsearch中的聚合,包含多种类型,最常用的两种,一个叫桶,一个叫度量:
桶(bucket):
桶的作用,是按照某种方式对数据进行分组,每一组数据在ES中称为一个桶。
Elasticsearch中提供的划分桶的方式有很多:
- Date Histogram Aggregation:根据日期阶梯分组,例如给定阶梯为周,会自动每周分为一组
- Histogram Aggregation:根据数值阶梯分组,与日期类似
- Terms Aggregation:根据词条内容分组,词条内容完全匹配的为一组
- Range Aggregation:数值和日期的范围分组,指定开始和结束,然后按段分组
- ……
度量(metrics):
分组完成以后,我们一般会对组中的数据进行聚合运算,例如求平均值、最大、最小、求和等,这些在ES中称为度量。
比较常用的一些度量聚合方式:
- Avg Aggregation:求平均值
- Max Aggregation:求最大值
- Min Aggregation:求最小值
- Percentiles Aggregation:求百分比
- Stats Aggregation:同时返回avg、max、min、sum、count等
- Sum Aggregation:求和
- Top hits Aggregation:求前几
- Value Count Aggregation:求总数
- ……
5.2.案例
例:
GET /cars/_search
{
"size": 0,
"aggs": {
"popular_brand": {
"terms": {
"field": "make"
},
"aggs": {
"price_avg": {
"avg": {
"field": "price"
}
}
}
}
}
}
结果:
......
......
"aggregations": {
"popular_brand": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "honda",
"doc_count": 3,
"price_avg": {
"value": 16666.666666666668
}
},
{
"key": "ford",
"doc_count": 2,
"price_avg": {
"value": 27500
}
},
{
"key": "toyota",
"doc_count": 2,
"price_avg": {
"value": 13500
}
},
{
"key": "bmw",
"doc_count": 1,
"price_avg": {
"value": 80000
}
}
]
}
}