Dockerfile 构建(安装ik)

  1. FROM elasticsearch:7.13.2
  2. ARG ANALYSIS_IK_URL=https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.13.2/elasticsearch-analysis-ik-7.13.2.zip
  3. ENV ANALYSIS_IK_URL=${ANALYSIS_IK_URL}
  4. RUN yes|/usr/share/elasticsearch/bin/elasticsearch-plugin install ${ANALYSIS_IK_URL}
  5. RUN echo 'xpack.security.enabled: true' >> /usr/share/elasticsearch/config/elasticsearch.yml
  6. RUN echo 'xpack.security.transport.ssl.enabled: true' >> /usr/share/elasticsearch/config/elasticsearch.yml

Elasticsearch 设置密码

  1. # 自由设置密码
  2. /usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive
  3. # 自动生成密码
  4. /usr/share/elasticsearch/bin/elasticsearch-setup-passwords auto

Elasticsearch DSL 之 Search

简单示例

  1. from elasticsearch import Elasticsearch
  2. from elasticsearch_dsl import Search, Q, A
  3. if __name__ == '__main__':
  4. es = Elasticsearch()
  5. es_index = "document"
  6. s = Search().index(es_index).using(es)
  7. s = s.query("has_child", type='author', query=Q("match_all"))
  8. # counts = s.count()
  9. # d = s.to_dict()
  10. es_response = s.execute()

获取查询总数

  1. # 获取查询总数
  2. counts = s.count()

匹配

  1. # 匹配
  2. s = s.query("match", title='Interface')

多字段匹配

  1. # 多字段匹配
  2. s = s.query("multi_match", fields=['title', 'source'], query='Interface')

精确过滤

  1. # 精确过滤
  2. s = s.filter("term", is_show=1)

排序

  1. # 排序
  2. s = s.sort('-view_times')

高亮

  1. # 高亮
  2. s = s.highlight('title')

指定返回字段

  1. # 指定返回字段
  2. s = s.source(includes=['title', 'source'], excludes=["child_.*"])

分页

  1. # 分页
  2. s = s[0:10]

求和

  1. # 求和
  2. s.aggs.metric('sum', 'sum', field='view_times')

求平均数

  1. # 求平均数
  2. s.aggs.metric('avg', 'avg', field='view_times')

按范围分组统计

  1. # 按范围分组统计
  2. s.aggs.bucket('range', 'range', field='view_times', ranges=[
  3. {"to": 100},
  4. {"from": 100, "to": 5000},
  5. {"from": 5000}
  6. ])

分组统计

  1. # 分组统计
  2. s.aggs.bucket(
  3. "group", A('terms', field='keywords', size=10000, order={'_count': 'desc'})
  4. ).metric(
  5. # 分组后再求和
  6. 'sum', 'sum', field='view_times'
  7. )
  8. # 分组后再求平均数
  9. s.aggs['group'].metric('avg', 'avg', field='view_times')

Nested Query(嵌套查询)

  1. s = s.query(
  2. "nested",
  3. path="fee",
  4. query={
  5. "bool": {
  6. "filter": [
  7. {
  8. "term": {
  9. "fee.title": "测试"
  10. }
  11. }
  12. ]
  13. }
  14. }
  15. )

Nested Aggregation(嵌套聚合)

  1. s.aggs.bucket(
  2. name="apply_fee_bucket",
  3. agg_type="nested",
  4. path="apply_fee",
  5. aggs={
  6. "min_amount": {"min": {"field": "apply_fee.amount"}},
  7. "max_amount": {"max": {"field": "apply_fee.amount"}},
  8. "group_by": {"terms": {"field": "apply_fee.true_title.raw"}}
  9. }
  10. )

Elasticsearch DSL 之 UpdateByQuery

直接赋值

  1. from elasticsearch import Elasticsearch
  2. from elasticsearch_dsl import UpdateByQuery
  3. es_response = UpdateByQuery().using(es).index( "document").filter(
  4. "term", _routing=es_doc_id
  5. ).filter(
  6. "term", child_index=child_index
  7. ).script( # 修改值
  8. inline="ctx._source.child_name = params.child_name",
  9. params={
  10. "child_name": "张三"
  11. },
  12. lang="painless"
  13. ).execute()

在原数据基础上更新

  1. from elasticsearch import Elasticsearch
  2. from elasticsearch_dsl import UpdateByQuery
  3. es_response = UpdateByQuery().using(es).index( "document").filter(
  4. "term", _routing=es_doc_id
  5. ).filter(
  6. "term", child_index=child_index
  7. ).script( # += 操作
  8. inline="ctx._source.view_times += params.num",
  9. params={"num": 3},
  10. upsert={"view_times": 0},
  11. lang="painless"
  12. ).script( # ++ 操作
  13. inline="ctx._source.download_times++",
  14. upsert={"download_times": 0},
  15. lang="painless"
  16. ).execute()