赞
踩
config/elasticsearch.yml
配置文库,写入下面配置。- xpack.security.enabled: true
- xpack.license.self_generated.type: basic
- xpack.security.transport.ssl.enabled: true
./elasticsearch-setup-passwords interactive
默认账号:elastic
浏览器访问:http://localhost:9200/
,此时浏览器弹出需要密码了。
访问控制台:http://127.0.0.1:9100/?auth_user=elastic&auth_password=密码
es基本操作命令
类似mysql创建表
- # 创建名称为shapping的索引
- PUT http://127.0.0.1:9200/shapping
- {
- "acknowledged": true, //创建成功
- "shards_acknowledged": true, //示索引创建、更新或删除的操作是否已被所有相关分片确认。
- "index": "shapping" //索引名为shapping
- }
索引具有幂等性,重复创建会报错
- get http://127.0.0.1:9200/shapping
-
- {
- "shapping": {
- "aliases": {},
- "mappings": {},
- "settings": {
- "index": {
- "routing": {
- "allocation": {
- "include": {
- "_tier_preference": "data_content"
- }
- }
- },
- "number_of_shards": "1",
- "provided_name": "shapping",
- "creation_date": "1692721844404",
- "number_of_replicas": "1",
- "uuid": "VeVq2ZAWQAyJtAcjmW6z8Q",
- "version": {
- "created": "8020199"
- }
- }
- }
- }
- }

- get http://127.0.0.1:9200/_cat/indices?v
-
- v : 显示详细信息
-
-
- health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
- yellow open shapping VeVq2ZAWQAyJtAcjmW6z8Q 1 1 0 0 225b 225b
- delete http://127.0.0.1:9200/shapping
-
-
- {
- "acknowledged": true
- }
- 请求方式 :post
- 路径 : http://127.0.0.1:9200/shapping/_doc
- http://127.0.0.1:9200/shapping/_doc/id #自己指定ID
- //也可以吧_doc换成_create,这样看起来更加规范 http://127.0.0.1:9200/shapping/_create/ID
- //这样请求会自动创建ID,如:"_id": "4iMnHooBhKSDaY6labt-",
- //自定义创建ID方式在_doc后面加上ID : http://127.0.0.1:9200/shapping/_doc/1001, 1001 是这个数据的ID
-
- 请求体:
-
- {
- "title" : "小米手机",
- "img" : "http://www.test.com/1.jpg",
- "price" : "3999"
- }
-
- 返回结果:
- {
- "_index": "shapping",
- "_id": "4iMnHooBhKSDaY6labt-",
- "_version": 1,
- "result": "created",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 0,
- "_primary_term": 1
- }

- get http://127.0.0.1:9200/shapping/_doc/1000
-
- 返回:
- {
- "_index": "shapping",
- "_id": "1000",
- "_version": 1,
- "_seq_no": 1,
- "_primary_term": 1,
- "found": true,
- "_source": {
- "title": "小米手机",
- "img": "http://www.test.com/1.jpg",
- "price": "3999"
- }
- }

- get http://127.0.0.1:9200/shapping/_search
-
- {
- "took": 328,
- "timed_out": false,
- "_shards": {
- "total": 1,
- "successful": 1,
- "skipped": 0,
- "failed": 0
- },
- "hits": {
- "total": {
- "value": 2,
- "relation": "eq"
- },
- "max_score": 1.0,
- "hits": [
- {
- "_index": "shapping",
- "_id": "4iMnHooBhKSDaY6labt-",
- "_score": 1.0,
- "_source": {
- "title": "小米手机",
- "img": "http://www.test.com/1.jpg",
- "price": "3999"
- }
- },
- {
- "_index": "shapping",
- "_id": "1000",
- "_score": 1.0,
- "_source": {
- "title": "小米手机",
- "img": "http://www.test.com/1.jpg",
- "price": "3999"
- }
- }
- ]
- }
- }

局部数据修改(推荐)
- post http://127.0.0.1:9200/shapping/_update/1000
-
- 请求json体
-
- // 华为修改为苹果
-
- {
- "doc" :{
- "title" : "苹果手机"
- }
- }
-
-
全量修改:
需要把json中的每个字段都写上(不推荐)
-
- put http://127.0.0.1:9200/shapping/_doc/1000
-
- //需要修改的数据
-
- {
- "title" : "华为手机", //小米改华为
- "img" : "http://www.test.com/123.jpg",
- "price" : "399999"
- }
delete http://127.0.0.1:9200/shapping/_doc/1000
【1】
- PUT /demo/_mapping
-
- {
- "properties":{
- "name":{
- "type":"text", // 类型为text 【text会被分词】
- "index":true //当前name建立索引【可以被索引查询】
- },
- "age":{
- "type":"keyword", // 不会被分词
- "index":true
- },
- "tel":{
- "type":"keyword",
- "index":false // 索引中不缓存tel,不能通过tel 搜索
- }
- }
- }

GET _cat/indices?v
GET api
- POST /api/_doc #给api索引添加数据,自动分配ID
- #/api/_doc/1 指定ID为1
- #/api/_create/100
- # _create == _doc
-
- {
- "name":"123"
- }
- GET api/_doc/1 #查询指定文档
- GET /api/_search #查询所有文档
DELETE /api
- PUT /api/_doc/1
- #全部修改
-
- {
- "name":"66666"
- }
-
-
- 【return】
- {
- "_index": "api",
- "_id": "1",
- "_version": 2,
- "result": "updated",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 17,
- "_primary_term": 1
- }

