查询Elasticsearch嵌套类型数据,且只返回嵌套数据中命中的元素.md

时间:2022-03-28 15:41:51

## 测试环境

  • Elasticsearch 6.3
  • Kibana 6.3

## 造点测试数据

新建一个index作为测试

以下是一个存储博客文章及其评论的数据结构,评论(comments)是nested类型:

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、HTML5、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": {}
          }
        }
      ]
    }
  }
}

返回结果:

{
  "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、HTML5、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": []
            }
          }
        }
      }
    ]
  }
}

查询content中出现[java]或评论中出现[python]的数据

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、HTML5、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": []
            }
          }
        }
      }
    ]
  }
}