ElasticSearch与python交互

测试环境 ElasticSearch7.7.0 python 3.8

1. 基本查询数据

from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "match_all": {

        }
    }
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#查询索引为movies  type为movie的所有数据

term 和terms的用法

from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "term": {
            "title": "Kill"
        }
    }
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#查询查询索引为movies  type为movie的 且title等于kill的所有数据
from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "terms": {
            "title":
                [
                    "Kill", "Lawrence of Arabia"
                ]
        }
    }
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#查询查询索引为movies  type为movie的 且title等于kill或title等于Lawrence of Arabia的所有数据

match与multi_match

from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "match": {
            "title": "kill"

        }
    }
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#查询查询索引为movies  type为movie的 且title包含kill的所有数据
from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "multi_match": {
            "query": "kill",
            "fields": ["title", "genres"]

        }
    }
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#查询查询索引为movies  type为movie的 条件是在title和genres中查询包含kill的所有数据

ids

from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "ids": {
            "type": "movie",
            "values": [
                "1", "2"
            ]

        }
    }
}
res = es.search(index='movies', body=query)
print(res)
#查询查询索引为movies  type为movie的 id为1或者2的说有的值

复合查询

bool有3类查询关系,must(都满足),should(其中一个满足),must_not(都不满足)

from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "bool": {
            "must": [{
                "match": {
                    "city": "Brogan"
                }
            },
                {
                    "match": {
                        "state": "IL"
                    }
                }
            ]

        }
    }
}
res = es.search(index='bank', doc_type="typeName", body=query)
print(res)
#查询索引bank  type是typeName city包含Brogan且state包含IL的所有数据,
#如果把match换成term就是精准查找

切片查询 from size

from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "match_all": {},
    },
    "from": 2,
    "size": 4
}
res = es.search(index='bank', doc_type="typeName", body=query)
print(res)
#from 从什么地方开始,size查询几条记录 类似于sql中的skip 和limit的用法

范围查询 range

from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "range": {
            "age": {
                "gte": 20,
                "lte": 25
            }
        },
    }

}
res = es.search(index='bank', doc_type="typeName", body=query)
print(res)
# 查询年龄在20-25之间的所有数据

根据前缀查询

from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "prefix": {
            "title": "T"
        }
    }
}
res = es.search(index='movies', doc_type="movie", body=query)
print(res)
查询title 前缀是T开头的说有数据

排序

from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "match_all": {}
    },
    "sort": {
        "age": {  # 根据age字段升序排序
            "order": "asc"  # asc升序,desc降序
        }
    }

}
res = es.search(index='bank', doc_type="typeName", body=query)
print(res)
按照年龄升序排列

响应过滤 和mongo的查询之后显示部分字段类似

# 只获取自己想要的字段
from elasticsearch import Elasticsearch

es = Elasticsearch()
query=["hits.hits._id"]
es.search(index="bank",doc_type="typeName",filter_path=query)
 
# 获取所有数据
query=["hits.hits._*"]
es.search(index="bank",doc_type="typeName",filter_path=query)

查询匹配数量

es.count(index="bank",doc_type="typeName")
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章