es查询响应结果中获取某些字段的值
2023-09-11 14:15:14 时间
有时候使用es查询出的结果包含多个字段,如果数据中仅仅包含几个字段时,我们是很容易挑出自己需要的字段值,但是如果数据中包含几十或者几百甚至更多时,尤其是数据中嵌套好多层时,不容易直接挑取出需要的值,这时候可以借助程序直接查找出来。或者针对性的直接查询时就限定条件查询某些字段的值。
直接从es中查询出的示例数据:
{
"took": 918,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.0,
"hits": [{
"_index": "test",
"_type": "user",
"_id": "QHi1UoIBpyNh4YQ4T1Sq",
"_score": 1.0,
"_source": {
"id": 1001,
"name": "张三",
"age": 20,
"sex": "男",
"grade": {
"Chinese": 99,
"Math": 98,
"English": 96
}
}
},
{
"_index": "test",
"_type": "user",
"_id": "1002",
"_score": 1.0,
"_source": {
"id": 1002,
"name": "李四",
"age": 23,
"sex": "女",
"grade": {
"Chinese": 98,
"Math": 99,
"English": 97
}
}
},
{
"_index": "test",
"_type": "user",
"_id": "1003",
"_score": 1.0,
"_source": {
"id": 1003,
"name": "王五",
"age": 27,
"sex": "男",
"grade": {
"Chinese": 93,
"Math": 90,
"English": 99
}
}
},
{
"_index": "test",
"_type": "user",
"_id": "1004",
"_score": 1.0,
"_source": {
"id": 1004,
"name": "赵六",
"age": 29,
"sex": "女",
"grade": {
"Chinese": 100,
"Math": 95,
"English": 94
}
}
}
]
}
}
使用python打印出需要的字段值:
import json
import jmespath
with open('text.txt', 'r', encoding='utf-8') as f:
data = f.read()
json_data = json.loads(data)
# print(json_data)
sources = json_data.get('hits').get('hits')
# print(sources)
for source in sources:
# print(source)
data = source.get('_source')
print(data)
valid_fields = '{name: name, Chinese_grade: grade.Chinese}'
ret = jmespath.search(valid_fields, data)
print(ret)
运行结果:
注意:使用json.load()时,会把文本中的fase、true直接变为False、True
参考博文:
相关文章
- ES 集群上,业务单点如何优化升级?
- Android OpenGL ES(十一)绘制一个20面体 .
- Android OpenGL ES(九)绘制线段Line Segment .
- Adobe Livecycle ES PDF generation log for CCTV
- 使用Adobe Lifecycle ES将若干个word合并成一个PDF
- Atitit lucence es solr的各种query 与sql运算符的对比 目录 1.1. 等于运算 TermQuery1 1.2. 范围运算1 1.3. 大小运算1 1.4. Wi
- 音视频开发(七):OpenGL ES 基本概念
- Lucene默认的打分算法——ES默认
- ES禁用_source不会影响聚合
- ES查看segment大小
- 【ES】Elasticsearch核心基础概念:文档与索引
- ElasticSearch_09_ES 7.x版本的两个变动