es 通配符查询多个索引 es查询所有索引的名称_字段

小贝查询到宝贝、贝贝

这就类似于你在京东查询一个商品,可能没有一样的,但是并不是显示没有,而是显示类似的

使用match_phrase可以进行完全匹配查询

es 通配符查询多个索引 es查询所有索引的名称_面试_02

完全匹配小贝没有结果

高亮查询

添加highlight

es 通配符查询多个索引 es查询所有索引的名称_es 通配符查询多个索引_03

高亮部分添加了em标签

聚合查询

使用aggs,表示聚合操作,嵌套分组terms,添加分组字段

es 通配符查询多个索引 es查询所有索引的名称_es 通配符查询多个索引_04

聚合查询

可以看到19 20 18岁的doc数量

映射

将字段映射为关键字或文本类型,关键字不可拆分,设置index为false时不可拆分。

添加索引killer,给killer添加映射,http:///ip:9200/index_name/_mapping,方法使用PUT

{
    "properties":{
        "name":{
            "type":"keyword",
            "index":true
        },
        "hobby":{
            "type":"text",
            "index":true
        }
    }
}

es 通配符查询多个索引 es查询所有索引的名称_网络安全_05

添加映射

名字是关键字,不可拆分,爱好是文本,可以拆分。

添加两个文档

{
    "name":"弗兰克",
    "hobby":"枪杀"
}
{
    "name":"弗兰克林",
    "hobby":"捧杀"
}

es 通配符查询多个索引 es查询所有索引的名称_网络安全_06

按名字查询

es 通配符查询多个索引 es查询所有索引的名称_学习_07

按爱好查询

可以看到名字作为关键字,必须一样才能查到,爱好却能查到类似的

修改

全量修改

http://ip:9200/index_name/_doc/_id,使用PUT方法,添加请求体。

es 通配符查询多个索引 es查询所有索引的名称_面试_08

全量修改

可以看到上面数据没有全部改动,大部分情况下还是部分修改的。

局部修改

http://ip:9200/index_name/_update/_id,方法为POST,请求体使用doc字段嵌套修改内容,见下图。

es 通配符查询多个索引 es查询所有索引的名称_网络安全_09

部分修改

删除

http://ip:9200/index_name/_doc/_id,方法使用DELETE

es 通配符查询多个索引 es查询所有索引的名称_网络安全_10

删除文档

Python API

又支持异步的版本和不支持异步的,这里就使用简单的不支持异步的

安装与导入

pip install elasticsearch==7.10
from elasticsearch import Elasticsearch

es 通配符查询多个索引 es查询所有索引的名称_es 通配符查询多个索引_11

包内容

es 通配符查询多个索引 es查询所有索引的名称_网络安全_12

项目结构

使用单元测试对API进行简单测试。后续仅展示esclient里面的函数即test_函数

初始化

def __init__(self, host="127.0.0.1", port: int = 9200):
        self._client = Elasticsearch(host=host, port=port)
参数

host: es的ip

port: es的端口

测试代码

es = Elasticsearch()

索引

创建
def index_create(self, index_name: str, map=None):
        if map is None:
            map = {}
        res = self._client.index(index=index_name, body=map)
        return res

参数

index:索引名

body:请求体

测试代码

def test_index_create(self):
        res = es.index_create("lady1")
        print(res)
        res = es.index_create("heros", map={
            "mapping": {
                "doc": {
                    "properties": {
                        "name": {
                            "type": "text",
                            "index": True
                        },
                        "age": {
                            "type": "keyword",
                            "index": True
                        },
                        "role": {
                            "type": "keyword",
                            "index": True
                        }
                    }
                }
            }
        })
        print(res)

结果

es 通配符查询多个索引 es查询所有索引的名称_字段_13

使用wireshark抓包

es 通配符查询多个索引 es查询所有索引的名称_学习_14

可以看到是一个POST包,使用的是json格式,body是空的

es 通配符查询多个索引 es查询所有索引的名称_字段_15

es 通配符查询多个索引 es查询所有索引的名称_字段_16

这个是有body的,但是映射貌似也不对,没有向_mapping发送,后面我们自己发送一个PUT数据包

创建映射

测试代码

