Dockerfile 构建(安装ik)
FROM elasticsearch:7.13.2
ARG ANALYSIS_IK_URL=https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.13.2/elasticsearch-analysis-ik-7.13.2.zip
ENV ANALYSIS_IK_URL=${ANALYSIS_IK_URL}
RUN yes|/usr/share/elasticsearch/bin/elasticsearch-plugin install ${ANALYSIS_IK_URL}
RUN echo 'xpack.security.enabled: true' >> /usr/share/elasticsearch/config/elasticsearch.yml
RUN echo 'xpack.security.transport.ssl.enabled: true' >> /usr/share/elasticsearch/config/elasticsearch.yml
Elasticsearch 设置密码
# 自由设置密码
/usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive
# 自动生成密码
/usr/share/elasticsearch/bin/elasticsearch-setup-passwords auto
Elasticsearch DSL 之 Search
简单示例
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q, A
if __name__ == '__main__':
es = Elasticsearch()
es_index = "document"
s = Search().index(es_index).using(es)
s = s.query("has_child", type='author', query=Q("match_all"))
# counts = s.count()
# d = s.to_dict()
es_response = s.execute()
获取查询总数
# 获取查询总数
counts = s.count()
匹配
# 匹配
s = s.query("match", title='Interface')
多字段匹配
# 多字段匹配
s = s.query("multi_match", fields=['title', 'source'], query='Interface')
精确过滤
# 精确过滤
s = s.filter("term", is_show=1)
排序
# 排序
s = s.sort('-view_times')
高亮
# 高亮
s = s.highlight('title')
指定返回字段
# 指定返回字段
s = s.source(includes=['title', 'source'], excludes=["child_.*"])
分页
# 分页
s = s[0:10]
求和
# 求和
s.aggs.metric('sum', 'sum', field='view_times')
求平均数
# 求平均数
s.aggs.metric('avg', 'avg', field='view_times')
按范围分组统计
# 按范围分组统计
s.aggs.bucket('range', 'range', field='view_times', ranges=[
{"to": 100},
{"from": 100, "to": 5000},
{"from": 5000}
])
分组统计
# 分组统计
s.aggs.bucket(
"group", A('terms', field='keywords', size=10000, order={'_count': 'desc'})
).metric(
# 分组后再求和
'sum', 'sum', field='view_times'
)
# 分组后再求平均数
s.aggs['group'].metric('avg', 'avg', field='view_times')
Nested Query(嵌套查询)
s = s.query(
"nested",
path="fee",
query={
"bool": {
"filter": [
{
"term": {
"fee.title": "测试"
}
}
]
}
}
)
Nested Aggregation(嵌套聚合)
s.aggs.bucket(
name="apply_fee_bucket",
agg_type="nested",
path="apply_fee",
aggs={
"min_amount": {"min": {"field": "apply_fee.amount"}},
"max_amount": {"max": {"field": "apply_fee.amount"}},
"group_by": {"terms": {"field": "apply_fee.true_title.raw"}}
}
)
Elasticsearch DSL 之 UpdateByQuery
直接赋值
from elasticsearch import Elasticsearch
from elasticsearch_dsl import UpdateByQuery
es_response = UpdateByQuery().using(es).index( "document").filter(
"term", _routing=es_doc_id
).filter(
"term", child_index=child_index
).script( # 修改值
inline="ctx._source.child_name = params.child_name",
params={
"child_name": "张三"
},
lang="painless"
).execute()
在原数据基础上更新
from elasticsearch import Elasticsearch
from elasticsearch_dsl import UpdateByQuery
es_response = UpdateByQuery().using(es).index( "document").filter(
"term", _routing=es_doc_id
).filter(
"term", child_index=child_index
).script( # += 操作
inline="ctx._source.view_times += params.num",
params={"num": 3},
upsert={"view_times": 0},
lang="painless"
).script( # ++ 操作
inline="ctx._source.download_times++",
upsert={"download_times": 0},
lang="painless"
).execute()