首页
留言
统计
朋友
关于
推荐
我的动态
Search
1
宝塔安装git,实现实时更新服务器代码
111 阅读
2
hello world!- 我的博客诞生啦~
79 阅读
3
PHP之PhpSpreadsheet 的使用
68 阅读
4
利用数组做积分系统
61 阅读
5
Elasticsearch的学习笔记(一)
53 阅读
工作
学习
生活
登录
/
注册
Search
标签搜索
elasticsearch
PHP
Hello World
PhpSpreadsheet
elasticsearch-php
默然
累计撰写
11
篇文章
累计收到
7
条评论
首页
栏目
工作
学习
生活
页面
留言
统计
朋友
关于
推荐
我的动态
搜索到
4
篇与
的结果
2022-05-12
Elasticsearch-php学习笔记
连接Elasticsearch这里采用集群的方式,所以需要启动多个Elasticsearch实例。$parms = [ "localhost:1001", "localhost:1002", "localhost:1003", ]; //连接elasticsearch $this->client = \Elasticsearch\ClientBuilder::create()->setHosts($parms)->build;创建索引$params = [ 'index' => 'user', 'body' => [ 'settings' => [ 'number_of_shards' => 3, 'number_of_replicas' => 2 ] ] ] echo json_encode($this->client->indices()->create($params));返回结果:{ "acknowledged": true, //索引是否创建成功 "shards_acknowledged": true, //分片是否创建成功 "index": "user" //索引名称 }删除索引$params = [ 'index' => 'user', ]; echo json_encode($this->client->delete($params));返回结果:{ "acknowledged": true //确认是否删除成功 }获取文档$params = [ 'index' => 'user', //索引名称 'type' => 'user', //类型名称 'id' => '1', ]; echo json_encode($this->client->get($params));返回结果:{ "_index": "user", //索引名称 "_type": "user", //类型名称 "_id": "1", //文档id "_version": 2, //版本号 "_seq_no": 30, //序列号 "_primary_term": 1, //主键 "found": true, //是否找到 "_source": { //文档内容 "uid": 1, "username": "test", "signature": "test", "phone": "13276535811", "money": "100", "address": "天津 天津市 东丽区", "create_time": 1652340350 } }修改文档$params = [ 'index' => 'user', //索引名称 'type' => 'user', //类型名称 'id' => '1', //文档id 'body' => [ //文档内容 'uid' => 1, 'username' => 'test', 'signature' => 'test', 'phone' => '13276535811', 'money' => '100', 'address' => '天津 天津市 东丽区', 'create_time' => time(), ], ]; echo json_encode($this->client->index($params));返回结果:{ "_index": "user", //索引名称 "_type": "user", //类型名称 "_id": "1", //文档id "_version": 2, //版本号 "result": "updated", //操作结果 "_shards": { "total": 3, //分片总数 "successful": 3, //成功数 "failed": 0 //失败数 }, "_seq_no": 30, //操作序列号 "_primary_term": 1 //主分片的term }搜索文档Match 查询$params = [ 'index' => 'user', //索引名称 'type' => 'user', //类型名称 'body' => [ 'query' => [ 'match' => [ 'username' => 'test', ], ], ], ]; echo json_encode($this->client->search($params));返回结果:{ "took": 23, //查询耗时 "timed_out": false, //是否超时 "_shards": { //分片信息 "total": 3, //分片总数 "successful": 3, //成功数 "skipped": 0, //跳过数 "failed": 0 //失败数 }, "hits": { //查询结果 "total": { //总数 "value": 1, //总数 "relation": "eq" //关系 }, "max_score": 6.800749, //最高分 "hits": [ //结果 { "_index": "user", //索引名称 "_type": "user", //类型名称 "_id": "1", //文档id "_score": 6.800749, //分数 "_source": { //文档内容 "uid": 1, "username": "test", "signature": "test", "phone": "13276535811", "money": "100", "address": "天津 天津市 东丽区", "create_time": 1652340350 } } ] } }bool 查询$params = [ 'index' => 'user', //索引名称 'type' => 'user', //类型名称 'body' => [ 'query' => [ 'bool' => [ 'must' => [ //查询条件 must表示必须满足(相当于and),should表示可以满足(相当于or) [ 'match' => [ 'username' => '胡' ] ], [ 'match' => [ 'address' => '云' ] ], ] ] ] ] ]; echo json_encode($this->client->search($params));返回结果:{ "took": 896, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 8.003002, "hits": [ { "_index": "user", "_type": "user", "_id": "305", "_score": 8.003002, "_source": { "uid": 305, "username": "李超", "phone": "13574552270", "signature": "新改思所满确无性口区装农斯更员整图动工织备山象革道转正清系电前自物象很度使今经", "money": "15.36", "address": "广东省 惠州市 惠阳区", "creat_time": "2011-04-15 15:43:49" } } ] } }复杂查询$params = [ 'index' => 'user', 'type' => 'user', 'body' => [ 'query' => [ 'bool' => [ 'filter' => [ //过滤,返回符合条件的数据 'term' => [ 'address' => '云南' ] ], 'should' => [ 'match' => [ 'username' => '李' ] ] ] ] ] ]; echo json_encode($this->client->search($params));Scrolling 查询$params = [ 'index' => 'user', 'type' => 'user', 'body' => [ "query" => [ "match_all" => new \stdClass() ], 'size' => 5 //查询数量 ] ]; echo json_encode($this->client->search($params)); while (isset($response['hits']['hits']) && count($response['hits']['hits']) > 0) { 判断是否还有下一页 $scroll_id = $response['_scroll_id']; //这个是上一次请求返回的scroll_id $response = $this->client->scroll([ //这个是滚动查询 "scroll_id" => $scroll_id, //上一次请求返回的scroll_id "scroll" => "30s" //滚动时间 ] ); }删除文档$params = [ 'index' => 'user', //索引名称 'type' => 'user', //类型名称 'id' => '1', //文档id ]; echo json_encode($this->client->delete($params));返回结果:{ "_index": "user", // 索引名称 "_type": "user", // 类型名称 "_id": "1", // 文档id "_version": 3, // 版本号 "result": "deleted", // 操作结果 "_shards": { // 分片信息 "total": 3, // 分片总数 "successful": 3, // 成功数 "failed": 0 // 失败数 }, // 分片信息 "_seq_no": 309, // 操作序列号 "_primary_term": 1 // 主分片的term }忽略异常这种会忽略 404 异常$params = [ 'index' => 'user', //索引名称 'type' => 'user', //类型名称 'id' => '1', //文档id 'client' => [ 'ignore' => 404 ], //忽略异常 ]; echo json_encode($this->client->get($params)); 返回结果:{ "_index": "user", "_type": "user", "_id": "1", "found": false }这里BadRequest400Exception 和 MissingDocument404Exception 都会被忽略:$params = [ 'index' => 'user', //索引名称 'type' => 'user', //类型名称 'id' => '1', //文档id 'client' => [ 'ignore' => [400, 404] ], //忽略异常 ]; echo json_encode($this->client->get($params));返回结果:{ "_index": "user", "_type": "user", "_id": "1", "found": false }返回详细输出$params = [ 'index' => 'user', //索引名称 'type' => 'user', //类型名称 'id' => '1', //文档id 'client' => [ 'trace' => true ], //返回详细输出 ]; echo json_encode($this->client->get($params));返回结果:原来的返回结果是这样的:{ "_index": "user", "_type": "user", "_id": "5", "_version": 1, "_seq_no": 0, "_primary_term": 1, "found": true, "_source": { "uid": 5, "username": "曾杰", "phone": "13215426458", "signature": "老和际不日强老理县治件京济划热选影江例事导还开点深际建起厂党方速识来当西住原清处维安价油些", "money": "30.37", "address": "澳门特别行政区 离岛 -", "creat_time": "1996-10-06 18:37:13" } }但是加了一个trace字段,用于输出详细输出{ "transfer_stats": { //传输统计 "url": "http://localhost/user/user/5", //请求url "content_type": "application/json; charset=UTF-8", //请求内容类型 "http_code": 200, //请求状态码 "header_size": 282, //请求头大小 "request_size": 163, //请求体大小 "filetime": -1, //文件时间 "ssl_verify_result": 0, //ssl验证结果 "redirect_count": 0, //重定向次数 "total_time": 0.001899, //总时间 "namelookup_time": 0.000505, //域名解析时间 "connect_time": 0.000615, //连接时间 "pretransfer_time": 0.000635, //预传输时间 "size_upload": 0, //上传大小 "size_download": 572, //下载大小 "speed_download": 572000, //下载速度 "speed_upload": 0, //上传速度 "download_content_length": 572, //下载内容长度 "upload_content_length": -1, //上传内容长度 "starttransfer_time": 0.001882, //开始传输时间 "redirect_time": 0, //重定向时间 "redirect_url": "", //重定向url "primary_ip": "::1", //主ip "certinfo": [ //证书信息 ], "primary_port": 1003, //主端口 "local_ip": "::1", //本地ip "local_port": 54994, //本地端口 "http_version": 2, //http版本 "protocol": 1, //协议 "ssl_verifyresult": 0, //ssl验证结果 "scheme": "HTTP", //协议 "appconnect_time_us": 0, //app连接时间 "connect_time_us": 615, //连接时间 "namelookup_time_us": 505, //域名解析时间 "pretransfer_time_us": 635, //预传输时间 "redirect_time_us": 0, //重定向时间 "starttransfer_time_us": 1882, //开始传输时间 "total_time_us": 1899, //总时间 "error": "", //错误信息 "errno": 0 //错误码 }, "curl": { //curl信息 "error": "", //错误信息 "errno": 0 //错误码 }, "effective_url": "http://localhost/user/user/5", //有效url "headers": { //请求头 "Warning": [ //警告 "299 Elasticsearch-7.8.0-757314695644ea9a1dc2fecd26d1a43856725e65 \"[types removal] Specifying types in document get requests is deprecated, use the /{index}/_doc/{id} endpoint instead.\"" //警告信息 ], "content-type": [ //内容类型 "application/json; charset=UTF-8" //内容类型 ], "content-length": [ //内容长度 "572" //内容长度 ] }, "version": "1.1", //版本 "status": 200, //状态码 "reason": "OK", //状态码说明 "body": { //返回内容 "_index": "user", //索引 "_type": "user", //类型 "_id": "5", //id "_version": 1, //版本 "_seq_no": 0, //序列号 "_primary_term": 1, //主键 "found": true, //是否找到 "_source": { //内容 "uid": 5, "username": "曾杰", "phone": "13215426458", "signature": "老和际不日强老理县治件京济划热选影江例事导还开点深际建起厂党方速识来当西住原清处维安价油些", "money": "30.37", "address": "澳门特别行政区 离岛 -", "creat_time": "1996-10-06 18:37:13" } } }PHP 处理 JSON 数组或对象空对象{ "query" : { "match" : { "content" : "quick brown fox" } }, "highlight" : { "fields" : { "content" : {} //这个空对象便会引起问题, 因为这个空对象会被转换成一个字符串, 并且会被解析成一个数组, 因此会出现问题 } } }改变为:$params['body'] = array( 'query' => array( 'match' => array( 'username' => '默然' ) ), 'highlight' => array( 'fields' => array( 'content' => new \stdClass() //使用PHP stdClass类来代替空对象, 因为这个类不会被转换成字符串, 并且不会被解析成数组, 因此不会出现问题 ) ) ); $results = $this->client->search($params);修改文档部分修改$params = [ 'index' => 'user', 'type' => 'user', 'id' => '3', 'body' => [ 'doc' => [ 'username' => 'ab313131313c', 'money' => '100', ] ] ]; $response = $this->client->update($params); echo json_encode($response); 返回结果:{ "_index": "user", "_type": "user", "_id": "3", "_version": 3, "result": "updated", "_shards": { "total": 3, "successful": 3, "failed": 0 }, "_seq_no": 369, "_primary_term": 1 }
2022年05月12日
9 阅读
0 评论
0 点赞
2022-04-27
Elasticsearch的学习笔记(三)
条件查询查询指定字段GET http://localhost:9200/shopping/_search查询所有数据,只显示字段title和price请求体:{ "query":{ "match_all":{} }, "_source":["title","price"] }返回结果:{ "took": 3, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 9, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "kq3nZIAB6CX3m_v3APT6", "_score": 1, "_source": { "price": 3999, "title": "小米手机" } }, { "_index": "shopping", "_type": "_doc", "_id": "k63tZIAB6CX3m_v3vfQI", "_score": 1, "_source": { "price": 3999, "title": "小米手机" } }, { "_index": "shopping", "_type": "_doc", "_id": "l636ZIAB6CX3m_v3q_R5", "_score": 1, "_source": { "price": 1999, "title": "华为手机" } }, { "_index": "shopping", "_type": "_doc", "_id": "ma37ZIAB6CX3m_v3BvQc", "_score": 1, "_source": { "price": 1999, "title": "华为手机" } }, { "_index": "shopping", "_type": "_doc", "_id": "n60KZYAB6CX3m_v3q_Tc", "_score": 1, "_source": { "price": 3999, "title": "小米手机" } }, { "_index": "shopping", "_type": "_doc", "_id": "oK0KZYAB6CX3m_v3rfQs", "_score": 1, "_source": { "price": 3999, "title": "小米手机" } }, { "_index": "shopping", "_type": "_doc", "_id": "oa0KZYAB6CX3m_v3rvS_", "_score": 1, "_source": { "price": 3999, "title": "小米手机" } }, { "_index": "shopping", "_type": "_doc", "_id": "oq0KZYAB6CX3m_v3sPQR", "_score": 1, "_source": { "price": 3999, "title": "小米手机" } }, { "_index": "shopping", "_type": "_doc", "_id": "o60KZYAB6CX3m_v3sfT8", "_score": 1, "_source": { "price": 3999, "title": "小米手机" } } ] } }分页查询GET http://localhost:9200/shopping/_search请求体:{ "query":{ "match_all":{} }, "from":0, "size":2 }返回结果:{ "took": 2, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 9, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "kq3nZIAB6CX3m_v3APT6", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "k63tZIAB6CX3m_v3vfQI", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } } ] } }查询排序GET http://localhost:9200/shopping/_search按价格的从高到低排序请求体:{ "query":{ "match_all":{} }, "sort":[ { "price":{ "order":"desc" } } ] }返回结果:{ "took": 13, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 9, "relation": "eq" }, "max_score": null, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "kq3nZIAB6CX3m_v3APT6", "_score": null, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 }, "sort": [ 3999 ] }, { "_index": "shopping", "_type": "_doc", "_id": "k63tZIAB6CX3m_v3vfQI", "_score": null, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 }, "sort": [ 3999 ] }, { "_index": "shopping", "_type": "_doc", "_id": "n60KZYAB6CX3m_v3q_Tc", "_score": null, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 }, "sort": [ 3999 ] }, { "_index": "shopping", "_type": "_doc", "_id": "oK0KZYAB6CX3m_v3rfQs", "_score": null, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 }, "sort": [ 3999 ] }, { "_index": "shopping", "_type": "_doc", "_id": "oa0KZYAB6CX3m_v3rvS_", "_score": null, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 }, "sort": [ 3999 ] }, { "_index": "shopping", "_type": "_doc", "_id": "oq0KZYAB6CX3m_v3sPQR", "_score": null, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 }, "sort": [ 3999 ] }, { "_index": "shopping", "_type": "_doc", "_id": "o60KZYAB6CX3m_v3sfT8", "_score": null, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 }, "sort": [ 3999 ] }, { "_index": "shopping", "_type": "_doc", "_id": "l636ZIAB6CX3m_v3q_R5", "_score": null, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 1999 }, "sort": [ 1999 ] }, { "_index": "shopping", "_type": "_doc", "_id": "ma37ZIAB6CX3m_v3BvQc", "_score": null, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 1999 }, "sort": [ 1999 ] } ] } }多条件查询GET http://localhost:9200/shopping/_search查询小米的牌子,价格为3999元的手机。请求体:{ "query":{ "bool":{ "must":[{ //must表示必须满足(相当于and),should表示可以满足(相当于or) "match":{ "category":"小米" } },{ "match":{ "price":3999.00 } }] } } }返回结果:{ "took": 3, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 7, "relation": "eq" }, "max_score": 1.9979823, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "kq3nZIAB6CX3m_v3APT6", "_score": 1.9979823, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "k63tZIAB6CX3m_v3vfQI", "_score": 1.9979823, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "n60KZYAB6CX3m_v3q_Tc", "_score": 1.9979823, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oK0KZYAB6CX3m_v3rfQs", "_score": 1.9979823, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oa0KZYAB6CX3m_v3rvS_", "_score": 1.9979823, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oq0KZYAB6CX3m_v3sPQR", "_score": 1.9979823, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "o60KZYAB6CX3m_v3sfT8", "_score": 1.9979823, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } } ] } }条件查询列子2GET http://localhost:9200/shopping/_search查询小米和华为的牌子,价格大于3000元的手机。查询范围可以使用以下4个:gt 大于gte 大于或等于lt 小于lte 小于或等于must表示必须满足(相当于and),should表示可以满足(相当于or)请求体:{ "query":{ "bool":{ "should":[ { "match":{ "category":"小米" } }, { "match":{ "category":"华为" } } ], "filter":{ "range":{ "price":{ "gt":3000 } } } } } }返回结果:{ "took": 2, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 7, "relation": "eq" }, "max_score": 0.99798226, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "kq3nZIAB6CX3m_v3APT6", "_score": 0.99798226, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "k63tZIAB6CX3m_v3vfQI", "_score": 0.99798226, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "n60KZYAB6CX3m_v3q_Tc", "_score": 0.99798226, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oK0KZYAB6CX3m_v3rfQs", "_score": 0.99798226, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oa0KZYAB6CX3m_v3rvS_", "_score": 0.99798226, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oq0KZYAB6CX3m_v3sPQR", "_score": 0.99798226, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "o60KZYAB6CX3m_v3sfT8", "_score": 0.99798226, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } } ] } }全文检索GET http://localhost:9200/shopping/_search这功能像搜索引擎那样,如品牌输入“小华”,返回结果带回品牌有“小米”和华为的。请求体:{ "query":{ "match":{ "category" : "小华" } } }返回结果:{ "took": 2, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 9, "relation": "eq" }, "max_score": 0.93430924, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "l636ZIAB6CX3m_v3q_R5", "_score": 0.93430924, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 1999 } }, { "_index": "shopping", "_type": "_doc", "_id": "ma37ZIAB6CX3m_v3BvQc", "_score": 0.93430924, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 1999 } }, { "_index": "shopping", "_type": "_doc", "_id": "kq3nZIAB6CX3m_v3APT6", "_score": 0.49899113, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "k63tZIAB6CX3m_v3vfQI", "_score": 0.49899113, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "n60KZYAB6CX3m_v3q_Tc", "_score": 0.49899113, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oK0KZYAB6CX3m_v3rfQs", "_score": 0.49899113, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oa0KZYAB6CX3m_v3rvS_", "_score": 0.49899113, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oq0KZYAB6CX3m_v3sPQR", "_score": 0.49899113, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "o60KZYAB6CX3m_v3sfT8", "_score": 0.49899113, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } } ] } }完全匹配GET http://localhost:9200/shopping/_search{ "query":{ "match_phrase":{ "category" : "为" } } }返回结果:{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 0.93430924, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "l636ZIAB6CX3m_v3q_R5", "_score": 0.93430924, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 1999 } }, { "_index": "shopping", "_type": "_doc", "_id": "ma37ZIAB6CX3m_v3BvQc", "_score": 0.93430924, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 1999 } } ] } }高亮查询GET http://localhost:9200/shopping/_search请求体:{ "query":{ "match_phrase":{ "category" : "为" } }, "highlight":{ "fields":{ "category":{}//<----高亮这字段 } } }返回结果:{ "took": 19, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 0.93430924, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "l636ZIAB6CX3m_v3q_R5", "_score": 0.93430924, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 1999 }, "highlight": { "category": [ "华<em>为</em>" ] } }, { "_index": "shopping", "_type": "_doc", "_id": "ma37ZIAB6CX3m_v3BvQc", "_score": 0.93430924, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 1999 }, "highlight": { "category": [ "华<em>为</em>" //<------高亮一个为字。 ] } } ] } }聚合查询分组查询GET http://localhost:9200/shopping/_search类似与关系型数据库中的 group by,当然还有很多其他的聚合,例如取最大值max、平均值avg等等。接下来按price字段进行分组:请求体:{ "aggs":{//聚合操作 "price_group":{//名称,随意起名 "terms":{//分组 "field":"price"//分组字段 } } } }返回结果:{ "took": 11, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 9, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "kq3nZIAB6CX3m_v3APT6", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "k63tZIAB6CX3m_v3vfQI", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "l636ZIAB6CX3m_v3q_R5", "_score": 1, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 1999 } }, { "_index": "shopping", "_type": "_doc", "_id": "ma37ZIAB6CX3m_v3BvQc", "_score": 1, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 1999 } }, { "_index": "shopping", "_type": "_doc", "_id": "n60KZYAB6CX3m_v3q_Tc", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oK0KZYAB6CX3m_v3rfQs", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oa0KZYAB6CX3m_v3rvS_", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oq0KZYAB6CX3m_v3sPQR", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "o60KZYAB6CX3m_v3sfT8", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } } ] }, "aggregations": { "price_group": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": 3999, "doc_count": 7 }, { "key": 1999, "doc_count": 2 } ] } } }上面返回结果会附带原始数据的。若不想要不附带原始数据的结果,可以使用下面的方式:请求体:{ "aggs":{ "price_group":{ "terms":{ "field":"price" } } }, "size":0 }返回结果:{ "took": 4, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 9, "relation": "eq" }, "max_score": null, "hits": [] }, "aggregations": { "price_group": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": 3999, "doc_count": 7 }, { "key": 1999, "doc_count": 2 } ] } } }平均值GET所有手机价格求平均值。请求体:{ "aggs":{ "price_avg":{//名称,随意起名 "avg":{//求平均 "field":"price" } } }, "size":0 }返回结果:{ "took": 2, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 9, "relation": "eq" }, "max_score": null, "hits": [] }, "aggregations": { "price_avg": { "value": 3554.5555555555557 } } }
2022年04月27日
12 阅读
0 评论
0 点赞
2022-04-26
Elasticsearch的学习笔记(二)
数据格式Elasticsearch是面向文档型数据库,一条数据在这里就是一个文档。 为了方便大家理解,我们将 Elasticsearch 里存储文档数据和关系型数据库 MySQL 存储数据的概念进行一个类比ES 里的 Index 可以看做一个库,而 Types 相当于表, Documents 则相当于表的行。这里 Types 的概念已经被逐渐弱化, Elasticsearch 6.X 中,一个 index 下已经只能包含一个type, Elasticsearch 7.X 中, Type 的概念已经被删除了。创建索引PUT http://localhost:9200/shopping{ "acknowledged": true, "shards_acknowledged": true, "index": "shopping" }查看当前索引GET http://localhost:9200/shopping{ "shopping": {//索引名 "aliases": {},//别名 "mappings": {},//映射 "settings": {//设置 "index": {//设置 - 索引 "creation_date": "1650959730840",//设置 - 索引 - 创建时间 "number_of_shards": "1",//设置 - 索引 - 主分片数量 "number_of_replicas": "1",//设置 - 索引 - 主分片数量 "uuid": "Y2GAupofQFiHc5nsAlDe1w",//设置 - 索引 - 主分片数量 "version": {//设置 - 索引 - 主分片数量 "created": "7080099" }, "provided_name": "shopping"//设置 - 索引 - 主分片数量 } } } }查看当前ES中所有的索引GET http://localhost:9200/_cat/indices?vhealth status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open shopping Y2GAupofQFiHc5nsAlDe1w 1 1 0 0 208b 208b表头含义health当前服务器健康状态: green(集群完整) yellow(单点正常、集群不完整) red(单点不正常)status索引打开、关闭状态index索引名uuid索引统一编号pri主分片数量rep副本数量docs.count可用文档数量docs.deleted文档删除状态(逻辑删除)store.size主分片和副分片整体占空间大小pri.store.size主分片占空间大小删除索引DELETE http://localhost:9200/shopping{ "acknowledged": true }增加文档POST http://localhost:9200/shopping/_doc请求体JSON内容为:{ "title":"小米手机", "category":"小米", "images":"http://www.gulixueyuan.com/xm.jpg", "price":3999.00 }注意,此处发送请求的方式必须为 POST,不能是 PUT,否则会发生错误 。返回结果:{ "_index": "shopping", "_type": "_doc", "_id": "k63tZIAB6CX3m_v3vfQI", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 3, "_primary_term": 1 }上面的数据创建后,由于没有指定数据唯一性标识(ID),默认情况下, ES 服务器会随机生成一个。如果想要自定义唯一性标识,需要在创建时指定: http://127.0.0.1:9200/shopping/_doc/1001,这样就可以指定 ID 为 1001 。{ "_index": "shopping", "_type": "_doc", "_id": "1001", //自定义唯一ID标识 "_version": 3, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 4, "_primary_term": 1 }此处需要注意:如果增加数据时明确数据主键,那么请求方式也可以为 PUT。查询文档通过主键ID查询文档:GET http://localhost:9200/shopping/_doc/1001{ "_index": "shopping", "_type": "_doc", "_id": "1001", "_version": 3, "_seq_no": 4, "_primary_term": 1, "found": true, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }查询全部文档:GET http://localhost:9200/shopping/_search{ "took": 2, //查询花费时长(毫秒) "timed_out": false, // 请求是否超时 "_shards": { //搜索了多少分片,成功、失败或者跳过了多个分片(明细) "total": 1, //总分片数 "successful": 1, //成功分片数 "skipped": 0, //失败分片数 "failed": 0 // 跳过分片数 }, "hits": { "total": { "value": 3, // 找到的文档总数 "relation": "eq" }, "max_score": 1, 最相关的文档分数 "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "kq3nZIAB6CX3m_v3APT6", "_score": 1, //文档的相关性算分 (match_all 没有算分) "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "k63tZIAB6CX3m_v3vfQI", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "1001", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } } ] } }更新文档全局更新POST http://localhost:9200/shopping/_doc/1001请求JSON数据:{ "title":"华为手机", "category":"华为", "images":"http://www.gulixueyuan.com/hw.jpg", "price":1999.00 }返回结果:{ "_index": "shopping", "_type": "_doc", "_id": "1001", "_version": 10, "result": "updated", //updated 表示数据被更新 "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 13, "_primary_term": 1 }局部更新PUT http://localhost:9200/shopping/_update/1001请求JSON数据:{ "doc": { "title":"小米手机", "category":"小米" } }返回结果:{ "_index": "shopping", "_type": "_doc", "_id": "1001", "_version": 11, "result": "updated", //updated 表示数据被更新 "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 14, "_primary_term": 1 }删除文档DELETE http://localhost:9200/shopping/_doc/1001返回结果:{ "_index": "shopping", "_type": "_doc", "_id": "1001", "_version": 12, "result": "deleted", // deleted 表示数据被删除 "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 15, "_primary_term": 1 }查询文档查询全部GET http://localhost:9200/shopping/_search返回结果数据:{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 4, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "kq3nZIAB6CX3m_v3APT6", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "k63tZIAB6CX3m_v3vfQI", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "l636ZIAB6CX3m_v3q_R5", "_score": 1, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 1999 } }, { "_index": "shopping", "_type": "_doc", "_id": "ma37ZIAB6CX3m_v3BvQc", "_score": 1, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 1999 } } ] } }按条件查询(URL带参查询)GET http://localhost:9200/shopping/_search?q=category:小米返回结果:{ "took": 19, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 1.8889232, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "kq3nZIAB6CX3m_v3APT6", "_score": 1.8889232, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "k63tZIAB6CX3m_v3vfQI", "_score": 1.8889232, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } } ] } }按条件查询(请求体带参查询)POST http://localhost:9200/shopping/_search请求体:{ "query": { "match": { "category": "小米" } } }返回结果:{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 7, "relation": "eq" }, "max_score": 0.99798226, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "kq3nZIAB6CX3m_v3APT6", "_score": 0.99798226, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "k63tZIAB6CX3m_v3vfQI", "_score": 0.99798226, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "n60KZYAB6CX3m_v3q_Tc", "_score": 0.99798226, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oK0KZYAB6CX3m_v3rfQs", "_score": 0.99798226, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oa0KZYAB6CX3m_v3rvS_", "_score": 0.99798226, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oq0KZYAB6CX3m_v3sPQR", "_score": 0.99798226, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "o60KZYAB6CX3m_v3sfT8", "_score": 0.99798226, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } } ] } }查询全部数据(请求体带参查询)POST http://localhost:9200/shopping/_search请求体:{ "query": { "match_all": {} } }等同于下面此接口GET http://127.0.0.1:9200/shopping/_search返回结果:{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 9, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "kq3nZIAB6CX3m_v3APT6", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "k63tZIAB6CX3m_v3vfQI", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "l636ZIAB6CX3m_v3q_R5", "_score": 1, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 1999 } }, { "_index": "shopping", "_type": "_doc", "_id": "ma37ZIAB6CX3m_v3BvQc", "_score": 1, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 1999 } }, { "_index": "shopping", "_type": "_doc", "_id": "n60KZYAB6CX3m_v3q_Tc", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oK0KZYAB6CX3m_v3rfQs", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oa0KZYAB6CX3m_v3rvS_", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "oq0KZYAB6CX3m_v3sPQR", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } }, { "_index": "shopping", "_type": "_doc", "_id": "o60KZYAB6CX3m_v3sfT8", "_score": 1, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": 3999 } } ] } }
2022年04月26日
24 阅读
0 评论
0 点赞
2022-04-13
Elasticsearch的学习笔记(一)
今天早上来公司,就开始了学习elasticsearch,结果万万没想到,直接是一波三折呀,才把这个启动起来。先从官网下载压缩包解压后得到这几个文件夹详解:目录名称介绍bin二进制脚本,包含启动命令和安装插件命令等config配置文件目录lib依赖包目录logs日志文件目录modules模块库plugins插件目录data数据储存目录然后点击启动bin目录下的elasticsearch.bat启动,然后我的就变成了这样就一直再解决,最后修改了elasticsearch.yml才得以成功访问修改配置注意:9300端口为Elasticsearch集群间组件的通信端口,9200端口为浏览器访问的http协议RESTful端口。把有几个的true改为false就OK了最后来安装个插件试试安装插件我们先来看看有无插件(本机)执行命令 elasticsearch-plugin list然后安装个analysis-icu插件试试执行 elasticsearch-plugin install analysis-icu这样就安装成功了再来用哪个list命令浏览器查看(http://127.0.0.1:9200/_cat/plugins)好了 完了 继续学习!!!
2022年04月13日
53 阅读
1 评论
0 点赞