Elasticsearch嵌套字段检索:如何仅保留指定键值对并排除特定字段?
解决方案:过滤嵌套字段中的特定条目
首先纠正你mapping里的两处问题:
paperName和paperType应该与details同级,而非嵌套在details的properties中- 文档里的
text和double字段,与mapping中定义的textVal/doubleVal不匹配,需要统一字段名,否则数据无法正确索引
正确的mapping如下:
{ "mappings": { "properties": { "paperName": { "type": "keyword" }, "paperType": { "type": "keyword" }, "details": { "type": "nested", "properties": { "fieldName": { "type": "keyword" }, "text": { "type": "text" }, "double": { "type": "double" } } } } } }
方法一:查询时通过inner_hits过滤嵌套条目
Elasticsearch无法直接在返回的_source中过滤嵌套数组的元素,但可以通过nested查询配合inner_hits获取仅包含text字段的嵌套条目,之后在应用层重构数据结构即可。
查询语句:
{ "query": { "nested": { "path": "details", "query": { "exists": { "field": "details.text" } }, "inner_hits": { "_source": ["details.fieldName", "details.text"], "size": 100 } } }, "_source": ["paperType", "paperName"] }
返回结果中,每个匹配的文档会包含inner_hits.details.hits.hits数组,里面就是符合条件的嵌套条目,你可以在应用代码中将这部分数据替换为文档的details字段,得到预期的结构。
方法二:索引前拆分字段(更推荐)
如果可以重新索引文档,建议使用Ingest Pipeline在索引阶段就将details拆分为两个独立数组:仅包含text类型条目的details_text,和仅包含double类型条目的details_double。这样查询时直接返回details_text即可,无需额外处理。
- 创建Ingest Pipeline:
PUT _ingest/pipeline/split_details { "description": "Split details into text and double-specific arrays", "processors": [ { "split": { "field": "details", "target_field": "temp_details" } }, { "foreach": { "field": "temp_details", "processor": { "script": { "source": """ def item = ctx._ingest.value; if (item.containsKey('text')) { if (!ctx.containsKey('details_text')) ctx.details_text = []; ctx.details_text.add(item); } else if (item.containsKey('double')) { if (!ctx.containsKey('details_double')) ctx.details_double = []; ctx.details_double.add(item); } """ } } } }, { "remove": { "field": ["temp_details", "details"] } } ] }
- 使用该pipeline重新索引文档:
POST _reindex { "source": { "index": "your_source_index" }, "dest": { "index": "your_target_index", "pipeline": "split_details" } }
- 查询时直接返回需要的字段:
GET your_target_index/_search { "_source": ["paperType", "paperName", "details_text"] }
这样返回的文档结构会直接符合你的预期,无需额外处理。
内容的提问来源于stack exchange,提问作者Erdem Tuna
相关产品推荐
相关产品推荐

