網站首頁 編程語言 正文
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()
原文鏈接:https://www.cnblogs.com/guyouyin123/p/13308721.html
相關推薦
- 2022-10-19 Python?變量教程私有變量詳解_python
- 2022-04-15 c語言?深入理解函數的遞歸_C 語言
- 2024-02-17 pytorch花式索引提取topk的張量
- 2022-02-13 error C4996:‘scanf‘:This function or variable may
- 2022-04-11 Python寫一個簡單的在線編輯器_python
- 2023-02-09 Python去除html標簽的幾種方法總結_python
- 2022-03-26 .Net?Core微服務rpc框架GRPC通信基礎_基礎應用
- 2022-12-05 Android?RecyclerView緩存復用原理解析_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支