Elasticseach 基础使用

 

相关文档:

# pip install elasticsearch
# 文档:https://pypi.org/project/elasticsearch/5.4.0/
# 解释文档:https://zhuanlan.zhihu.com/p/95163799

  

python操作es基本方法:

# -*- coding: utf-8 -*-
"""
@Time : 
@Auth : wang
"""

from datetime import datetime
from elasticsearch import Elasticsearch

# 连接ES
es = Elasticsearch([{'host': '11.12.12.12', 'port': 9200}], timeout=3600)

# 创建索义
es.indices.create(index='my-index', ignore=400)

# 插入数据,指定id
res = es.index(index="my-index", doc_type="test-type", id=43, body={"any": "data_43", "timestamp": datetime.now()})
print(res)
# {'_index': 'my-index', '_type': 'test-type', '_id': '43', '_version': 1, 'result': 'created',
#  '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 0, '_primary_term': 1}

# 不指定id,自动生成
res = es.index(index="my-index", doc_type="test-type",
               body={"first_name": "xiao", "last_name": "xiao", 'age': 25, 'about': 'I love to go rock climbing',
                     'interests': ['game', 'play']})
print(res)  # 生成的id'_id': 'g_34tnMBj125sz4lUz_q'

# 简单的根据id查询数据
ret = es.get(index="my-index", doc_type="test-type", id=43)
ret1 = es.get(index="my-index", doc_type="test-type", id='g_34tnMBj125sz4lUz_q')
print(ret, ret1)

# 删除数据,根据id
ret = es.delete(index="my-index", doc_type="test-type", id=43)
print(ret)

# delete_by_query:删除满足条件的所有数据
query = {
    "query": {
        "match": {
            "first_name": "xiao"
        }
    }
}
result = es.delete_by_query(index="my-index", doc_type="test-type", body=query)
print(result)

# 更新
doc_body = {
    'script': "ctx._source.remove('age')"
}

# # 增加字段
doc_body = {
    'script': "ctx._source.address = '合肥'"
}
#
# 修改部分字段
doc_body = {
    "doc": {"last_name": "xiao"}
}
es.update(index="my-index", doc_type="test-type", id=43, body=doc_body)  # 根据ID更新
ret = es.get(index="my-index", doc_type="test-type", id=43)
print(ret)

# update_by_query:更新满足条件的所有数据,写法同上删除和查询
query = {
    "query": {
        "match": {
            "last_name": "xiao"
        }
    },
    "script": {
        "source": "ctx._source.last_name = params.name;ctx._source.any = params.age",  # 根据参数params字段来
        "lang": "painless",
        "params": {
            "name": "wang",
            "age": 100,
        },
    }

}
result = es.update_by_query(index="my-index", doc_type="test-type", body=query)
print(result)
ret = es.get(index="my-index", doc_type="test-type", id=43)
print(ret)

# --------------------------------------------------------------------------------
# 查询所有
query = {
  "query": {
    "match_all": {}
  }
}
result = es.search(index="my-index", body=query)
print(result)

# 使用DSL语句查询
query = {
    "query": {
        "term": {  # term 过滤--term主要用于精确匹配哪些
            'any': 100  # 可以多添件匹配[100,39]
        }
    }
}
result = es.search(index="my-index", body=query)
print(result)


# range 过滤--按照指定范围查找一批数据 gt : 大于 gte : 大于等于 lt : 小于 lte : 小于等于
query = {
    "query": {
        "range": {
            'any': {
                "lt": 34
            }
        }
    }
}
result = es.search(index="my-index", body=query)
print(result)


# exists 和 missing 过滤--查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的IS_NULL条件
query = {
    "query": {
        "exists": {
            "field": "last_name"
        }
    }
}
result = es.search(index="my-index", body=query)
print(result)

# bool 过滤--合并多个过滤条件查询结果的布尔逻辑 must :: 多个查询条件的完全匹配,相当于 and。must_not :: 多个查询条件的相反匹配,相当于 not。should :: 至少有一个查询条件匹配, 相当于 or。
query = {
    "query": {
        "bool": {
            "must": {
                "term": {"last_name": 'wang'},
                "term": {"any": 100}
            },
        }
    }
}
result = es.search(index="my-index", body=query)
print(result)

# 可以嵌套查询条件
# query = {
#     "query": {
#          "bool": {
#              "must": {
#                  "term": { "age": 32 }
#                 },
#              "must_not":{
#                  "exists":   {
#                     "field":    "name"
#                 }
#              }
#          }
#     }
# }

  

 

posted @ 2020-08-04 09:51  独角兕大王  阅读(174)  评论(0编辑  收藏  举报