es 子文档查询

版权声明:未经允许不得转载。 https://blog.csdn.net/qq_35958788/article/details/88044725

使用方法

  • 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.

猜你喜欢

转载自blog.csdn.net/qq_35958788/article/details/88044725