python 操作 ES 二、mappings
环境 python:3.8 es:7.8.0
环境安装
pip install elasticsearch==7.8.0
from elasticsearch import Elasticsearch #环境 python:3.8 es:7.8.0 #环境安装 #pip install elasticsearch==7.8.0 #1、创建ES对象,创建连接 es = Elasticsearch(['127.0.0.1:9200'],ignore=[400, 405, 502]) print('---------------1--------------------------') #result=es.indices.create(index="py4",ignore=400) mappings = { "mappings": { "properties": { "age": { "type": "long" }, "name": { "type": "keyword" }, "note": { "type": "text" } } } } #删除索引 # es.indices.delete(index='py4') # #创建索引 并带mappings # es.indices.create(index='py4', body=mappings) #查看索引的mappings print(es.indices.get_mapping(index='py4')) #创建一个没有mappings的索引 # es.indices.create(index="user",ignore=400) # print(es.indices.get_mapping(index='user')) print(es.index(index='py4', doc_type='_doc', id='1', body={"name":"可可", "age": 18,"note":"你好啊"})) # 正常 #1、long数据类型查询 body = { "query": { "match": { "age": 18 } } } print('-------1---------') print(es.search(index='py4', body=body)) #2、关键字查询 body1 = { "query": { "match": { "name": "可可" #查询 可可 有返回数据 } } } print('-------2-1---------') print(es.search(index='py4', body=body1)) body2 = { "query": { "match": { "name": "可" #查询 可 没有返回数据 } } } print('-------2-2---------') print(es.search(index='py4', body=body2)) #3、text查询 可以实现模糊查询说明已经分词了 body3 = { "query": { "match": { "note": "你好啊" #查询 你好啊 有返回数据 } } } print('-------3-1---------') print(es.search(index='py4', body=body3)) body4 = { "query": { "match": { "note": "你好" #查询 你好 有返回数据 } } } print('-------3-2---------') print(es.search(index='py4', body=body4))