es的基本操作
创建索引
PUT /products/
{
"mappings":{
"properties":{
"name":{
"type":"text",
"analyzer":"ik_smart"
},
"long_name":{
"type":"text",
"analyzer":"ik_smart"
},
"brand_id":{
"type":"integer"
},
"category_id":{
"type":"integer"
},
"shop_id":{
"type":"integer"
},
"price":{
"type":"scaled_float",
"scaling_factor":100
},
"sold_count":{
"type":"integer"
},
"review_count":{
"type":"integer"
},
"status":{
"type":"integer"
},
"create_time":{
"type":"date"
},
"last_time":{
"type":"date"
}
}
}
}
查看索引列表
GET /_cat/indices?v
查看索引的全部内容
GET /products/_search
普通查询
GET /products/_search
{
"query":{
"match":{
"name":"HUAWEI"
}
}
}
布尔查询,多条件查询
其中bool代表此查询为布尔查询,也就是多条件查询,而must则是and的意思,就是后面集合里的所有条件都要满足:
- filter(and),所有的条件都要符合,相当于where id=1 and name=xxx(不打分)
- must(and),所有的条件都要符合,相当于where id=1 and name=xxx (权重打分)
- should(or),满足一个条件即可,相当于 where id=1 or name=xxx
- must_not(!=),所有条件必须满足不等于,相当于where id!=1 and name!=xxx
注:filter 和 must 与 SQL 中的 and 类似,查询的文档必须符合这两类下的条件,只不过 must 下的条件会参与 打分 而 filter 下的条件不会。
AND查询:(must)
GET /products/_search
{
"query":{
"bool":{
"must":[
{
"match":{
"name":"Book"
}
},
{
"match":{
"shop_id":1
}
}
]
}
}
}
OR查询:(should)
GET /products/_search
{
"query":{
"bool":{
"should":[
{
"match":{
"name":"Book"
}
},
{
"match":{
"shop_id":1
}
}
]
}
}
}
Where Not 查询:(must_not)
GET /products/_search
{
"query":{
"bool":{
"must_not":[
{
"match":{
"name":"Book"
}
},
{
"match":{
"shop_id":1
}
}
]
}
}
}
过滤查询
查询name带有Book或者price等于8888,并筛选出1000<price<=6000的文档
GET /products/_search
{
"query":{
"bool":{
"should":[
{
"match":{
"name":"Book"
}
},
{
"match":{
"price":8888
}
}
],
"filter":{
"range":{
"price":{
"gt":1000,
"lte":6000
}
}
}
}
}
}
精准查询
-
term:通过倒排索引指定的词条进行精确的查找,配合keyword类型的字段,都不分词,直接匹配
-
match:查询之前会通过分词器解析,解析后再进行查询
两个类型:text(会被分词器解析)和keyword(不会被分词器解析)
term 精准查询
GET /products/_search
{
"query":{
"bool":{
"should":[
{
"term":{
"long_name":"HUAWEI Mate Book"
}
},
{
"term":{
"price":6299
}
}
]
}
}
}
match 分词查询
GET /products/_search
{
"query":{
"bool":{
"should":[
{
"match":{
"long_name":"HUAWEI Mate Book"
}
},
{
"match":{
"price":6299
}
}
]
}
}
}
多字段 - 权重查询
搜索字段为 fields : long_name,category,可以添加多个要参与搜索的字段,其中 ^ 的数值越高权重越大,搜索到的数据排名越靠前
GET /products/_search
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "笔记本电脑",
"fields": ["long_name^3","category^2"]
}
}
]
}
},
"from": 0,
"size": 20,
"_source": ["id","name","long_name","category"]
}
高亮查询
对于关键字的查询会是红色字体,高亮进行显示,分数越高的数据越靠前
GET /products/_search
{
"query":{
"match":{
"name":"Book"
}
},
"highlight": {
"fields": {
"name":{}
}
}
}
查询获取指定的字段
GET /products/_search
{
"query":{
"match":{
"name":"HUAWEI"
}
},
"_source":["name","price"]
}
查询排序
默认是根据_score匹配分值进行降序排序的,但如果我们指定一个字段进行asc或者desc排序呢?这里我使用price来进行降序
GET /products/_search
{
"query":{
"match":{
"name":"Book"
}
},
"_source":["name","price"],
"sort":[
{
"price":{
"order":"desc"
}
}
]
}
分页查询
添加了from和size属性,其中from是指分页的起始索引,size是指分页容量
GET /products/_doc/_search
{
"query":{
"match":{
"name":"Book"
}
},
"_source":["name","price"],
"sort":[
{
"price":{
"order":"desc"
}
}
],
"from":0,
"size":1
}
nested 对象内部类查询
es索引结构
PUT /products/
{
"mappings": {
"properties": {
"name":{
"type": "text",
"analyzer": "ik_smart"
},
"long_name":{
"type": "text",
"analyzer": "ik_smart"
},
"skus":{
"type": "nested",
"properties": {
"name":{
"type":"text",
"analyzer": "ik_smart"
},
"price":{
"type":"scaled_float",
"scaling_factor":100
}
}
},
"attributes":{
"type": "nested",
"properties": {
"name": { "type": "keyword" },
"value": { "type": "keyword"}
}
}
}
}
}
nested查询
GET /products/_search
{
"query": {
"bool": {
"filter": [
{
"nested": {
"path": "attributes",
"query": {
"bool": {
"must": [
{
"term": {
"attributes.name": "颜色"
}
},
{
"term": {
"attributes.value": "深空灰"
}
}
]
}
}
}
}
]
}
}
}
GET /products/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "skus",
"query": {
"bool": {
"must": [
{
"match": {
"skus.name": "皓月银"
}
},
{
"match": {
"skus.price": 6299
}
}
]
}
}
}
}
]
}
}
}
注:
nested 映射的字段名称的类型需要先定义,而且需要与查询中的path名称一致