es 子文档查询

本文档介绍了如何在ElasticSearch中进行子文档查询,包括创建索引和映射、添加文档,以及检索和设置检索选项。通过示例展示了如何获取匹配的内嵌文档,并解释了from和size参数用于控制返回结果的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用方法

  • Nested inner hitsedit
创建索引及映射
PUT test
{
  "mappings": {
    "_doc": {
      "properties": {
        "comments": {
          "type": "nested"
        }
      }
    }
  }
}
创建文档
PUT test/_doc/1
{
  "title": "Test title",
  "comments": [
	              {
	                  "author": "kimchy",
	                  "number": 1,
	                  "like": "dog"
	              },
	              {
	                  "author": "nik9000",
	                  "number": 2,
	                  "like": "dog"
	              },
	              {
	                  "author": "tom",
	                  "number": 4,
	                  "like": "cat"
	              },
	              {
	                  "author": "nik900",
	                  "number": 2,
	                  "like": "dog"
	              }
  ]
}
检索
POST test/_search
{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "match": {"comments.number" : 2}
      },
      "inner_hits": {} 
    }
  }
}
  • 结果
{
{
    "took": 13,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.24116206,
        "hits": [
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.24116206,
                "_source": {
                    "title": "Test title",
                    "comments": [
                        {
                            "author": "kimchy",
                            "number": 1,
                            "like": "dog"
                        },
                        {
                            "author": "nik9000",
                            "number": 2,
                            "like": "dog"
                        },
                        {
                            "author": "tom",
                            "number": 4,
                            "like": "cat"
                        },
                        {
                            "author": "nik900",
                            "number": 2,
                            "like": "dog"
                        }
                    ]
                },
                "inner_hits": {
                    "comments": {
                        "hits": {
                            "total": 3,
                            "max_score": 0.24116206,
                            "hits": [
                                {
                                    "_index": "test_index",
                                    "_type": "_doc",
                                    "_id": "1",
                                    "_nested": {
                                        "field": "comments",
                                        "offset": 5
                                    },
                                    "_score": 0.24116206,
                                    "_source": {
                                        "author": "kimchy",
                                        "number": 1,
                                        "like": "dog"
                                    }
                                },
                                {
                                    "_index": "test_index",
                                    "_type": "_doc",
                                    "_id": "1",
                                    "_nested": {
                                        "field": "comments",
                                        "offset": 4
                                    },
                                    "_score": 0.24116206,
                                    "_source": {
                                        "author": "nik9000",
                                        "number": 2,
                                        "like": "dog"
                                    }
                                },
                                {
                                    "_index": "test_index",
                                    "_type": "_doc",
                                    "_id": "1",
                                    "_nested": {
                                        "field": "comments",
                                        "offset": 3
                                    },
                                    "_score": 0.24116206,
                                    "_source": {
                                        "author": "nik900",
                                        "number": 2,
                                        "like": "dog"
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}
选项说明:
  • from
    The offset from where the first hit to fetch for each inner_hits in the returned regular search hits.

  • size
    The maximum number of hits to return per inner_hits. By default the top three matching hits are returned.

  • sort
    How the inner hits should be sorted per inner_hits. By default the hits are sorted by the score.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值