Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
zzsn_spider
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
1
合并请求
1
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
丁双波
zzsn_spider
Commits
50571df6
提交
50571df6
authored
10月 27, 2023
作者:
刘伟刚
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ES操作工具
上级
c2749092
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
256 行增加
和
0 行删除
+256
-0
Esmethod.py
estool/Esmethod.py
+230
-0
esconn.py
estool/esconn.py
+26
-0
没有找到文件。
estool/Esmethod.py
0 → 100644
浏览文件 @
50571df6
import
json
from
elasticsearch
import
Elasticsearch
class
EsMethod
(
object
):
def
__init__
(
self
):
# 创建Elasticsearch对象,并提供账号信息
self
.
es
=
Elasticsearch
([
'http://114.116.19.92:9700'
],
http_auth
=
(
'elastic'
,
'zzsn9988'
),
timeout
=
300
)
self
.
index_name
=
'researchreportdata'
'''
模糊
# 查询方法:模糊查询(会被分词)。
# 比如 我爱你中国,会查到只包含:“我爱你”, “中国”的内容
'''
def
match
(
self
,
index_name
,
pnum
):
body
=
{
'query'
:{
'match'
:{
'title'
:
'.pdf'
,
'origin'
:
'雪球网'
,
'type'
:
'1'
,
}
},
'from'
:
pnum
,
'size'
:
20
,
}
filter_path
=
[
'hits.hits._source.title'
,
# 字段1
'hits.hits._source.id'
]
# 字段2
result
=
self
.
es
.
search
(
index
=
index_name
,
doc_type
=
'_doc'
,
filter_path
=
filter_path
,
body
=
body
)
print
(
result
)
'''
包含查询
# 查询方法:模糊查询(不会被分词)。会查到包含:“我爱你中国”的内容
'''
def
match_phrase
(
self
,
index_name
):
body
=
{
'query'
:{
'match_phrase'
:{
'm_ext1'
:
'XXXXXX'
#keyword
}
}
}
filter_path
=
[
'hits.hits._source.title'
,
# 字段1
'hits.hits._source.id'
,
'hits.hits._source.sourceAddress'
,
'hits.hits._source.publishDate'
]
# 字段2
result
=
self
.
es
.
search
(
index
=
index_name
,
doc_type
=
'_doc'
,
filter_path
=
filter_path
,
body
=
body
)
print
(
result
)
'''
精准查询
'''
def
term
(
self
,
index_name
):
body
=
{
'query'
:{
'term'
:{
'm_slhm'
:
'XXXXXX'
}
}
}
filter_path
=
[
'hits.hits._source.m_ext1'
,
# 字段1
'hits.hits._source.m_ext2'
]
# 字段2
result
=
self
.
es
.
search
(
index
=
index_name
,
doc_type
=
'_doc'
,
filter_path
=
filter_path
,
body
=
body
)
print
(
result
)
'''
多个条件精准查询
'''
def
terms
(
self
,
index_name
):
body
=
{
'query'
:{
'terms'
:{
'm_slhm'
:
[
'13XXXXXX '
,
'13XXXXXX'
]
}
}
}
filter_path
=
[
'hits.hits._source.m_ext1'
,
# 字段1
'hits.hits._source.m_slhm'
]
# 字段2
result
=
self
.
es
.
search
(
index
=
index_name
,
doc_type
=
'_doc'
,
filter_path
=
filter_path
,
body
=
body
)
print
(
result
)
'''
多条件 and 查询
'''
def
multi_must
(
self
,
index_name
):
body
=
{
'query'
:
{
'bool'
:
{
'must'
:[
{
'term'
:{
'm_slhm'
:
'13XXXXXXX'
}},
{
'terms'
:{
'm_slhm'
:
[
'13XXXXXX'
]}},
]
}
}
}
filter_path
=
[
'hits.hits._source.m_ext1'
,
# 字段1
'hits.hits._source.m_slhm'
]
# 字段2
result
=
self
.
es
.
search
(
index
=
index_name
,
doc_type
=
'_doc'
,
filter_path
=
filter_path
,
body
=
body
)
print
(
result
)
'''
更新
'''
def
update
(
self
,
index_name
):
result
=
self
.
es
.
update
(
index
=
index_name
,
id
=
'20220901-XXXXX'
,
body
=
{
'serialno'
:
'XXXXXX'
})
print
(
'更新结果:
%
s'
%
result
)
'''
新增
'''
def
add
(
self
,
index_name
):
result
=
self
.
es
.
index
(
index
=
index_name
,
id
=
'20220901-XXXXXX'
,
body
=
{
'serialno'
:
'XXXXXX'
})
print
(
'新增结果:
%
s'
%
result
)
'''
删除
'''
def
delete
(
self
,
index_name
):
result
=
self
.
es
.
delete
(
index
=
index_name
,
doc_type
=
"_doc"
,
id
=
'20220901-XXXXXX'
)
print
(
'删除结果
%
s'
%
result
)
'''
多条件 or 查询
'''
def
multi_should
(
self
,
index_name
,
pnum
):
body
=
{
'query'
:
{
'bool'
:
{
'should'
:[
{
'term'
:{
'origin'
:
'雪球网'
}},
{
'term'
:{
'type'
:
1
}},
],
'must'
:
[
{
'match'
:
{
'title'
:
'.pdf'
}}
]
}
},
'from'
:
pnum
,
'size'
:
6000
,
}
filter_path
=
[
'hits.hits._source.title'
,
# 字段1
'hits.hits._source.id'
,
'hits.total.value'
,
]
# 字段2
result
=
self
.
es
.
search
(
index
=
index_name
,
doc_type
=
'_doc'
,
filter_path
=
filter_path
,
body
=
body
)
print
(
result
)
return
result
'''
更新
'''
def
updateaunn
(
self
,
index_name
,
id
,
utitle
):
body
=
{
'doc'
:
{
'title'
:
utitle
}
}
result
=
self
.
es
.
update
(
index
=
index_name
,
id
=
id
,
body
=
body
)
print
(
'更新结果:
%
s'
%
result
)
def
getFileds
(
self
,
index_name
):
mapping
=
self
.
es
.
indices
.
get_mapping
(
index
=
index_name
)
fields
=
mapping
[
index_name
][
'mappings'
][
'properties'
]
.
keys
()
print
(
fields
)
if
__name__
==
'__main__'
:
esMethod
=
EsMethod
()
# esMethod.getFileds(index_name=esMethod.index_name)
num
=
1
for
pnum
in
range
(
0
,
num
):
p
=
pnum
*
20
print
(
f
'第{pnum}页数据'
)
result
=
esMethod
.
multi_should
(
index_name
=
esMethod
.
index_name
,
pnum
=
p
)
msglist
=
result
[
'hits'
][
'hits'
]
print
(
msglist
)
for
mms
in
msglist
:
id
=
mms
[
'_source'
][
'id'
]
title
=
mms
[
'_source'
][
'title'
]
utitle
=
title
.
replace
(
'.pdf'
,
''
)
print
(
f
'id:{id}---title:{title}--utitle:{utitle}'
)
esMethod
.
updateaunn
(
esMethod
.
index_name
,
str
(
id
),
utitle
)
print
(
'跟新成功!!'
)
estool/esconn.py
0 → 100644
浏览文件 @
50571df6
import
json
from
elasticsearch
import
Elasticsearch
# 创建Elasticsearch对象,并提供账号信息
es
=
Elasticsearch
(
[
'http://114.116.19.92:9700'
],
http_auth
=
(
'elastic'
,
'zzsn9988'
)
# 账号和密码
)
index_name
=
'basedata'
# 搜索文档
search_query
=
{
"query"
:
{
"match"
:
{
"sourceAddress"
:
"www"
}
}
}
# search_query=json.dumps(search_query)
res
=
es
.
search
(
index
=
index_name
,
body
=
search_query
)
for
hit
in
res
[
'hits'
][
'hits'
]:
print
(
hit
[
'_source'
])
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论