網站首頁 編程語言 正文
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-11-01 Android?Activity?Results?API代替onActivityResult處理頁面
- 2022-03-03 解決Warning: [antdv: LocaleProvider] `LocaleProvider
- 2022-01-12 cesium polygon添加邊界線不起作用
- 2023-02-09 Rust?所有權機制原理深入剖析_Rust語言
- 2022-07-24 Golang?CSP并發機制及使用模型_Golang
- 2023-03-26 數據結構TypeScript之棧和隊列詳解_其它
- 2022-09-24 Pytorch實現邏輯回歸分類_python
- 2022-11-23 Android?IdleHandler基本使用及應用案例詳解_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同步修改后的遠程分支