- POST /api/_update/1000
-
-
- {
- "doc":{
- "age":666
- }
-
- }
-
- q: 查询条件 age = 1
- http://127.0.0.1:9200/api/_search?q=age:1
- get /api/_search
-
- // 查询name=a,根据age 小到大排序
- {
- "query" : { //query 查询
- "match":{ //match 匹配查询 match_all 查询所有
- "name":"a" //要查询的字段name=a
- }
- },
- "from":0, //分页其实值
- "size":6 , //查询总数
- "_source":["age"] , //只显示age
- "sort":{
- "age":{ // 根据age循序排序
- "order":"asc"
- }
- }
-
- }
-
- ==================================
- 【return】
- {
- "took": 1,
- "timed_out": false,
- "_shards": {
- "total": 1,
- "successful": 1,
- "skipped": 0,
- "failed": 0
- },
- "hits": {
- "total": {
- "value": 13, //返回匹配的总数
- "relation": "eq"
- },
- "max_score": null,
- "hits": [
- {
- "_index": "api",
- "_id": "YkFG_oABrskZ6DWlXrba",
- "_score": null,
- "_source": {
- "age": 1
- },
- "sort": [
- 1
- ]
- }
- ]
- }
- }

- {
- "query" : {
- "bool":{
- "should":[ // 必须一个条件即可 or
- {
- "match":{
- "name" :"a"
- }
- },
- {
- "match":{
- "age":5
- }
- }
- ]
- }
- }
-
- }
-

- {
- "query" : {
- "bool":{
- "must":[ // 同时满足以下条件
- {
- "match":{
- "name" :"小米"
- }
- },
- {
- "match":{
- "status":1
- }
- }
- ],
- "filter":{ //二次过滤
- "range": {
- "money": {
- "gt":1000
- }
- }
- }
- }
- },
- "highlight" : { //高亮显示
- "fields":{
- "name": { //高亮显示的字段
- "pre_tags": ["<strong>"], //亮显示的字段的起始标签
- "post_tags": ["</strong>"] //亮显示的字段的结束标签
- }
- }
- }
-
- }
-

- // get /api/_search
-
- {
- "aggs":{ //聚合操作
- "money_group":{ //分组名称【随便取名】
- "terms":{ //分组关键词【类似mysql group关键词】
- "field":"money" //分组的字段
- }
- },
- "money_avg":{ //平均数名 随便取名
- "avg":{ //平均数
- "field":"money" //需要求平均数的字段
- }
- }
-
-
- },
- "size":0 //只看聚合数据内容,不查看原始数据
- }
-
- //【return】
- {
- "took": 52,
- "timed_out": false,
- "_shards": {
- "total": 1,
- "successful": 1,
- "skipped": 0,
- "failed": 0
- },
- "hits": {
- "total": {
- "value": 64,
- "relation": "eq"
- },
- "max_score": null,
- "hits": []
- },
- "aggregations": {
- "money_avg": {
- "value": 9680.818181818182
- },
- "money_group": {
- "doc_count_error_upper_bound": 0,
- "sum_other_doc_count": 6,
- "buckets": [
- {
- "key": 2999,
- "doc_count": 3
- },
- {
- "key": 5999,
- "doc_count": 3
- },
- {
- "key": 1999,
- "doc_count": 2
- },
- {
- "key": 3999,
- "doc_count": 2
- },
- {
- "key": 299,
- "doc_count": 1
- },
- {
- "key": 399,
- "doc_count": 1
- },
- {
- "key": 699,
- "doc_count": 1
- },
- {
- "key": 799,
- "doc_count": 1
- },
- {
- "key": 999,
- "doc_count": 1
- },
- {
- "key": 4999,
- "doc_count": 1
- }
- ]
- }
- }
- }

- PUT /test/_mapping
-
-
- {
- "mappings":{
- "properties":{
- "name":{
- "type":"text",
- "index":true
- },
- "age":{
- "type":"keyword",
- "index":true
- },
- "tel":{
- "properties":{
- "msg":{
- "type":"keyword",
- "index":true
- },
-
- "data":{
- "properties":{
- "resu":{
- "type":"text",
- "index":true
- }
- }
- }
- }
- }
- }
-
- }
- }

- post /test/_doc/
-
- {
- "name":"小米3",
- "age":3,
- "tel":{
- "msg": 3,
- "data":{
- "resu": "hello world"
- }
- }
- }
- get /test/_search
-
- {
- "query" : {
- "match":{
- "tel.data.resu":"hello"
- }
- }
- }
-
- //【return】
-
- {
- "took": 1,
- "timed_out": false,
- "_shards": {
- "total": 1,
- "successful": 1,
- "skipped": 0,
- "failed": 0
- },
- "hits": {
- "total": {
- "value": 2,
- "relation": "eq"
- },
- "max_score": 0.52354836,
- "hits": [
- {
- "_index": "test",
- "_id": "hkFkBIEBrskZ6DWlrbYT",
- "_score": 0.52354836,
- "_source": {
- "name": "小米",
- "age": 1,
- "tel": {
- "msg": 1,
- "data": {
- "resu": "hello"
- }
- }
- }
- },
- {
- "_index": "test",
- "_id": "iEFlBIEBrskZ6DWlR7aJ",
- "_score": 0.39019167,
- "_source": {
- "name": "小米3",
- "age": 3,
- "tel": {
- "msg": 3,
- "data": {
- "resu": "hello world"
- }
- }
- }
- }
- ]
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。