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

测试环境


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

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/robinzh/p/9310977.html

时间: 2024-08-10 08:42:54

查询Elasticsearch嵌套类型数据,且只返回嵌套数据中命中的元素的相关文章

关于用Hibernate执行sql查询,字符字段类型只返回第一个字节的问题

关于用Hibernate执行sql查询,字符字段类型只返回第一个字节的问题 今天遇到了一个问题,就是在Hibernate中,我用sql去查询mysql数据库里面的user表里面的username字段,但是发现查出来的数据都是只有第一个字,例如:在user表里面的username字段中有一条数据是:"Chen chiwei",查询后返回的结果却是这条数据的第一个字母:"C":于是乎,我查了一下user这张表的设计,发现username这个字段的类型是'char'类型,

SQL查询某表是否存在及返回新增数据的ID

下面简单介绍了SQL查询某表是否存在以及返回新增数据的ID值. 1.查询表是否存在: 表名:"t_Demo", type = 'u'  查看是不是用户表 select * from sysobjects where id = object_id('t_Demo') and type = 'u' select * from sys.tables where name='t_Demo' and type = 'u' 2.查询字段是否存在: 表名:"t_Demo", 字段

mybatis 关联查询时,从表只返回第一条记录解决办法

如果两表联查,主表和明细表的主键都是id的话,明细表的多条只能查询出来第一条. 造成以上情况可能的原因: 1.级联查询的时候,主表和从表有一样的字段名的时候,在mysql上命令查询是没问题的.但在mybatis中主从表需要为相同字段名设置别名.设置了别名就OK了. 例子: 主表Standard, 从表StandEntity,均有名为id的字段 1 <resultMap id="StandardAndEntityResultMap" type="whu.edu.irlab

在C#应用程序中,利用表值参数过滤重复,批量向数据库导入数据,并且返回重复数据

在很多情况下,应用程序都需要实现excel数据导入功能,数据如果只有几十条,或上百条,甚至上千条,速度还好. 但是不仅如此,如果客户提供给你的excel本身存在着重复数据,或是excel中的某些数据已经在数据库存在,那这时,在向数据库插入数据前你还得判重,如果不存在才进行导入 通常,我们第一步就会通过上传的方式把excel中的数据读到内存,然后通过循环的方式得出一条一条数据,接着对于每条数据用关键字段去往数据库中进行一次查重,若存在则不做事情,若 不存在则向数据库中插入一条数据.这样一来,我们每

在Activity之间传递数据—获取Activity返回的数据

在获取返回值时要注意的是打开Activity的方式,用方法:startActivityForResult 接收时,重写方法:onActivityResult 在子Activity中,写数据用方法:setResult MainActivity代码: private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCon

springmvc,通过ajax方式提交页面数据,后台返回json数据中文信息乱码

本人刚开始接触springmvc,项目搭建参照https://my.oschina.net/gaussik/blog/385697.在用IDEA写登录注册的时候,想通过ajax方式提交数据到后台,然后遇到如题所述的乱码问题,然后度娘了好多,终于解决了.废话不多说,直接上代码. 首先是登录页面login.jsp 1 <%-- 2 Created by IntelliJ IDEA. 3 User: PENG027 4 Date: 2016/11/11 5 Time: 15:48 6 To chang

删除表中重复数据,只删除重复数据中ID最小的

delete t_xxx_user where recid in ( select recid from t_xxx_user where recid in ( select min(recid) from t_sz_grid_forecast_user where ddatetime = to_date('2019-12-17 16:00:00','yyyy-MM-dd hh24:mi:ss') and forecaster = 'XXX'  group by venueid,ybsx hav

通过id查询用户,但是只返回指定的字段

使用hibernate和spring MVC 通过id查询到一个用户,但是只返回指定的字段 方式一: 拼接hql /*** * 通过数据库ID查询用户,但是只返回指定的字段 * @param id * @param propertyNames : 指定的多个成员变量 * @return */ public Object[] getPropertiesById(int id,String[] propertyNames){ if(ValueWidget.isNullOrEmpty(property

SpringBoot 03_利用FastJson返回Json数据

自上一节:SpringBoot 02_返回json数据,可以返回json数据之后,由于有些人习惯于不同的Json框架,比如fastjson,这里介绍一下如何在SpringBoot中集成fastjson来实现对数据的json序列化. 在使用fastjson时,可以有以下两种集成方式,但是都需要引入fastjson的依赖包 1:引入fastjson依赖包 <dependency> <groupId>com.alibaba</groupId> <artifactId&g