python Elasearch 查询

python es 基本查询

pip install elasticsearch

index = 'my_test_index'   # 自定义index名称

1. 基本查询体结构

查询所有, 默认返回10条数据

from elasticsearch.client import Elasticsearch
 
es = Elasticsearch('127.0.0.1:90201')
 
 
body = {
            "query": {"match_all": {}}
      }
 
 
search_result = es.search(index=index, body=body)

2. 指定返回的条数(size)

from elasticsearch.client import Elasticsearch
 
es = Elasticsearch('127.0.0.1:90201')
 
 
body = {
            "query": {"match_all": {}}
      }
 
 
search_result = es.search(index=index, body=body, size=100)

3.指定匹配条件 (match)

from elasticsearch.client import Elasticsearch
 
es = Elasticsearch('127.0.0.1:90201')
 
body = {
            "query": {
                "bool": {
                    "must": [
                        {"match": {"id": 11}},                  # 匹配条件
                        {"match": {"is_deleted": 0}},
                        {"exists": {"field": "age"}}   # age字段的值不为空
                    ]
                },
            },
        }
 
search_result = es.search(index=self.agile_issue_log_index, body=body, size=10000) 

4. 查询范围控制 (range)

from elasticsearch.client import Elasticsearch
 
es = Elasticsearch('127.0.0.1:90201')
 
body = {
            "query": {
                "bool": {
                    "must": [
                        {"match": {"is_deleted": 0}},
                    ],
                    "filter": [
                        {"range": {"creation_date": {"gte": starttime, "lte": lasttime}}}   # 范围查询
                    ]
                },
            }
        }
 
search_result = es.search(index=self.agile_issue_log_index, body=body, size=10000)  

5.字段的值为指定某几个值(in)

from elasticsearch.client import Elasticsearch
 
es = Elasticsearch('127.0.0.1:90201')
 
body = {
           "query": {
                "bool": {
                    "must": [
                        {"match": {"is_deleted": 0}},
                        {"exists": {"field": "age"}},
                    ],
                    "filter": [
                                {"terms": {"age": [18, 19]}},   # age值为18或19                       
                    ]
                },
            },
        }
search_result = es.search(index=self.agile_issue_log_index, body=body, size=10000)

6.排序(sort)

from elasticsearch.client import Elasticsearch
 
es = Elasticsearch('127.0.0.1:90201')
 
body = {
           "query": {
                "bool": {
                    "must": [
                        {"match": {"project_id": 11}}
                     ]
                  },
            },
            "sort": [{"creation_date": {"order": "desc"}}]    # 排序,按照creation_date倒叙
        }
 
search_result = es.search(index=self.agile_issue_log_index, body=body, size=10000) 

7.查询返回结果中字段过滤(includes)

from elasticsearch.client import Elasticsearch
 
es = Elasticsearch('127.0.0.1:90201')
 
body = {
            "_source": {
                "includes": ["id", "name"],   # 设置只返回指定字段
                "excludes": []
            },
            "query": {"match_all": {}}
        }
search_result = es.search(index=index, body=body, size=10000)

8.求和(sum)

from elasticsearch.client import Elasticsearch
 
es = Elasticsearch('127.0.0.1:90201')
 
sum_filed = "sum_ages"   # 自定义求和后的字段名
body = {
            "query": {"match_all":{}},
            "aggs": {
                sum_filed: {"sum": {"field": "age"}},   # 对age值求和
            },
        }
search_result = es.search(index=self.agile_issue_index, body=body, size=10000)
sum_ages = search_result["aggregations"][sum_filed]["value"]

结合1-7所有的查询体结构如下:

from elasticsearch.client import Elasticsearch
 
es = Elasticsearch('127.0.0.1:90201')
 
body = {
            "_source": {
                "includes": ["id", "name"],
                "excludes": []
            },
            "query": {
                "bool": {
                    "must": [
                        {"match": {"is_deleted": 0}},
                        {"exists": {"field": "age"}},
                    ],
                    "filter": [
                                {"terms": {"age": [18, 19]}},                        
                                {"range": {"creation_date": {"gte": starttime, "lte": lasttime}}}
                    ]
                },
            },
            "sort": [{"creation_date": {"order": "desc"}}]
        }
search_result = es.search(index=self.agile_issue_log_index, body=body, size=10000)

猜你喜欢

转载自www.cnblogs.com/leontom/p/12680018.html
今日推荐