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.
上一篇:es集群安装配置及常用命令


下一篇:ES 增删改查 批量操作