ADD file via upload

zf_branch
ptgkifnrw 5 months ago
parent 70c7b14954
commit fac7ac60a8

@ -0,0 +1,328 @@
#zf导入时间模块用于生成时间戳
import time
#zf导入elasticsearch客户端模块
import elasticsearch.client
#zf导入Django配置模块
from django.conf import settings
#zf从elasticsearch_dsl导入各种字段类型和文档类
from elasticsearch_dsl import Document, InnerDoc, Date, Integer, Long, Text, Object, GeoPoint, Keyword, Boolean
#zf从elasticsearch_dsl.connections导入连接管理器
from elasticsearch_dsl.connections import connections
#zf从blog.models导入Article模型
from blog.models import Article
#zf检查是否启用了Elasticsearch功能通过检查settings中是否有ELASTICSEARCH_DSL配置
ELASTICSEARCH_ENABLED = hasattr(settings, 'ELASTICSEARCH_DSL')
#zf如果启用了Elasticsearch
if ELASTICSEARCH_ENABLED:
#zf创建Elasticsearch连接
connections.create_connection(
hosts=[settings.ELASTICSEARCH_DSL['default']['hosts']])
#zf导入Elasticsearch客户端
from elasticsearch import Elasticsearch
#zf创建Elasticsearch实例
es = Elasticsearch(settings.ELASTICSEARCH_DSL['default']['hosts'])
#zf导入IngestClient用于管理管道
from elasticsearch.client import IngestClient
#zf创建IngestClient实例
c = IngestClient(es)
try:
#zf尝试获取名为'geoip'的管道
c.get_pipeline('geoip')
except elasticsearch.exceptions.NotFoundError:
#zf如果管道不存在则创建一个geoip管道
#zf该管道用于根据IP地址添加地理位置信息
c.put_pipeline('geoip', body='''{
"description" : "Add geoip info",
"processors" : [
{
"geoip" : {
"field" : "ip"
}
}
]
}''')
#zf定义GeoIp内部文档类用于存储地理位置信息
class GeoIp(InnerDoc):
#zf大洲名称
continent_name = Keyword()
#zf国家ISO代码
country_iso_code = Keyword()
#zf国家名称
country_name = Keyword()
#zf地理位置坐标
location = GeoPoint()
#zf定义UserAgentBrowser内部文档类用于存储浏览器信息
class UserAgentBrowser(InnerDoc):
#zf浏览器家族
Family = Keyword()
#zf浏览器版本
Version = Keyword()
#zf定义UserAgentOS内部文档类继承自UserAgentBrowser用于存储操作系统信息
class UserAgentOS(UserAgentBrowser):
pass
#zf定义UserAgentDevice内部文档类用于存储设备信息
class UserAgentDevice(InnerDoc):
#zf设备家族
Family = Keyword()
#zf设备品牌
Brand = Keyword()
#zf设备型号
Model = Keyword()
#zf定义UserAgent内部文档类用于存储用户代理信息
class UserAgent(InnerDoc):
#zf浏览器信息
browser = Object(UserAgentBrowser, required=False)
#zf操作系统信息
os = Object(UserAgentOS, required=False)
#zf设备信息
device = Object(UserAgentDevice, required=False)
#zf完整的User-Agent字符串
string = Text()
#zf是否为机器人
is_bot = Boolean()
#zf定义ElapsedTimeDocument文档类用于存储页面性能数据
class ElapsedTimeDocument(Document):
#zfURL地址
url = Keyword()
#zf耗时毫秒
time_taken = Long()
#zf记录时间
log_datetime = Date()
#zfIP地址
ip = Keyword()
#zf地理位置信息
geoip = Object(GeoIp, required=False)
#zf用户代理信息
useragent = Object(UserAgent, required=False)
#zf定义索引配置
class Index:
#zf索引名称
name = 'performance'
settings = {
#zf分片数量
"number_of_shards": 1,
#zf副本数量
"number_of_replicas": 0
}
#zf定义文档元数据
class Meta:
#zf文档类型
doc_type = 'ElapsedTime'
#zf定义ElapsedTime文档管理器类
class ElaspedTimeDocumentManager:
#zf静态方法构建索引
@staticmethod
def build_index():
from elasticsearch import Elasticsearch
#zf创建Elasticsearch客户端
client = Elasticsearch(settings.ELASTICSEARCH_DSL['default']['hosts'])
#zf检查performance索引是否存在
res = client.indices.exists(index="performance")
if not res:
#zf如果不存在则初始化索引
ElapsedTimeDocument.init()
#zf静态方法删除索引
@staticmethod
def delete_index():
from elasticsearch import Elasticsearch
#zf创建Elasticsearch实例
es = Elasticsearch(settings.ELASTICSEARCH_DSL['default']['hosts'])
#zf删除performance索引忽略400和404错误
es.indices.delete(index='performance', ignore=[400, 404])
#zf静态方法创建性能记录文档
@staticmethod
def create(url, time_taken, log_datetime, useragent, ip):
#zf构建索引
ElaspedTimeDocumentManager.build_index()
#zf创建UserAgent对象并填充数据
ua = UserAgent()
ua.browser = UserAgentBrowser()
ua.browser.Family = useragent.browser.family
ua.browser.Version = useragent.browser.version_string
ua.os = UserAgentOS()
ua.os.Family = useragent.os.family
ua.os.Version = useragent.os.version_string
ua.device = UserAgentDevice()
ua.device.Family = useragent.device.family
ua.device.Brand = useragent.device.brand
ua.device.Model = useragent.device.model
ua.string = useragent.ua_string
ua.is_bot = useragent.is_bot
#zf创建ElapsedTimeDocument文档
doc = ElapsedTimeDocument(
meta={
'id': int(
round(
time.time() *
1000)) #zf使用当前时间戳作为ID
},
url=url,
time_taken=time_taken,
log_datetime=log_datetime,
useragent=ua,
ip=ip)
#zf保存文档并使用geoip管道处理
doc.save(pipeline="geoip")
#zf定义ArticleDocument文档类用于存储文章搜索数据
class ArticleDocument(Document):
#zf文章正文使用ik_max_word分词器进行索引ik_smart进行搜索
body = Text(analyzer='ik_max_word', search_analyzer='ik_smart')
#zf文章标题使用ik_max_word分词器进行索引ik_smart进行搜索
title = Text(analyzer='ik_max_word', search_analyzer='ik_smart')
#zf作者信息
author = Object(properties={
#zf昵称
'nickname': Text(analyzer='ik_max_word', search_analyzer='ik_smart'),
#zfID
'id': Integer()
})
#zf分类信息
category = Object(properties={
#zf分类名
'name': Text(analyzer='ik_max_word', search_analyzer='ik_smart'),
#zfID
'id': Integer()
})
#zf标签信息
tags = Object(properties={
#zf标签名
'name': Text(analyzer='ik_max_word', search_analyzer='ik_smart'),
#zfID
'id': Integer()
})
#zf发布时间
pub_time = Date()
#zf文章状态
status = Text()
#zf评论状态
comment_status = Text()
#zf文章类型
type = Text()
#zf浏览量
views = Integer()
#zf文章排序
article_order = Integer()
#zf定义索引配置
class Index:
#zf索引名称
name = 'blog'
settings = {
#zf分片数量
"number_of_shards": 1,
#zf副本数量
"number_of_replicas": 0
}
#zf定义文档元数据
class Meta:
#zf文档类型
doc_type = 'Article'
#zf定义ArticleDocument管理器类
class ArticleDocumentManager():
#zf初始化方法
def __init__(self):
self.create_index()
#zf创建索引方法
def create_index(self):
ArticleDocument.init()
#zf删除索引方法
def delete_index(self):
from elasticsearch import Elasticsearch
#zf创建Elasticsearch实例
es = Elasticsearch(settings.ELASTICSEARCH_DSL['default']['hosts'])
#zf删除blog索引忽略400和404错误
es.indices.delete(index='blog', ignore=[400, 404])
#zf将文章对象转换为文档对象的方法
def convert_to_doc(self, articles):
return [
ArticleDocument(
meta={
#zf使用文章ID作为文档ID
'id': article.id},
#zf文章正文
body=article.body,
#zf文章标题
title=article.title,
author={
#zf作者昵称
'nickname': article.author.username,
#zf作者ID
'id': article.author.id},
category={
#zf分类名
'name': article.category.name,
#zf分类ID
'id': article.category.id},
tags=[
{
#zf标签名
'name': t.name,
#zf标签ID
'id': t.id} for t in article.tags.all()],
#zf发布时间
pub_time=article.pub_time,
#zf文章状态
status=article.status,
#zf评论状态
comment_status=article.comment_status,
#zf文章类型
type=article.type,
#zf浏览量
views=article.views,
#zf排序
article_order=article.article_order) for article in articles]
#zf重建索引方法
def rebuild(self, articles=None):
#zf初始化索引
ArticleDocument.init()
#zf如果没有提供文章列表则获取所有文章
articles = articles if articles else Article.objects.all()
#zf转换文章为文档对象
docs = self.convert_to_doc(articles)
#zf保存所有文档
for doc in docs:
doc.save()
#zf更新文档方法
def update_docs(self, docs):
#zf保存所有文档
for doc in docs:
doc.save()
Loading…
Cancel
Save