如下是一個存儲博客文章及其評論的數據結構,評論(comments)是nested類型:java
PUT /es_blog { "mappings": { "blogpost": { "properties": { "title": { "type": "text" }, "summary": { "type": "text" }, "content": { "type": "text" }, "comments": { "type": "nested", "properties": { "name": { "type": "text" }, "comment": { "type": "text" }, "age": { "type": "short" }, "stars": { "type": "short" }, "date": { "type": "date" } } } } } } }
PUT /es_blog/blogpost/1 { "title": "無標題", "summary": "全棧工程師、JAVA、HTML5", "content": "全棧工程師須要掌握:JAVA、HTML五、JavaScript、經常使用緩存、大數據等等", "comments": [ { "name": "John Smith", "comment": "Great article", "age": 28, "stars": 4, "date": "2014-09-01" }, { "name": "Alice White", "comment": "More like this please", "age": 31, "stars": 5, "date": "2014-10-22" } ] } PUT /es_blog/blogpost/2 { "title": "Java後端開發工程師", "summary": "JAVA、Oracle、Hibernate、Spring", "content": "Java後端開發工程師須要掌握:JAVA、Oracle、Hibernate、Spring、經常使用緩存等等", "comments": [ { "name": "John Smith", "comment": "工程師真牛", "age": 28, "stars": 4, "date": "2014-09-01" }, { "name": "Alice White", "comment": "Java工程師真牛", "age": 31, "stars": 5, "date": "2014-10-22" } ] } PUT /es_blog/blogpost/3 { "title": "大數據工程師", "summary": "Hadoop、Hive、Hdfs、JAVA", "content": "大數據工程師須要掌握:Hadoop、Hive、Hdfs、JAVA、Spark、流式計算等等", "comments": [ { "name": "John Smith", "comment": "大數據工程師真牛", "age": 28, "stars": 4, "date": "2014-09-01" }, { "name": "Alice White", "comment": "我不會啊", "age": 31, "stars": 5, "date": "2014-10-22" } ] } PUT /es_blog/blogpost/4 { "title": "機器學習工程師", "summary": "Python、迴歸算法、分類算法、神經網絡、數據基礎", "content": "機器學習工程師須要掌握:Python、迴歸算法、分類算法、神經網絡、有紮實的數據基礎等等", "comments": [ { "name": "John Smith", "comment": "機器學習NX", "age": 28, "stars": 4, "date": "2014-09-01" }, { "name": "Alice White", "comment": "Python好學麼?", "age": 31, "stars": 5, "date": "2014-10-22" } ] }
GET es_blog/blogpost/_search { "_source": { "includes": [ "*" ], "excludes": [ "comments" //去掉返回結果中父級中的comments信息 ] }, "query": { "bool": { "should": [ { "match": { "title": "工程師" } }, { "match": { "summary": "工程師" } }, { "match": { "content": "工程師" } }, { "nested": { "path": "comments", "query": { "bool": { "should": [ { "match": { "comments.comment": "工程師" } } ] } }, "inner_hits": {} } } ] } } }
返回結果:python
{ "took": 9, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 3.5560012, "hits": [ { "_index": "es_blog", "_type": "blogpost", "_id": "3", "_score": 3.5560012, "_source": { "summary": "Hadoop、Hive、Hdfs、JAVA", "title": "大數據工程師", "content": "大數據工程師須要掌握:Hadoop、Hive、Hdfs、JAVA、Spark、流式計算等等" }, "inner_hits": { "comments": { "hits": { "total": 1, "max_score": 1.8299085, "hits": [ { "_index": "es_blog", "_type": "blogpost", "_id": "3", "_nested": { "field": "comments", "offset": 0 }, "_score": 1.8299085, "_source": { "name": "John Smith", "comment": "大數據工程師真牛", "age": 28, "stars": 4, "date": "2014-09-01" } } ] } } } }, { "_index": "es_blog", "_type": "blogpost", "_id": "2", "_score": 3.1327708, "_source": { "summary": "JAVA、Oracle、Hibernate、Spring", "title": "Java後端開發工程師", "content": "Java後端開發工程師須要掌握:JAVA、Oracle、Hibernate、Spring、經常使用緩存等等" }, "inner_hits": { "comments": { "hits": { "total": 2, "max_score": 2.0794415, "hits": [ { "_index": "es_blog", "_type": "blogpost", "_id": "2", "_nested": { "field": "comments", "offset": 0 }, "_score": 2.0794415, "_source": { "name": "John Smith", "comment": "工程師真牛", "age": 28, "stars": 4, "date": "2014-09-01" } }, { "_index": "es_blog", "_type": "blogpost", "_id": "2", "_nested": { "field": "comments", "offset": 1 }, "_score": 1.9221728, "_source": { "name": "Alice White", "comment": "Java工程師真牛", "age": 31, "stars": 5, "date": "2014-10-22" } } ] } } } }, { "_index": "es_blog", "_type": "blogpost", "_id": "1", "_score": 1.7260926, "_source": { "summary": "全棧工程師、JAVA、HTML5", "title": "無標題", "content": "全棧工程師須要掌握:JAVA、HTML五、JavaScript、經常使用緩存、大數據等等" }, "inner_hits": { "comments": { "hits": { "total": 0, "max_score": null, "hits": [] } } } }, { "_index": "es_blog", "_type": "blogpost", "_id": "4", "_score": 1.0651813, "_source": { "summary": "Python、迴歸算法、分類算法、神經網絡、數據基礎", "title": "機器學習工程師", "content": "機器學習工程師須要掌握:Python、迴歸算法、分類算法、神經網絡、有紮實的數據基礎等等" }, "inner_hits": { "comments": { "hits": { "total": 0, "max_score": null, "hits": [] } } } } ] } }
GET es_blog/blogpost/_search { "_source": { "includes": [ "*" ], "excludes": [ "comments" ] }, "query": { "bool": { "should": [ { "match": { "content": "java" } }, { "nested": { "path": "comments", "query": { "bool": { "should": [ { "match": { "comments.comment": "python" } } ] } }, "inner_hits": {} } } ] } } }
返回結果算法
{ "took": 9, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 1.3112576, "hits": [ { "_index": "es_blog", "_type": "blogpost", "_id": "4", "_score": 1.3112576, "_source": { "summary": "Python、迴歸算法、分類算法、神經網絡、數據基礎", "title": "機器學習工程師", "content": "機器學習工程師須要掌握:Python、迴歸算法、分類算法、神經網絡、有紮實的數據基礎等等" }, "inner_hits": { "comments": { "hits": { "total": 1, "max_score": 1.3112576, "hits": [ { "_index": "es_blog", "_type": "blogpost", "_id": "4", "_nested": { "field": "comments", "offset": 1 }, "_score": 1.3112576, "_source": { "name": "Alice White", "comment": "Python好學麼?", "age": 31, "stars": 5, "date": "2014-10-22" } } ] } } } }, { "_index": "es_blog", "_type": "blogpost", "_id": "2", "_score": 0.75974846, "_source": { "summary": "JAVA、Oracle、Hibernate、Spring", "title": "Java後端開發工程師", "content": "Java後端開發工程師須要掌握:JAVA、Oracle、Hibernate、Spring、經常使用緩存等等" }, "inner_hits": { "comments": { "hits": { "total": 0, "max_score": null, "hits": [] } } } }, { "_index": "es_blog", "_type": "blogpost", "_id": "1", "_score": 0.2876821, "_source": { "summary": "全棧工程師、JAVA、HTML5", "title": "無標題", "content": "全棧工程師須要掌握:JAVA、HTML五、JavaScript、經常使用緩存、大數據等等" }, "inner_hits": { "comments": { "hits": { "total": 0, "max_score": null, "hits": [] } } } }, { "_index": "es_blog", "_type": "blogpost", "_id": "3", "_score": 0.2876821, "_source": { "summary": "Hadoop、Hive、Hdfs、JAVA", "title": "大數據工程師", "content": "大數據工程師須要掌握:Hadoop、Hive、Hdfs、JAVA、Spark、流式計算等等" }, "inner_hits": { "comments": { "hits": { "total": 0, "max_score": null, "hits": [] } } } } ] } }