【ElasticSearch笔记】使用Python生成ES DSL语句

目录

一、创建连接(可选)

二、基本查询(Q方法)

2.1 query

空查询

基于字典生成Search对象

Query查询语句

组合查询

should

must 

must_not 

2.2 filter

空查询

terms

2.3 分页

2.4 排序

2.5 限制返回字段

指定显示字段 

 指定包含和不包含字段

仅返回元数据 

2.6 更新已存在的语句

2.7 复杂类型字段查询

2.8 额外的属性和参数

extra(添加额外属性)

三、深度分页查询

3.1 scroll

3.2 search_after

四、聚合查询(A方法)

4.1 桶(bucket)

聚合嵌套(嵌套时支持bucket、metric、pipeline)

五、附录

 5.1 根据指定规则生成DSL脚本


在使用Python客户端操作ES时,需要编写ES DSL语句对ES进行查询操作。但体验下来,发现以下缺点:

字段冗长,容易出现语法错误,如不正确的嵌套,难以修改(如添加另一个过滤器)

基于此目的,发现了解到了elasticsearch-dsl库。

将传统的JSON DSL语句,映射为Python类、对象操作,使其两者相互转换互通,减少手动编写JSON DSL语句的繁琐,提高可维护性。

一、创建连接(可选)

dsl库支持查询,并序列化结果。

若仅为了生成DSL语句,则无需创建连接。

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search

es = Elasticsearch(hosts="127.0.0.1:9200")
s = Search(using=es, index="teset_index")

二、基本查询(Q方法)

2.1 query

空查询

# 空查询
s = Search().query(Q())
{
  "query": {
    "match_all": {}
  }
}

基于字典生成Search对象

# 基于字典生成Search对象
s = Search.from_dict({"track_total_hits": True}).query(Q())
{
    "query": {
        "match_all": {}
    },
    "track_total_hits": true
}

Query查询语句

# query查询
s = Search.from_dict({"track_total_hits": True}).query(Q())
s = s.query("multi_match", query='python django', fields=['title', 'body'])
{
    "query": {
        "multi_match": {
            "query": "python django",
            "fields": [
                "title",
                "body"
            ]
        }
    },
    "track_total_hits": true
}

组合查询

should
# should
s = Search().query(Q("match", title='python') | Q("match", title='django'))
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "title": "python"
                    }
                },
                {
                    "match": {
                        "title": "django"
                    }
                }
            ]
        }
    }
}
must 
# must
s = Search().query(Q("match", title='python') & Q("match", title='django'))
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "title": "python"
                    }
                },
                {
                    "match": {
                        "title": "django"
                    }
                }
            ]
        }
    }
}
must_not 
# must_not
s = Search().query(~Q("match", title='python'))
{
    "query": {
        "bool": {
            "must_not": [
                {
                    "match": {
                        "title": "python"
                    }
                }
            ]
        }
    }
}

2.2 filter

空查询

# 空查询
s = Search().filter(Q())
{
    "query": {
        "bool": {
            "filter": [
                {
                    "match_all": {}
                }
            ]
        }
    }
}

terms

# trems
s = Search().filter('terms', tags=['search', 'python'])
{
    "query": {
        "bool": {
            "filter": [
                {
                    "terms": {
                        "tags": [
                            "search",
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值