def test_mapping(self):
        import requests
        import json
        url_put = "http://127.0.0.1:9200/heros/_mapping"
        headers_put = {
            'Content-Type': "application/json"
        }

        param ={
            "properties": {
                "name": {
                    "type": "text",
                    "index": True
                },
                "role": {
                    "type": "keyword",
                    "index": True
                }
            }
        }
        payload = json.dumps(param)
        response_put = requests.put(url_put, data=payload, headers=headers_put)
        print(response_put.text)

测试结果

es 通配符查询多个索引 es查询所有索引的名称_es 通配符查询多个索引_17

之后就不再展示Wireshark抓包结果了

删除索引
def index_delete(self, index_name: str):
        res = self._client.indices.delete(index=index_name)
        return res

参数

index_name:索引的名字

测试代码

def test_index_delete(self):
        res = es.index_delete(index_name="lady1")
        print(res)

结果

es 通配符查询多个索引 es查询所有索引的名称_网络安全_18

查询

单个索引

def index_get(self, index_name: str):
        res = self._client.indices.get(index=index_name)
        return res

参数

index_name:索引的名字

测试代码

def test_index_get(self):
        res = es.index_get("heros")
        print(res)

结果

es 通配符查询多个索引 es查询所有索引的名称_网络安全_19

文档

创建
def doc_create(self, index_name, body, id_):
        res = self._client.create(index=index_name, body=body, id=id_)
        return res

参数

index_name:索引的名字

body:请求体

id_:文档的id

测试代码

def test_doc_create(self):
        zhaoyun = {
            "name": "赵云",
            "age": 25,
            "role": "打野"
        }
        yunzhongjun = {
            "name": "云中君",
            "age": 25,
            "role": "打野"
        }
        yao = {
            "name": "瑶",
            "age": 16,
            "role": "辅助"
        }
        sunshangxiang = {
            "name": "孙尚香",
            "age": 18,
            "role": "射手"
        }
        liubei = {
            "name": "刘备",
            "age": 30,
            "role": "打野"
        }
        heros = [zhaoyun, yunzhongjun, yao, sunshangxiang, liubei]
        init_id = 1000
        for h in heros:
            res = es.doc_create("heros", h, str(init_id))
            init_id += 1
            print(res)

结果

es 通配符查询多个索引 es查询所有索引的名称_学习_20

删除

def doc_delete(self, index_name, doc_id):
        res = self._client.delete(index=index_name, id=doc_id)
        return res

参数

index_name:索引的名字

doc_id:文档的id

一共就这几个数据,我就不删除了

修改
def doc_update(self, index_name, doc_id, doc):
        res = self._client.update(index=index_name, id=doc_id, body=doc)
        return res

参数

index_name:索引的名字

doc_id:文档的id

doc:文档

测试代码

def test_doc_update(self):
        sun = {
            "doc": {
                "age": 21
            }
        }
        res = es.doc_update("heros", "1002", sun)
        print(res["result"])

结果

es 通配符查询多个索引 es查询所有索引的名称_es 通配符查询多个索引_21

查询
查询所有
def doc_get__all(self, index_name):
        res = self._client.search(index=index_name)
        return res['hits']['hits']

参数

index_name:索引的名字

这里仅返回hit到的json对象列表

测试代码

def test_doc_get_all(self):
        res = es.doc_get__all("heros")
        for hit in res:
            print(hit["_source"])

结果

es 通配符查询多个索引 es查询所有索引的名称_es 通配符查询多个索引_22

根据id查询
def doc_get_id(self, index_name, id_):
        res = self._client.get(index=index_name, id=id_)
        return res["_source"]

参数

index_name:索引的名字

id_:文档的id

测试代码

def test_doc_get_id(self):
        res = es.doc_get_id("heros", "1002")
        print(res)

结果

es 通配符查询多个索引 es查询所有索引的名称_网络安全_23

根据条件查询
def doc_get_condition(self, index_name, con):
        res = self._client.search(index=index_name, body=con)
        return res['hits']['hits']

参数

index_name:索引的名字

con:查询的请求体,即条件

单个条件

测试代码

