ElaticSearch
1.索引基本操作
1.1 创建一个索引
#创建一个索引
PUT /person
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
}
}
1.2 查看索引信息
#查看索引
GET /person
1.3 删除索引
#删除索引
DELETE /person
1.4 ES中Field可以指定的类型
#String:
text:一般用于全文检索。将当前的field进行分词
# keyword: 当前的Field不可被分词
#
#
#
#
1.5 创建索引并指定数据结构
——以创建小说为例子
PUT /book
{
"settings": {
#备份数
"number_of_replicas": 1,
#分片数
"number_of_shards": 5
},
#指定数据结构
"mappings": {
#指定类型 Type
"novel": {
# 文件存储的Field属性名
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word",
# 指定当前的Field可以作为查询的条件
"index": true
},
"authoor": {
"type": "keyword"
},
"onsale": {
"type": "date",
"format": "yyyy-MM-dd"
}
}
}
}
}
1.6 文档的操作
- 文档在ES服务中的唯一标志,_index, _type, _id 三个内容为组合,来锁定一个文档,操作或修改
1.6.1 新建文档
- 自动生成id
PUT /book/novel
{
"name": "西游记",
"authoor": "刘明",
"onsale": "2020-12-11"
}
- 手动指定ID(更推荐)
PUT /book/novel/1
{
"name": "三国演义",
"authoor": "小明",
"onsale": "2020-12-11"
}
1.6.2 修改文档
-
覆盖式修改
POST /book/novel/1 { "name": "三国演义", "authoor": "小明", "onsale": "2020-12-11" }
-
doc修改方式(更推荐)
POST /book/novel/1/_update { "doc": { "name": "极品家丁" } } #先锁定文档,_update 修改需要的字段即可
1.6.3 删除文档
-
删库跑路
DELETE /book/novel/1
2. Python操作ElaticSearch
2.1 Python操作ES
- 使用elasticsearch下的Elasticsearch模块,封装基础的操作方法
from elasticsearch import Elasticsearch
class ES:
def __init__(self, ip, port):
self.es = Elasticsearch([ip], port=port)
def get(self, index, doc_type, id):
'''
通过文档id查询index.doc_type的文档
:param index:
:param doc_type:
:param id:
:return:
'''
res = self.es.get(index=index, doc_type=doc_type, id=id)
return res
def search(self, index, doc_type, body):
'''
通过文档DSL查询语句查询index.doc_type的文档
:param index:
:param doc_type:
:param id:
:return:
'''
res = self.es.search(index=index, doc_type=doc_type, body=body)
return res['hits']['hits']
def index(self, index, doc_type, id, body):
'''
指定id插入数据到index.doc_type下
:param index:
:param doc_type:
:param id:
:param body:插入字段信息
:return:
'''
res = self.es.index(index=index, doc_type=doc_type, id=id, body=body)
return res
def delete(self, index, doc_type, id):
'''
通过id删除文档
:param index:
:param doc_type:
:param id:
:return:
'''
res = self.es.delete(index=index, doc_type=doc_type, id=id)
return res
def delete_by_query(self, index, doc_type, body):
'''
根据查询内容删除文档
:param index:
:param doc_type:
:param body: DSL 语句
:return:
'''
res = self.es.delete_by_query(index=index, doc_type=doc_type, body=body)
return res
def update(self, index, doc_type, id, body):
'''
指定id更新文档字段信息
:param index:
:param doc_type:
:param id:
:param body: 更新字段信息
:return:
'''
res = self.es.update(index=index, doc_type=doc_type, id=id, body=body)
return res
def update_by_query(self, index, doc_type, body):
'''
根据查询内容更新文档
:param index:
:param doc_type:
:param body: DSL 语句
:return:
'''
res = self.es.update_by_query(index=index, doc_type=doc_type, body=body)
return res
3. ES的各种查询
3.1 term&terms查询
3.1.1 term查询
- term的查询是代表完全匹配,搜索之前不会对你的关键字进行分词
#term匹配查询, 查询id=20的数据
POST http://ip:9202/chance_assign_record_detail/chance_assign_record_detail/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"id": "20"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}
- terms是针对一个字段包含多个值得运用
查询id为20,30的数据
POST http://ip:9202/chance_assign_record_detail/chance_assign_record_detail/_search
{
"query": {
"bool": {
"must": [
{
"terms": {
"id": [20, 30]
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}
3.2 match查询
match查询属于高层查询,它会根据你查询字段类型不一样,采用不同的查询方式
match查询,实际底层就是多个term查询,将多个term查询的结果进行了封装
查询的如果是日期或者是数值的话,它会根据你的字符串查询内容转换为日期或者是数值对等
如果查询的内容是一个不可被分的内容(keyword),match查询不会对你的查询的关键字进行分词
如果查询的内容是一个可被分的内容(text),match则会根据指定的查询内容按照一定的分词规则去分词进行查询
3.2.1 match_all查询
- 查询全部内容,不指定任何查询条件
POST http://ip:9202/chance_assign_record_detail/chance_assign_record_detail/_search
{
"query": {
"must": [
{
"match_all": {}
}
]
},
"from": 0,
"size": 10,
"sort": []
}
3.3 嵌套查询
- 查询嵌套对象labels下的labelId为649的数据
{
"query": {
"filter": [
{
"nested": {
"path": "labels",
"query": {
"term": {
"labels.labelId": 649
}
}
}
}
]
}
}
- 查询嵌套对象labels下的labelId不为空的数据
{
"query": {
"filter": [
{
"nested": {
"path": "labels",
"query": {
"exists": {
"field": "labels.labelId"
}
}
}
}
]
}
}
3.4 符合查询
- 查询activeType等于1且customerId大于小于等于100000000的所有满足条件的数据
{
"query": {
"bool": {
"must": [
{
"term": {
"activeType": "1"
}
},
{
"range": {
"customerId": {
"gt": "1",
"lte": "100000000"
}
}
}
]
}
}
}
3. ES的更新数据
平时最常用的是update_by_query方法
3.1更新文档某个字段
#将id=105900的文档中的mobileBelong的值改为 上海
{
"script": {
"inline": "ctx._source['mobileBelong']='上海'"
},
"query": {
"bool": {
"must": [
{
"term": {
"id": "105900"
}
}
],
"must_not": [],
"should": []
}
}
}
3.2更新嵌套对象
{
"script": {
"inline": "Map item = new HashMap();item.put('taskId',1);item.put('taskName', '我的任务');item.put('visitTime',1598630400000L);ctx._source['landSeaVisit']=item"
},
"query": {
"bool": {
"must": [
{
"term": {
"id": "15108"
}
}
],
"must_not": [],
"should": []
}
}
}
3.3嵌套对象新插入数据
{
"script": {
"inline": "Map item = new HashMap();item.put('taskId',1);item.put('taskName', '我的任务');item.put('visitTime',1598630400000L);ctx._source['landSeaVisit'].add(item)"
},
"query": {
"bool": {
"must": [
{
"term": {
"id": "15108"
}
}
],
"must_not": [],
"should": []
}
}
}