当前位置: 代码迷 >> 综合 >> Elasticsearch 7 : 查询结果只展示部分字段
  详细解决方案

Elasticsearch 7 : 查询结果只展示部分字段

热度:99   发布时间:2023-09-19 18:22:16.0

创建索引:

PUT student
{"mappings" : {"properties" : {"name" : {"type" : "keyword"},"age" : {"type" : "integer"}}}
}

使用 _bulk 创建文档

POST _bulk
{ "index" : { "_index" : "student", "_id" : "1" } }
{ "name" : "张三", "age": 12}
{ "index" : { "_index" : "student", "_id" : "2" } }
{ "name" : "李四", "age": 10 }
{ "index" : { "_index" : "student", "_id" : "3" } }
{ "name" : "王五", "age": 11 }
{ "index" : { "_index" : "student", "_id" : "4" } }
{ "name" : "陈六", "age": 11 }

比如查询结果中只展示 name:

GET student/_search
{"query": {"match": {"age": "12"}},"_source": {"includes": ["name"]}
}

查询结果:

{"took" : 26,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "student","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"name" : "张三"}}]}
}

将 includes 换成 include 也可以,但会提示 include 已经废弃。所以不建议使用 include 。

  相关解决方案