es基本操作

索引

PUT http://localhost:9200/job  // 创建索引
POST http://localhost:9200/job/_open  // 启动索引
POST http://localhost:9200/job/_close  // 关闭索引
GET http://localhost:9200/job  // 获取指定索引元数据
GET http://localhost:9200/_all  // 获取所有索引元数据
GET http://localhost:9200/_cat/indices?v  // 获取所有索引摘要数据
DELETE http://localhost:9200/job  // 删除索引

mapping

PUT http://localhost:9200/job/_mapping  // 创建mapping, Body:raw json

{
    "properties":{
        "title": {
            "type": "text"
        },
        "desc": {
            "type": "text"
        },
        "salary": {
            "type": "integer"
        },
        "city": {
            "type": "keyword"
        }
    }
}
GET http://localhost:9200/job/_mapping  // 获取指定索引的映射
GET http://localhost:9200/_mapping  // 获取所有索引的映射

文档

POST http://localhost:9200/job/_create/1  // 创建文档
{
    "title": "java高级工程师",
    "desc": "熟悉es,熟悉kafka",
    "salary": 22000,
    "city": "福州市"
}
POST http://localhost:9200/job/_doc/1  // 重建文档
{
    "title": "java高级工程师",
    "desc": "熟悉es,熟悉kafka,熟悉mysql",
    "salary": 22000,
    "city": "福州市"
}
GET http://localhost:9200/job/_doc/1  // 获取单个文档
POST http://localhost:9200/job/_doc/_mget  // 获取多个文档
{
    "ids": [1, 2]
}
DELETE http://localhost:9200/job/_doc/1  // 删除单个文档
GET http://localhost:9200/job/_search  // 获取所有文档

批量增删改bulk

POST http://localhost:9200/_bulk
{"index": {"_index": "job", "_type": "_doc", "_id": "15722"}}
{"jid":15722,"title":"java开发(北京)","salary":{"gte":8000,"lte":15000},"city":"北京","company":"南京华飞数据技术有限公司","description":" 岗位职责:1. 文档编写:根据项目经理的要求,参与开发设计文档的编写"}
{"create": {"_index": "job", "_type": "_doc", "_id": "15727"}}
{"jid":15727,"title":"java开发(北京)","salary":{"gte":8000,"lte":15000},"city":"北京","company":"南京华飞数据技术有限公司","description":" 岗位职责:1. 文档编写:根据项目经理的要求,参与开发设计文档的编写"}
{"delete": {"_index": "job", "_type": "_doc", "_id": "15726"}}

第一行说明
index:代表操作类型index-重建索引 , create-创建, delete-删除
_index:代表指向索引名
_type:固定为_doc
_id:要操作的数据id
第二行则是要操作的数据,在postman中最后要加一个空行

URI Query

GET http://localhost:9200/job/_search?q=SpringBoot&df=description&sort=jid:desc&from=0&size=10&timeout=1s  // 标准形式
GET http://localhost:9200/job/_search?q=description:SpringBoot  // 单字段查询
GET http://localhost:9200/job/_search?q=description:"spring cloud"  // phrase(短语)查询
GET http://localhost:9200/job/_search?q=description:(spring AND mysql)  // boolean查询
GET http://localhost:9200/job/_search?q=description:(spring OR mysql)  // boolean查询
GET http://localhost:9200/job/_search?q=description:(spring NOT mysql)  // boolean查询
GET http://localhost:9200/job/_search?q=salary:>=20000  // 范围查询
GET http://localhost:9200/job/_search?q=title:java*  // 通配符查询
GET http://localhost:9200/job/_search?q=description:SpringBot~1  // 单字段模糊匹配
GET http://localhost:9200/job/_search?q=description:"cloud spring"~2  // phrase(短语)模糊匹配

DSL Query

GET|POST http://localhost:9200/job/_search

  • 基本结构
{
    "from": 0,
    "size": 10,
    "sort": [{"jid": "desc"}],
    "_source": ["jid", "title", "city"],
    "query": {
        "macth_all": {}
    }
}
  • 精准匹配term
{
   "query": {
        "term": {
            "jid": 15726
        }
    }
}

{
   "query": {
        "terms": {
            "jid": [15726, 15725]
        }
    }
}
  • 单子段全文检索
{
   "query": {
        "match": {
            "description": "java架构师"
        }
    }
}

{
   "query": {
        "match": {
            "description": {
                 "query": "java 架构师",
                 "operator": "or"
            }
        }
    }
}
  • 多字段全文查询
{
   "query": {
        "multi_match": {
              "query": "金融",
              "fields": ["title", "description"]
        }
    }
}
  • 短语查询
{
   "query": {
        "match_phrase": {
            "description": {
                 "query": "was tomcat",
                 "slop": 1
            }
        }
    }
}
  • 范围查询
{
   "query": {
        "range": {
            "salary": {
                 "gte": "5000",
                 "lte": "10000"
            }
        }
    }
}
  • bool多字段复合查询

任务需求
某猎头公司为客户匹配工作岗位,客户要求工作地在北京,工资不得低于6000的java工程师岗位,项目要求使用spring cloud技术,如涉及shell编程的则重点考虑。

