elasticsearch 基本操作

elasticsearch 基本操作

环境 docker deeping-20

java实现

elasticsearch-contact

  • 创建索引

    • curl -X PUT http://127.0.0.1:9200/shopping
  • 查询索引

    • curl -X GET http://127.0.0.1:9200/_cat/indices?v
  • 删除索引

  • curl -X DELETE http://127.0.0.1:9200/shopping

  • 文档创建

	# 默认ID
  curl -X POST -d '{"title":"华为","price":1999.00}' -H "Content-type: application/json" http://127.0.0.1:9200/shopping/_doc
  #1001为id
  curl -X POST -d '{"title":"华为","price":1999.00}' -H "Content-type: application/json" http://127.0.0.1:9200/shopping/_doc/1001 
  #1002为id
  curl -X POST -d '{"title":"华为","price":1999.00}' -H "Content-type: application/json" http://127.0.0.1:9200/shopping/_create/1002 
  • 主键查询

    • curl -X GET http://127.0.0.1:9200/shopping/_doc/1002
  • 全查询

    • curl -X GET http://127.0.0.1:9200/shopping/_search
  • 修改

    • #完全覆盖
      curl -X PUT -d '{"title":"华为","price":2999.00}' -H "Content-type: application/json" http://127.0.0.1:9200/shopping/_doc/1001
      #局部修改 
        	curl -X POST -d '{"doc":{"price":2980.00}}' -H "Content-type: application/json" http://127.0.0.1:9200/shopping/_update/1001
      #删除文档
      curl -X DELETE http://127.0.0.1:9200/shopping/_doc/1001
      

复杂查询

  • curl -X GET http://127.0.0.1:9200/shopping/_search?q=title:华为
    
  • curl -X GET -d '{"query":{"match":{"title":"华为"}}}' -H "Content-type: application/json" http://127.0.0.1:9200/shopping/_search
    

分页查询

curl -X GET -d '{"query":{"match_all":{}},"from":2,"size":2,"_source":["title"]}' -H "Content-type:application/json" http://127.0.0.1:9200/shopping/_search
## 分页查询 from:(页码-1)*每页数量
//查询参数结构
{
	"query": {
		"match_all": {} //全部查询
	},
	"from": 0, //第一页
	"size": 2, //每页两条
	"_source": ["title"], //文档中想要展示的字段
    "sort":{
        "price":{ //排序字段
            "order":"desc" //排序规则
        }
    }
}

多条件查询

//与
{
    "query":{
        "bool":{
            "must":[ //and查询
                {
                    "match":{
                        "title":"华为"
                    }
                },
                {
                    "match":{
                        "price":"1999.00"
                    }
                }
            ]
        }
    }
}
//或
{
    "query":{
        "bool":{
            "should":[ //or查询
                {
                    "match":{
                        "title":"华为"
                    }
                },
                {
                    "match":{
                        "title":"小米"
                    }
                }
            ]
        }
    }
}
//过滤

{
    "query":{
        "bool":{
            "should":[ //or查询
                {
                    "match":{
                        "title":"华为"
                    }
                },
                {
                    "match":{
                        "title":"小米"
                    }
                }
            ],
            "filter":{
                "range":{
                    "price":{
                        "gt":2000
                    }
                }
            }
        }
    }
}

文档全文查询

//分词查询-全文检索匹配
{
    "query":{
        "match":{ 
            "title":"小为" //此时会将字段分词为 小 、为 、小为进行查询,倒排查询
        }
    }
}
//精确查询-完全匹配
{
    "query":{
        "match_phrase":{ 
            "title":"小为" //结果为0
        }
    }
}

高亮查询

{
    "query":{
        "match_phrase":{ 
            "title":"华为" 
        }
    },
    "highlight":{
        "fileds":{
            "title":{}
        }
    }
}

聚合查询

{
    "aggs":{ //聚合
        "price_group":{ //自定义分组名称
            "terms":{ // 分组
                "field":"price" //分组字段
            }
        }
    },
    "size":0 //原始数据返回数量
}
//平局值
{
    "aggs":{ //聚合
        "price_avg":{ //自定义分组名称
            "avg":{ // 分组
                "field":"price" //分组字段
            }
        }
    },
    "size":0 //原始数据返回数量
}

映射关系

创建索引

curl -X PUT http://127.0.0.1:9200/user
配置映射
{
   "properties":{
       "name":{
           "type":"text", //可以全文检索
           "index":true  //可以索引查询
       },
       "sex":{
           "type":"keyword",//必须完全匹配
           "index":true //可以索引查询
       },
       "tel":{
           "type":"keyword",//必须完全匹配
           "index":false //不可以索引查询
       }
   }
}
执行
# 创建映射
curl -X PUT -d '{?}' -H "Content-type:application/json" http://127.0.0.1:9200/user/_mapping
# 获取映射
curl -X GET http://127.0.0.1:9200/user/_mapping
加入数据
curl -X POST -d '{"name":"小花","sex":"女","tel":1233}' -H "Content-type: application/json" http://127.0.0.1:9200/user/_create/10001

curl -X POST -d '{"name":"小李","sex":"男的","tel":1223}' -H "Content-type: application/json" http://127.0.0.1:9200/user/_create/10002

curl -X POST -d '{"name":"张三","sex":"男","tel":1213}' -H "Content-type: application/json" http://127.0.0.1:9200/user/_create/10003
测试查询
curl -X POST -d '{?}' http://127.0.0.1:9200/user/_search
//查询--名字为全文检索
{
    "query":{
        "match":{
            "name":"小王"
        }
    },
    "_source":["name"]
}
//结果
{
    //...
    "hits": {
       //...
        "hits": [
            {
                //...
                "_source": {
                    "name": "小花"
                }
            },
            {
              	//...
                "_source": {
                    "name": "小李"
                }
            }
        ]
    }
}
//查询--性别为完全匹配
{
    "query":{
        "match":{
            "sex":"男"
        }
    },
    "_source":["sex"]
}
//结果
{
   //...
    "hits": {
        //...
        "hits": [
            {
               //...
                "_source": {
                    "sex": "男"
                }
            }
        ]
    }
}
//查询手机号
{
    "query":{
        "match":{
            "tel":"111"
        }
    },
    "_source":["tel"]
}
//结果--不可查询
failed to create query: Cannot search on field [tel] since it is not indexed.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值