elasticsearch practice

目录

 

1、创建索引

2、插入数据

3、查询所有索引数据

4、根据ID查询索引数据

5、根据请求体进行查询,请求体中可以使用es的各种语法

6、根据ID删除索引中的数据

7、根据请求体查询出的结果删除数据

8、删除索引

9、代码main部分


1、创建索引


# 创建索引
def create_index():
    _index_mappings={
        'mappings':{
            'dynamic':'true',   # 设置索引为动态的
            'properties':{
                'ExpertID':{
                    'type':'long'
                },
                'filedID':{
                    'type':'long'
                },
                '姓名':{
                    'type':'text'   #对应string
                },
                '单位':{
                    'type':'text',
                    'analyzer':'ik_max_word'    #使用ik分词(细粒度)
                },
                '学历':{
                    'type':'text',
                    'analyzer':'ik_max_word'
                }
            }
        }
    }
    # 判断索引是否在
    if es.indices.exists(index=my_index) is not True:
        # 创建索引,指定索引名、索引,映射
        res=es.indices.create(index=my_index,body=_index_mappings)
        print('无索引,创建它:\n',res)
    else:
        print('索引已经存在')

2、插入数据

# 从csv文件中读取数据,并存储到es中
def index_data_from_csv(csvfile):
    """

    :param csvfile:
    :return:
    """
    # 取CSV数据
    with open(csvfile,'r',encoding='utf-8',newline='') as file:
        list_data=csv.reader(file)

        # 循环成字典形式,插入数据
        index=0
        doc={}
        # 将每一行数据拼接成字典
        for item in list_data:
            if index >1: # 第一行是标题
                doc['ExpertID']=item[0]
                doc['fieldID'] = item[1]
                doc['姓名'] = item[2]
                doc['单位'] = item[3]
                doc['学历'] = item[4]

                # 建立索引的数据,指定索引名、类型名、内容
                res=es.index(index=my_index,doc_type=my_doc_type,body=doc)
                print(res)

3、查询所有索引数据

# 查询所有索引数据
def query_all_index_data():
    query_all_body={
        'query':{
            'match_all':{}
        },
        'from':0,
        'size':100
    }
    all_data=es.search(index=my_index,body=query_all_body)
    print(all_data)

4、根据ID查询索引数据

# 根据ID查询索引数据
def get_data_id(id_):
    all_data=es.get(my_index,doc_type=my_doc_type,id=id_)
    print(all_data)

5、根据请求体进行查询,请求体中可以使用es的各种语法

# 根据请求体进行查询,这里可以使用各种es的语法
def get_data_by_body():
    query_body={
        'query':{
            'match':{
                '地址':'大学'
            }
        }
    }
    res=es.search(index=my_index,body=query_body)
    print(res)

6、根据ID删除索引中的数据

# 删除索引中的一条
def delete_index_data_by_id(_id):
    '''
    删除索引中的一条
    :param _id:
    :return:
    '''
    res=es.delete(index='con_pro',doc_type='_doc',id=_id)
    print(res)

7、根据请求体查询出的结果删除数据

# 根据请求体查询出的结果删除数据
def delete_index_data_by_body():
    query_body={
        'query':{
            'match':{
                '地址':'大学'
            }
        }
    }
    res=es.delete_by_query(index=my_index,body=query_body)
    print(res)

8、删除索引

# 删除索引
def delete_index():
    # 判断索引是否存在
    if es.indices.exists(index=my_index) is True:
        res=es.indices.delete(index=my_index)

9、代码main部分


if __name__=='__main__':
    my_index='con_pro'
    my_doc_type='_doc'

    # 1.建立与ES的连接
    es=Elasticsearch(['ip'],http_auth=('username','pwd'),port=9200)

    # 2. 插入数据
    insert_index_data()

    # 3. 查询所有0-100条数据
    query_all_index_data()

    #4. 根据ID查询
    get_data_id('ox2aaG4Bbd3iU8yReS')

    #5. 根据请求体查询
    get_data_by_body()

    #6. 根据ID删除
    delete_index_data_by_id('ox2aaG4Bbd3iU8yReS')

    #7. 根据查询结果删除
    delete_index_data_by_body()

    #8. 删除索引
    delete_index()

猜你喜欢

转载自blog.csdn.net/weixin_38664232/article/details/110730092