python 操作 ES 二、mappings

环境 python:3.8 es:7.8.0

环境安装

pip install elasticsearch==7.8.0

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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))

 

posted @   万笑佛  阅读(398)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示