{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "title": {
                        "query": "java 工程师",
                        "operator": "or"
                    }
                }
            },
            "filter": {
                "match": {
                    "city": "北京"
                }
            },
            "must_not": {
                "range": {
                    "salary": {
                        "lt": "15000"
                    }
                }
            },
            "should": [{
                "match_phrase": {
                    "description": {
                        "query": "spring cloud",
                        "boost": 2
                    }
                }},{
                "match": {
                    "description": "shell"
                }
            }],
            "minimum_should_match": 1
        }
    }
}

ik分词器安装

  1. 下载安装包,要与es版本相同
  2. 在es目录下plugin里创建ik文件夹,将安装包解压到该文件夹
  3. 重启es
elasticsearch‐plugin list

调试分词

GET http://localhost:9200/_analyze
{
  "analyzer" : "ik_max_word",  // ik_smart
  "text" : ["马保国语录:这两个年轻人不讲武德,来骗,来偷袭,我六十九岁的老同志,这好吗?这不好,我劝这位年轻人耗子尾汁,好好反思,以后不要再犯这样的聪明"]
}

Completion Suggester 自动完成

需要作为自动完成搜索的字段需要增加一个附属字段,如下

- 新建索引
- 创建mapping
POST http://localhost:9200/job1/_mapping
{
    "properties": {
        "jid" : {
            "type" : "long"
        },
        "title": {
            "type": "text"
        },
        "title_completion": {  // here
            "type": "completion"
        },
        "salary_text" : {
            "type": "text",
            "index": false
        },
        "city": {
            "type": "keyword"
        }
    }
}
  • bulk导入数据,title_completion与title内容相同
  • 前端每次输入框变化都需要查询这个api
GET http://127.0.0.1:9200/job1/_search
{
    "suggest": {
        "title‐suggest": {
            "prefix": "java工程师",
            "completion": {
                "field": "title_completion",
                "skip_duplicates": true,
                "analyzer": "ik_max_word",
                "size": 10
            }
        }
    }
}

聚合统计

  • 创建mapping时可以将预知会进行分组的字段(必须是keyword)设置eager_global_ordinals:true,则es在索引新文档时会进行分组预处理,将分组数据缓存,这样在分组聚合查询时大幅提高性能。
POST http://localhost:9200/employee/_mapping
{
    "properties": {
        "empno": {
            "type": "keyword"
        },
        "name": {
            "type": "keyword"
        },
        "job": {
            "type": "keyword"
        },
        "salary": {
            "type": "integer"
        },
        "age": {
            "type": "integer"
        },
        "gender": {
            "type": "keyword"
        },
        "dname": {
            "type": "keyword",
            "eager_global_ordinals": true
        }
    }
}
  • 单字段统计
    类似 select min(salary),avg(salary),max(salary) from employee group by salary
GET http://localhost:9200/employee/_search
{
    "size": 0,
    "aggs": {
        "min_salary": {
            "min": {
                "field": "salary"
            }
        },
        "max_salary": {
            "max": {
                "field": "salary"
            }
        },
        "avg_salary": {
            "avg": {
                "field": "salary"
            }
        }
    }
}
{
    "size": 0,
    "aggs": {
        "stats_salary": {
            "stats": {
                "field": "salary"
            }
        }
    }
}
  • bucket统计
    统计每个部门的人数
    类似 select count(*),dbname from employee group by dnname
GET http://localhost:9200/employee/_search
{
    "size": 0,
    "aggs": {
        "department": {
            "terms": {
                "field": "dname"
            }
        }
    }
}

获取每个部门工资最高的两个人的记录
类似 select * from employee group by dnname order by salary limit 2

GET http://localhost:9200/employee/_search
{
    "size": 0,
    "aggs": {
        "department": {
            "terms": {
                "field": "dname"
            },
            "aggs": {
                "top_salary": {
                    "top_hits": {
                        "size": 2,
                        "sort": [
                            {
                                "salary": {
                                    "order": "desc"
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
}
  • range 范围分组
    统计各年龄段最高工资
GET http://localhost:9200/employee/_search
{
    "size": 0,
    "aggs": {
        "age_range": {
            "range": {
                "field": "age",
                "ranges": [
                    {
                        "key": "20岁以下",
                        "to": 20
                    },
                    {
                        "key": "20‐30岁",
                        "from": 20,
                        "to": 30
                    },
                    {
                        "key": "30‐40人岁",
                        "from": 30,
                        "to": 40
                    },
                    {
                        "key": "40‐50岁",
                        "from": 40,
                        "to": 50
                    },
                    {
                        "key": "50岁以上",
                        "from": 50
                    }
                ]
            },
            "aggs": {
                "top_salary": {
                    "top_hits": {
                        "size": 1,
                        "sort": [
                            {
                                "salary": {
                                    "order": "desc"
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
}
  • Histogram阶梯分桶
    按5岁一个阶梯统计各阶段工资汇总数据
{
    "size": 0,
    "aggs": {
        "age_histogram": {
            "histogram": {
                "field": "age",
                "interval": 5,
                "extended_bounds": {
                    "min": 18,
                    "max": 65
                }
            },
            "aggs": {
                "stats_salary": {
                    "stats": {
                        "field": "salary"
                    }
                }
            }
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容