搜索引擎elasticsearch常用指令演示

Load ES 接口

http://business-search.aurayou/api/loadES/product/all

交互方式

操作ES有3种方式:

  • kibana控制台(Dev Tools)
  • Http + json
  • api接口

ES支持以下命令:

  • GET:获取请求对象的当前状态。
  • POST:改变对象的当前状态。
  • PUT:创建一个对象。
  • DELETE:销毁对象。
  • HEAD:请求获取对象的基础信息。

所有操作的格式都类似于:命令 + /索引/类型/ID/ + 操作 + ?选项 + body
比如下面的kibana控制台请求表示:
指令GET,索引aurajike,类型productinfo,ID为空。
操作是_search,选项为_source=id,即只返回id字段
BODY则是筛选的字段条件。

GET aurajike/productinfo/_search?_source=id
{
  "query": {
    "match": {
      "basicInfo.masterName": "蛋糕"
    }
  }
}

如果是HTTP方式,则对应于HTTP的GET/POST等方法,索引前面补上es的ip:port,如http://192.168.1.212:9200/aurajike/productinfo/_search?_source=id。
api接口的方式则视不同的api参数要求定。
下面我们提供一些kibana控制台的常用操作示例,其他方式可参考修改。

常用操作示例

创建索引

创建文档时,如果索引不存在,es会自动检测文档的字段类型并创建索引。
但默认索引对数组的搜索不友好,所以我们手动创建索引并指定数组为nested

  • es默认将数组元素的相同字段的值索引在一起,如{"field1":[v1,v2,v3], "field2":[v4,v5,v6]},这就打乱了原本V1-V4、V2-V5、V3-V6的对应关系。
  • 如果要保持数组元素内的字段关系,应指定nested类型,该类型下es会将每个数组的元素存储在一个独立的隐藏文档内,保持元素的内部结构

下面的索引配置指令,我们指定了分片数量1,副本1,分词器ik,mappings字段则指定了文档各个字段的type,重点是数组我们指定为nested。

DELETE aurajike

PUT aurajike
{
  "settings":{
    "number_of_shards": "1",
    "number_of_replicas": "1",  
    "analysis":{   
      "analyzer":{
        "ik":{
          "tokenizer":"ik_smart"
        }
      }
    }
  },
  "mappings": {
    "productinfo": {
      "properties": {
        "attrList": {
          "type": "nested",
          "properties": {
            "attrId": {
              "type": "long"
            },
            "attrValue": {
              "type": "keyword",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "id": {
              "type": "long"
            }
          }
        },
        "basicInfo": {
          "properties": {
            "autoDelivery": {
              "type": "long"
            },
            "defaultSkuId": {
              "type": "long"
            },
            "deliveryType": {
              "type": "long"
            },
            "displaySalesCount": {
              "type": "long"
            },
            "id": {
              "type": "long"
            },
            "masterName": {
              "type": "text",
			  "analyzer": "ik_smart",
			  "search_analyzer": "ik_smart",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "maxPrice": {
              "type": "long"
            },
            "minPrice": {
              "type": "long"
            },
            "natureCategoryId": {
              "type": "long"
            },
            "productSource": {
              "type": "long"
            },
            "productStatus": {
              "type": "long"
            },
            "productType": {
              "type": "long"
            },
            "refundAmountType": {
              "type": "long"
            },
            "slave_name": {
              "type": "text",
			  "analyzer": "ik_smart",
			  "search_analyzer": "ik_smart",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "sortNumber": {
              "type": "long"
            },
            "sortSalesCount": {
              "type": "long"
            },
            "status": {
              "type": "long"
            },
            "storeType": {
              "type": "long"
            },
            "supplierId": {
              "type": "long"
            },
            "virtualProduct": {
              "type": "long"
            }
          }
        },
        "categoryId": {
          "type": "long"
        },
        "frontCategoryIdList": {
          "type": "long"
        },
        "salesChannelList": {
          "type": "nested",
          "properties": {
            "availableInventory": {
              "type": "long"
            },
            "displaySalesCount": {
              "type": "long"
            },
            "id": {
              "type": "long"
            },
            "initSalesCount": {
              "type": "long"
            },
            "inventory": {
              "type": "long"
            },
            "price": {
              "type": "long"
            },
            "salesChannelId": {
              "type": "long"
            },
            "salesCount": {
              "type": "long"
            },
            "skuId": {
              "type": "long"
            },
            "slavePrice": {
              "type": "long"
            }
          }
        },
        "skuList": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "long"
            },
            "marketPrice": {
              "type": "long"
            },
            "masterName": {
              "type": "text",
			  "analyzer": "ik_smart",
			  "search_analyzer": "ik_smart",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "price": {
              "type": "long"
            },
            "slaveCostPrice": {
              "type": "long"
            },
            "slaveName": {
              "type": "text",
			  "analyzer": "ik_smart",
			  "search_analyzer": "ik_smart",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        },
        "skuOptionList": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "long"
            },
            "option_id": {
              "type": "long"
            },
            "option_value": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "skuId": {
              "type": "long"
            }
          }
        }
      }
    }
  }
}