def test_doc_get_con_1(self):
        name = {
            "name": "小云"
        }
        con = {
            "query": {
            }
        }
        con["query"]["match"] = name
        res = es.doc_get_condition("heros", con)
        for hit in res:
            print(hit["_source"])

结果

es 通配符查询多个索引 es查询所有索引的名称_面试_24

因为name在前面设置映射时,设置为了text,所以即使不完全匹配也会有类似的结果。

分页查询

测试代码

def test_doc_get_page(self):
        page_num = 2
        size = 3
        con = {
            "from": (page_num - 1) * size,
            "size": size
        }
        res = es.doc_get_condition("heros", con)
        for hit in res:
            print(hit["_source"])

结果

es 通配符查询多个索引 es查询所有索引的名称_字段_25

对结果排序

测试代码

def test_doc_get_sort(self):
        page_num = 1
        size = 3
        field = "age"
        order = "desc"
        con = {
            "from": (page_num - 1) * size,
            "size": size,
            "sort": {
                field: {
                    "order": order
                }
            }
        }
        res = es.doc_get_condition("heros", con)
        for hit in res:
            print(hit["_source"])

结果

es 通配符查询多个索引 es查询所有索引的名称_es 通配符查询多个索引_26

多个条件查询

测试代码

def test_doc_get_con_more(self):
        eighteen_match = {
            "match": {
                "age": "18"
            }
        }
        daye_match = {
            "match": {
                "role": "打野"
            }
        }
        con = {
            "query": {
                "bool": {
                    "must_not": []
                }
            }
        }
        con["query"]["bool"]["must_not"].append(eighteen_match)
        con["query"]["bool"]["must_not"].append(daye_match)
        res = es.doc_get_condition("heros", con)
        for hit in res:
            print(hit["_source"])

结果

es 通配符查询多个索引 es查询所有索引的名称_字段_27

范围查询

测试代码

def test_doc_get_scope(self):
        filter = {
            "range": {
                "age": {
                    "gt": 19
                }
            }
        }
        con = {
            "query": {
                "bool": {
                    "filter": filter
                }
            }
        }
        res = es.doc_get_condition("heros", con)
        for hit in res:
            print(hit["_source"])

结果

es 通配符查询多个索引 es查询所有索引的名称_网络安全_28

完全匹配

测试代码

def test_doc_get_phrase(self):
        name = {
            "name": "赵云"
        }
        con = {
            "query": {
            }
        }
        con["query"]["match_phrase"] = name
        res = es.doc_get_condition("heros", con)
        for hit in res:
            print(hit["_source"])

结果

es 通配符查询多个索引 es查询所有索引的名称_es 通配符查询多个索引_29

高亮查询

测试代码

def test_doc_get_highlight(self):
        name = {
            "name": "云"
        }
        con = {
            "query": {
            },
            "highlight": {
                "fields": {
                    "name": {}
                }
            }
        }
        con["query"]["match"] = name
        res = es.doc_get_condition("heros", con)
        for hit in res:
            print(hit["highlight"])

结果

es 通配符查询多个索引 es查询所有索引的名称_字段_30

聚合查询
def doc_get__agg(self, index_name, con):
        res = self._client.search(index=index_name, body=con)
        return res['aggregations']

参数

index: 索引的名字

body:请求体,即聚合的字段

测试代码

def test_doc_get_aggs(self):
        con = {
            "aggs": {
                "role_groups": {


### 给大家的福利


**零基础入门**


对于从来没有接触过网络安全的同学,我们帮你准备了详细的学习成长路线图。可以说是最科学最系统的学习路线,大家跟着这个大的方向学习准没问题。


![](https://s2.51cto.com/images/blog/202406/25211421_667ac2adcb37f74586.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)


同时每个成长路线对应的板块都有配套的视频提供:


![在这里插入图片描述](https://s2.51cto.com/images/blog/202406/25211422_667ac2ae0533f59159.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)


因篇幅有限,仅展示部分资料

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以点击这里获取]()**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

合查询
def doc_get__agg(self, index_name, con):
    res = self._client.search(index=index_name, body=con)
    return res['aggregations']
**参数**


index: 索引的名字


body:请求体,即聚合的字段


**测试代码**
def test_doc_get_aggs(self):
    con = {
        "aggs": {
            "role_groups": {