Elasticsearches之python使用及Django与Flask集成示例
目录
Elasticsearch之Python使用Elasticsearch之Django/Flask集成elasticsearch-dsldjango集成Elasticsearch之Python使用
from elasticsearch import Elasticsearch
obj = Elasticsearch()
# 创建索引(Index)
result = obj.indices.create(index="user", body={"userid":"1","username":"lqz"},ignore=400)
# print(result)
# 删除索引
# result = obj.indices.delete(index="user", ignore=[400, 404])
# 插入数据
# data = {"userid": "1", "username": "lqz","password":"123"}
# result = obj.create(index="news", doc_type="politics", id=1, body=data)
# print(result)
# 更新数据
"""
不用doc包裹会报错
ActionRequestValidationException[Validation Failed: 1: script or doc is missing
"""
# data ={"doc":{"userid": "1", "username": "lqz","password":"123ee","test":"test"}}
# result = obj.update(index="news", doc_type="politics", body=data, id=1)
# print(result)
# 删除数据
# result = obj.delete(index="news", doc_type="politics", id=1)
# 查询
# 查找所有文档
query = {"query": {"match_all": {}}}
# 查找名字叫做jack的所有文档
# query = {"query": {"term": {"username": "lqz"}}}
# 查找年龄大于11的所有文档
# query = {"query": {"range": {"age": {"gt": 11}}}}
allDoc = obj.search(index="news", doc_type="politics", body=query)
print(allDoc["hits"]["hits"][0]["_source"])Elasticsearch之Django/Flask集成
elasticsearch-dsl
#安装: pip3 install elasticsearch-dsl
#示例
from datetime import datetime
from elasticsearch_dsl import Document, Date, Nested, Boolean, \
analyzer, InnerDoc, Completion, Keyword, Text
html_strip = analyzer("html_strip",
tokenizer="standard",
filter=["standard", "lowercase", "stop", "snowball"],
char_filter=["html_strip"]
)
class Comment(InnerDoc):
author = Text(fields={"raw": Keyword()})
content = Text(analyzer="snowball")
created_at = Date()
def age(self):
return datetime.now() - self.created_at
class Post(Document):
title = Text()
title_suggest = Completion()
created_at = Date()
published = Boolean()
category = Text(
analyzer=html_strip,
fields={"raw": Keyword()}
)
comments = Nested(Comment)
class Index:
name = "blog"
def add_comment(self, author, content):
self.comments.append(
Comment(author=author, content=content, created_at=datetime.now()))
def save(self, ** kwargs):
self.created_at = datetime.now()
return super().save(** kwargs)
django集成
from datetime import datetime
from elasticsearch_dsl import Document, Date, Nested, Boolean,analyzer, InnerDoc, Completion, Keyword, Text,Integer
from elasticsearch_dsl.connections import connections
connections.create_connection(hosts=["localhost"])
class Article(Document):
title = Text(analyzer="ik_max_word", search_analyzer="ik_max_word", fields={"title": Keyword()})
author = Text()
class Index:
name = "myindex"
def save(self, ** kwargs):
return super(Article, self).save(** kwargs)
if __name__ == "__main__":
# Article.init() # 创建映射
# 保存数据
# article = Article()
# article.title = "测试测试"
# article.save() # 数据就保存了
#查询数据
# s=Article.search()
# s = s.filter("match", title="测试")
# results = s.execute()
# print(results)
#删除数据
# s = Article.search()
# s = s.filter("match", title="测试").delete()
#修改数据
# s = Article().search()
# s = s.filter("match", title="测试")
# results = s.execute()
# print(results[0])
# results[0].title="xxx"
# results[0].save()以上就是Elasticsearches之python使用及Django与Flask集成示例的详细内容,更多关于python Elasticsearches之Django与Flask集成的资料请关注脚本之家其它相关文章!
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?