添加文档

用post方法可以由es为我们自动生成一串由英文字母或数字组成的20字节唯一的ID。
对于商品的文档,建议就以商品ID为文档ID,即PUT /aurajike/productinfo/[productID],从而在已知ID的情况下可以直接获取到商品详情。否则,得拿ID去搜索。

get aurajike/productinfo/_search
# 指定id
PUT /aurajike/productinfo/1
{
  "basicInfo": {
    "productId": 1,
    "masterName": "四季芝士蛋糕(四拼)",
    "slaveName": "草莓 蓝莓 芝士 巧克力 如四季流转各具风味"
  },
  "categoryId": 105,
}

// 由es自动生成id
POST /aurajike/productinfo
{
  "basicInfo": {
    "productId": 1,
    "masterName": "自动ID示例",
    "slaveName": "自动ID示例"
  },
  "categoryId": 105
}

删除文档

DELETE /aurajike/productinfo/1

修改文档

修改文档可以有以下几种方式:

  • PUT: 指定ID添加一个新文档,将旧文档直接覆盖掉
  • POST + _update 更新或插入字段
  • 执行脚本:能够比前两种方式更灵活且强大

对于数据库增量更新,POST + _update 感觉比较好用。

# 文档覆盖,旧文档将删除,新文档只有这里插入的categoryId字段
PUT /aurajike/productinfo/1
{
  "categoryId": 505
}

# 更新和插入字段
# basicInfo.basicInfo.slaveName:这个字段在原文档已存在,所以是更新
# newField:这个字段原文档不存在,所以会新增
POST /aurajike/productinfo/1/_update
{
  "doc": {
    "basicInfo" : {
      "slaveName" : "更新这个字段"
    },
    "newField": "新增这个字段"
  }
}

# 执行脚本:更新字段
# ctx._source代表当前的文档,后面可以直接跟文档的字段
# 这条指令我们把basicInfo的categoryId字段在原值基础上加了101
POST /aurajike/productinfo/1/_update
{
  "script" : "ctx._source.categoryId += 101"
}

# 执行脚本:数组元素的增删改
# 假设我们的文档里有如下结构:
# {
# 	"attrList": [
# 		{
# 			"id": 20223,
# 			"attrId": "1001",
# 			"attrValue": "满99元包邮"
# 		},
# 		{
# 			"id": 20232,
# 			"attrId": "1001",
# 			"attrValue": "7天无理由退换货"
# 		}
# 	]
# }
# 下面的语句先删除(removeIf) id==20223 && attrId=="1001"的数组元素
# 然后添加attr字段内容到数组内。
POST /aurajike/productinfo/1/_update
{
  "script": {
    "source": "ctx._source.attrList.removeIf(item -> (item.id==20223 && item.attrId==\"1001\"));ctx._source.user.add(params.attr)",
    "params": {
      "attr": {
        "id": 20223,
        "attrId": "1002",
        "attrValue": "江浙沪包邮"
      }
    }
  }
}

查询

简单查询

ES内置字段含义:
_source: 在get和_mget等命令里,_source 表示需要检索的字段,请求和响应的body里均包含此字段
found: 响应帧需要判断每个body前面的found字段,found == true时body字段才有效,否则未查找到结果

下面的示例包含获取指定文档、条件查询、指定返回的字段、分页、排序、批量查询等。

# 获取指定文档
GET /aurajike/productinfo/1/

# 搜索"masterName"字段包含"蛋糕"关键字的文档
# 只返回两个字段:_source=categoryId,basicInfo.productId
# 按categoryId降序
GET aurajike/_search?_source=categoryId,basicInfo.productId
{
  "query": { # 查询条件
    "match": {
      "basicInfo.masterName": "蛋糕"
    }
  },
  "from":0,"size":20, # 分页
  "sort": { "categoryId": { "order": "desc" }} # 排序
}

# 多文档查询_mget
# 方式一:GET /_mget,不同的_index、_type等字段在body内指定
# 方式二:GET /aurajike/productinfo,索引和类型相同,body指定id或不指定
# 下面这条搜索语句返回/aurajike/productinfo/下的所有文档的productId字段,以及/index/type/1/文档的test字段
GET /_mget
{
  "docs" : [
    {"_index":"aurajike","_type":"productinfo","_id":1,
	  "_source": "basicInfo.productId"
	},
	{"_index":"index","_type":"type","_id":1,"_source":"test"}
  ]
}

# 获取1~5这5个文档
GET /aurajike/productinfo/_mget
{
   "ids" : [ "1", "2", "3", "4", "5" ]
}

高级多条件查询

现实中的查询操作往往的多条件复杂的查询,这类查询,es也是支持的。
es支持的组合查询参数有4类:

  • must: 文档 必须 匹配这些条件才能被包含进来。
  • must_not: 文档 必须不 匹配这些条件才能被包含进来。
  • should:
  • 当存在must或must_not语句时:should语句可以用来加分。即满足should语句的文档在排序上有加分,满足的越多加分越多。一条都不满足时对匹配结果无影响
  • 当没有must或must_not语句时:至少要匹配一条should语句
  • filter: 必须 匹配,但它以不评分、过滤模式来进行。这些语句对评分没有贡献,只是根据过滤标准来排除或包含文档。

上面提到的加分,是指在bool查询时es会计算文档在匹配条件上的相关性得分,得分高的排序优先。
所以如果不需要对相关性进行计算,应直接用filter参数,可以提高性能。

filter过滤器的性能远大于评分查询,所以不需要相关性评分的情况应都用filter:一方面评分计算耗时,而filter是不评分的;另一方面filter会被缓存

引用官方的话:通常的规则是,使用 查询(query)语句来进行 全文 搜索或者其它任何需要影响 相关性得分 的搜索。除此以外的情况都使用过滤(filters)。

现在我们来演示一组复杂的组合查询:

GET /aurajike/productinfo/_search
{
  "_source": "_id", # 只返回文档ID(文档ID = 商品ID)
  "from": 0,"size": 10, # 分页
  "sort": {"basicInfo.maxPrice": {"order": "desc"}}, # 按最大价格降序
  "query": {
    "bool": {
      "filter": [ # filter过滤,不计算相关性;1/2/3/4/5/6语句是 与 的关系
        {"terms": {"frontCategoryIdList": [145,277]}}, # 1. 筛选前端类目ID为145或者277的商品
		{ # 2.筛选配送地区包含上海310100的商品
			"bool": {
			  "should": [ # 2.1 / 2.2 两条语句是 或 的关系,满足其一即语句2为真
				{
				  "bool": { # 2.1 属性ID为19的是配送范围,不存在ID为19的属性即表示全国配送
					"must_not": {
					  "nested": { # attrList是数组nested格式,必须用nested方式才能搜索到
						"path": "attrList",
						"query": {
						  "bool": {
							"must": [
							  {
								"term": {
								  "attrList.attrId": 19
								}
							  }
							]
						  }
						}
					  }
					}
				  }
				},
				{
				  "nested": { # 2.2 或者配置范围是上海的
					"path": "attrList",
					"query": {
					  "bool": {
						"must": [
						  {
							"term": {
							  "attrList.attrId": 19
							}
						  },
						  {
							"terms": {
							  "attrList.attrValue": [
								"","310000","310101","310114","310105",
                                "310109","310112","310113","310118",
                                "310120","310151","310106","310110",
                                "310115","310116","310117","310100",
                                "310104","310107"
							  ]
							}
						  }
						]
					  }
					}
				  }
				}
			  ]
			}
        }
        { # 3. 属性筛选
          "nested": {
            "path": "attrList",
            "query": {
              "bool": {
                "must": [
                  {"term": {"attrList.attrId": 22}},
                  {"terms": {"attrList.attrValue": ["40"]}}
                ]
              }
            }
          }
        },
        { # 4. 属性筛选
          "nested": {
            "path": "attrList",
            "query": {
              "bool": {
                "must": [
                  {"term": {"attrList.attrId": 27}},
                  {"terms": {"attrList.attrValue": ["3"]}}
                ]
              }
            }
          }
        },
        { # 5. 商品或SKU名称关键字匹配
          "bool": {
            "should": [
              {"match": {"basicInfo.masterName": "枇杷"}},
              {"match": {"basicInfo.slave_name": "枇杷"}},
              {"match": {"skuList.masterName": "枇杷"}},
              {"match": {"skuList.slaveName": "枇杷"}}
            ]
          }
        },
        { # 6. 属性过滤
          "bool": {
            "must_not": {
              "nested": {
                "path": "attrList",
                "query": {
                  "bool": {
                    "must": [
                      {"term": {"attrList.attrId": 1006}},
                      {"term": {"attrList.attrValue": "1"}}
                    ]
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}
posted @ 2019-06-21 18:36  Jo_ZSM  阅读(901)  评论(0编辑  收藏  举报